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 {
1010 // let c : i32; // not yet support for const variables
1111 // let d; // parse error
1212 if (a + b == 3) {
13 puts("OK");
13 let no_conflict = 5;
14 if (no_conflict == 5) { puts("OK 1"); }
1415 }
16
17 let c = {
18 let no_conflict = 10;
19 no_conflict
20 };
21 if (c == 10) { puts("OK 2"); }
1522 exit(0);
1623}
src/analyze.cpp+6-4
......@@ -335,9 +335,8 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, TypeTableEntry *
335335 if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid)
336336 return; // already complained
337337 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
341340 add_node_error(g, node,
342341 buf_sprintf("type mismatch. expected %s. got %s",
343342 buf_ptr(&expected_type->name),
......@@ -632,14 +631,16 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
632631 {
633632 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
635637 TypeTableEntry *else_type;
636638 if (node->data.if_expr.else_node) {
637639 else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);
638640 } else {
639641 else_type = g->builtin_types.entry_void;
640642 }
641 TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type,
642 node->data.if_expr.then_block);
643
643644
644645 TypeTableEntry *primary_type;
645646 TypeTableEntry *other_type;
......@@ -651,6 +652,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
651652 other_type = else_type;
652653 }
653654
655 check_type_compatibility(g, node, primary_type, other_type);
654656 check_type_compatibility(g, node, expected_type, other_type);
655657 return_type = primary_type;
656658 break;