authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 11:56:59-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 11:56:59-07:00
log6494cf208e8045c58e8ab79fb76e360cec077263
tree9b91865108d526d0427a5fb9af5e5b663f2128c9
parent0c2cc9d2cffd783a4db159cb84027e4016f618ff

fix if-else type mismatch crash


2 files changed, 14 insertions(+), 5 deletions(-)

example/expressions/expressions.zig+8-1
...@@ -10,7 +10,14 @@ export fn _start() -> unreachable {...@@ -10,7 +10,14 @@ export fn _start() -> unreachable {
10 // let c : i32; // not yet support for const variables10 // let c : i32; // not yet support for const variables
11 // let d; // parse error11 // let d; // parse error
12 if (a + b == 3) {12 if (a + b == 3) {
13 puts("OK");13 let no_conflict = 5;
14 if (no_conflict == 5) { puts("OK 1"); }
14 }15 }
16
17 let c = {
18 let no_conflict = 10;
19 no_conflict
20 };
21 if (c == 10) { puts("OK 2"); }
15 exit(0);22 exit(0);
16}23}
src/analyze.cpp+6-4
...@@ -335,9 +335,8 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, TypeTableEntry *...@@ -335,9 +335,8 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, TypeTableEntry *
335 if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid)335 if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid)
336 return; // already complained336 return; // already complained
337 if (actual_type == g->builtin_types.entry_unreachable)337 if (actual_type == g->builtin_types.entry_unreachable)
338 return; // TODO: is this true?338 return; // sorry toots; gotta run. good luck with that expected type.
339339
340 // TODO better error message
341 add_node_error(g, node,340 add_node_error(g, node,
342 buf_sprintf("type mismatch. expected %s. got %s",341 buf_sprintf("type mismatch. expected %s. got %s",
343 buf_ptr(&expected_type->name),342 buf_ptr(&expected_type->name),
...@@ -632,14 +631,16 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -632,14 +631,16 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
632 {631 {
633 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.if_expr.condition);632 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.if_expr.condition);
634633
634 TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type,
635 node->data.if_expr.then_block);
636
635 TypeTableEntry *else_type;637 TypeTableEntry *else_type;
636 if (node->data.if_expr.else_node) {638 if (node->data.if_expr.else_node) {
637 else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);639 else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);
638 } else {640 } else {
639 else_type = g->builtin_types.entry_void;641 else_type = g->builtin_types.entry_void;
640 }642 }
641 TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type,643
642 node->data.if_expr.then_block);
643644
644 TypeTableEntry *primary_type;645 TypeTableEntry *primary_type;
645 TypeTableEntry *other_type;646 TypeTableEntry *other_type;
...@@ -651,6 +652,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -651,6 +652,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
651 other_type = else_type;652 other_type = else_type;
652 }653 }
653654
655 check_type_compatibility(g, node, primary_type, other_type);
654 check_type_compatibility(g, node, expected_type, other_type);656 check_type_compatibility(g, node, expected_type, other_type);
655 return_type = primary_type;657 return_type = primary_type;
656 break;658 break;