fix if-else type mismatch crash

This commit is contained in:
Josh Wolfe 2015-12-03 11:56:59 -07:00
parent 0c2cc9d2cf
commit 6494cf208e
2 changed files with 14 additions and 5 deletions

View file

@ -10,7 +10,14 @@ export fn _start() -> unreachable {
// let c : i32; // not yet support for const variables // let c : i32; // not yet support for const variables
// let d; // parse error // let d; // parse error
if (a + b == 3) { 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); exit(0);
} }

View file

@ -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) if (expected_type == g->builtin_types.entry_invalid || actual_type == g->builtin_types.entry_invalid)
return; // already complained return; // already complained
if (actual_type == g->builtin_types.entry_unreachable) 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, add_node_error(g, node,
buf_sprintf("type mismatch. expected %s. got %s", buf_sprintf("type mismatch. expected %s. got %s",
buf_ptr(&expected_type->name), 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); 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; TypeTableEntry *else_type;
if (node->data.if_expr.else_node) { if (node->data.if_expr.else_node) {
else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node); else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);
} else { } else {
else_type = g->builtin_types.entry_void; 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 *primary_type;
TypeTableEntry *other_type; TypeTableEntry *other_type;
@ -651,6 +652,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
other_type = else_type; other_type = else_type;
} }
check_type_compatibility(g, node, primary_type, other_type);
check_type_compatibility(g, node, expected_type, other_type); check_type_compatibility(g, node, expected_type, other_type);
return_type = primary_type; return_type = primary_type;
break; break;