From 706f72f1b47ace8c6c9eecde2682ac46389af1ca Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Jan 2016 02:45:54 -0700 Subject: fix hang when returning from while loop also fixes duplicate error message for function missing return type. also makes guess number game use %void for main return type. closes #58 --- src/analyze.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index a137fdb3cb..7553796ec3 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1344,6 +1344,16 @@ static AstNode *create_ast_type_node(CodeGen *g, ImportTableEntry *import, TypeT return node; } +static AstNode *create_ast_void_node(CodeGen *g, ImportTableEntry *import, AstNode *source_node) { + AstNode *node = create_ast_node(g, import, NodeTypeContainerInitExpr); + node->data.container_init_expr.kind = ContainerInitKindArray; + node->data.container_init_expr.type = create_ast_type_node(g, import, g->builtin_types.entry_void); + node->line = source_node->line; + node->column = source_node->column; + normalize_parent_ptrs(node); + return node; +} + static TypeTableEntry *create_and_analyze_cast_node(CodeGen *g, ImportTableEntry *import, BlockContext *context, TypeTableEntry *cast_to_type, AstNode *node) { @@ -3596,25 +3606,17 @@ static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import, return g->builtin_types.entry_invalid; } + if (!node->data.return_expr.expr) { + node->data.return_expr.expr = create_ast_void_node(g, import, node); + normalize_parent_ptrs(node); + } + if (node->data.return_expr.kind != ReturnKindUnconditional) { zig_panic("TODO analyze_return_expr conditional"); } TypeTableEntry *expected_return_type = get_return_type(context); - TypeTableEntry *actual_return_type; - if (node->data.return_expr.expr) { - actual_return_type = analyze_expression(g, import, context, expected_return_type, node->data.return_expr.expr); - } else { - actual_return_type = g->builtin_types.entry_void; - } - - if (actual_return_type->id == TypeTableEntryIdUnreachable) { - // "return exit(0)" should just be "exit(0)". - add_node_error(g, node, buf_sprintf("returning is unreachable")); - actual_return_type = g->builtin_types.entry_invalid; - } - - resolve_type_compatibility(g, import, context, node, expected_return_type, actual_return_type); + analyze_expression(g, import, context, expected_return_type, node->data.return_expr.expr); return g->builtin_types.entry_unreachable; } -- cgit v1.2.3