diff --git a/example/expressions/expressions.zig b/example/expressions/expressions.zig index 48e7ac9196..e4b7703080 100644 --- a/example/expressions/expressions.zig +++ b/example/expressions/expressions.zig @@ -10,7 +10,14 @@ export fn _start() -> unreachable { // let c : i32; // not yet support for const variables // let d; // parse error if (a + b == 3) { - puts("OK"); + let no_conflict = 5; + if (no_conflict == 5) { puts("OK 1"); } } + + let c = { + let no_conflict = 10; + no_conflict + }; + if (c == 10) { puts("OK 2"); } exit(0); } diff --git a/src/analyze.cpp b/src/analyze.cpp index c0ce2f7e8e..614b80052c 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -335,9 +335,8 @@ static void check_type_compatibility(CodeGen *g, AstNode *node, TypeTableEntry * if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid) return; // already complained if (actual_type == g->builtin_types.entry_unreachable) - return; // TODO: is this true? + return; // sorry toots; gotta run. good luck with that expected type. - // TODO better error message add_node_error(g, node, buf_sprintf("type mismatch. expected %s. got %s", buf_ptr(&expected_type->name), @@ -632,14 +631,16 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, { analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.if_expr.condition); + TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type, + node->data.if_expr.then_block); + TypeTableEntry *else_type; if (node->data.if_expr.else_node) { else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node); } else { else_type = g->builtin_types.entry_void; } - TypeTableEntry *then_type = analyze_expression(g, import, context, expected_type, - node->data.if_expr.then_block); + TypeTableEntry *primary_type; TypeTableEntry *other_type; @@ -651,6 +652,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, other_type = else_type; } + check_type_compatibility(g, node, primary_type, other_type); check_type_compatibility(g, node, expected_type, other_type); return_type = primary_type; break;