authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-12 08:53:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-12 08:53:00-07:00
log5a479720ec5786145dac6c85deae4e322bd5972e
tree309c5fd5e0a3062657c1ef2419d587b59394af64
parent954a451c516288b3638692febf557ee8190918dc

add error for unable to infer expression type in if statements

closes #136

1 files changed, 22 insertions(+), 15 deletions(-)

src/analyze.cpp+22-15
......@@ -2592,6 +2592,7 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As
25922592 expr->const_val.ok = true;
25932593
25942594 bignum_init_unsigned(&expr->const_val.data.x_bignum, x);
2595
25952596 return g->builtin_types.entry_num_lit_int;
25962597}
25972598
......@@ -2603,12 +2604,7 @@ static TypeTableEntry *resolve_expr_const_val_as_float_num_lit(CodeGen *g, AstNo
26032604
26042605 bignum_init_float(&expr->const_val.data.x_bignum, x);
26052606
2606 if (expected_type) {
2607 num_lit_fits_in_other_type(g, node, expected_type);
2608 return expected_type;
2609 } else {
2610 return g->builtin_types.entry_num_lit_float;
2611 }
2607 return g->builtin_types.entry_num_lit_float;
26122608}
26132609
26142610static TypeTableEntry *resolve_expr_const_val_as_bignum_op(CodeGen *g, AstNode *node,
......@@ -2915,12 +2911,12 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
29152911 assert(node->type == NodeTypeBinOpExpr);
29162912 BinOpType bin_op_type = node->data.bin_op_expr.bin_op;
29172913
2918 AstNode *op1 = node->data.bin_op_expr.op1;
2919 AstNode *op2 = node->data.bin_op_expr.op2;
2920 TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, op1);
2921 TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, op2);
2914 AstNode **op1 = &node->data.bin_op_expr.op1;
2915 AstNode **op2 = &node->data.bin_op_expr.op2;
2916 TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, *op1);
2917 TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, *op2);
29222918
2923 AstNode *op_nodes[] = {op1, op2};
2919 AstNode *op_nodes[] = {*op1, *op2};
29242920 TypeTableEntry *op_types[] = {op1_type, op2_type};
29252921
29262922 TypeTableEntry *resolved_type = resolve_peer_type_compatibility(g, import, context, node,
......@@ -2942,8 +2938,8 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
29422938 return g->builtin_types.entry_invalid;
29432939 }
29442940
2945 ConstExprValue *op1_val = &get_resolved_expr(op1)->const_val;
2946 ConstExprValue *op2_val = &get_resolved_expr(op2)->const_val;
2941 ConstExprValue *op1_val = &get_resolved_expr(*op1)->const_val;
2942 ConstExprValue *op2_val = &get_resolved_expr(*op2)->const_val;
29472943 if (!op1_val->ok || !op2_val->ok) {
29482944 return g->builtin_types.entry_bool;
29492945 }
......@@ -3637,6 +3633,17 @@ static TypeTableEntry *analyze_continue_expr(CodeGen *g, ImportTableEntry *impor
36373633 return g->builtin_types.entry_unreachable;
36383634}
36393635
3636static TypeTableEntry *add_error_if_type_is_num_lit(CodeGen *g, TypeTableEntry *type_entry, AstNode *source_node) {
3637 if (type_entry->id == TypeTableEntryIdNumLitInt ||
3638 type_entry->id == TypeTableEntryIdNumLitFloat)
3639 {
3640 add_node_error(g, source_node, buf_sprintf("unable to infer expression type"));
3641 return g->builtin_types.entry_invalid;
3642 } else {
3643 return type_entry;
3644 }
3645}
3646
36403647static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,
36413648 TypeTableEntry *expected_type, AstNode *node,
36423649 AstNode **then_node, AstNode **else_node, bool cond_is_const, bool cond_bool_val)
......@@ -3682,7 +3689,7 @@ static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockCon
36823689 }
36833690
36843691 if (!cond_is_const) {
3685 return result_type;
3692 return add_error_if_type_is_num_lit(g, result_type, node);
36863693 }
36873694
36883695 ConstExprValue *other_const_val;
......@@ -3692,7 +3699,7 @@ static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockCon
36923699 other_const_val = &get_resolved_expr(*else_node)->const_val;
36933700 }
36943701 if (!other_const_val->ok) {
3695 return result_type;
3702 return add_error_if_type_is_num_lit(g, result_type, node);
36963703 }
36973704
36983705 ConstExprValue *const_val = &get_resolved_expr(node)->const_val;