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...@@ -2592,6 +2592,7 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As
2592 expr->const_val.ok = true;2592 expr->const_val.ok = true;
25932593
2594 bignum_init_unsigned(&expr->const_val.data.x_bignum, x);2594 bignum_init_unsigned(&expr->const_val.data.x_bignum, x);
2595
2595 return g->builtin_types.entry_num_lit_int;2596 return g->builtin_types.entry_num_lit_int;
2596}2597}
25972598
...@@ -2603,12 +2604,7 @@ static TypeTableEntry *resolve_expr_const_val_as_float_num_lit(CodeGen *g, AstNo...@@ -2603,12 +2604,7 @@ static TypeTableEntry *resolve_expr_const_val_as_float_num_lit(CodeGen *g, AstNo
26032604
2604 bignum_init_float(&expr->const_val.data.x_bignum, x);2605 bignum_init_float(&expr->const_val.data.x_bignum, x);
26052606
2606 if (expected_type) {2607 return g->builtin_types.entry_num_lit_float;
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 }
2612}2608}
26132609
2614static TypeTableEntry *resolve_expr_const_val_as_bignum_op(CodeGen *g, AstNode *node,2610static 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...@@ -2915,12 +2911,12 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
2915 assert(node->type == NodeTypeBinOpExpr);2911 assert(node->type == NodeTypeBinOpExpr);
2916 BinOpType bin_op_type = node->data.bin_op_expr.bin_op;2912 BinOpType bin_op_type = node->data.bin_op_expr.bin_op;
29172913
2918 AstNode *op1 = node->data.bin_op_expr.op1;2914 AstNode **op1 = &node->data.bin_op_expr.op1;
2919 AstNode *op2 = node->data.bin_op_expr.op2;2915 AstNode **op2 = &node->data.bin_op_expr.op2;
2920 TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, op1);2916 TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, *op1);
2921 TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, op2);2917 TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, *op2);
29222918
2923 AstNode *op_nodes[] = {op1, op2};2919 AstNode *op_nodes[] = {*op1, *op2};
2924 TypeTableEntry *op_types[] = {op1_type, op2_type};2920 TypeTableEntry *op_types[] = {op1_type, op2_type};
29252921
2926 TypeTableEntry *resolved_type = resolve_peer_type_compatibility(g, import, context, node,2922 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...@@ -2942,8 +2938,8 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
2942 return g->builtin_types.entry_invalid;2938 return g->builtin_types.entry_invalid;
2943 }2939 }
29442940
2945 ConstExprValue *op1_val = &get_resolved_expr(op1)->const_val;2941 ConstExprValue *op1_val = &get_resolved_expr(*op1)->const_val;
2946 ConstExprValue *op2_val = &get_resolved_expr(op2)->const_val;2942 ConstExprValue *op2_val = &get_resolved_expr(*op2)->const_val;
2947 if (!op1_val->ok || !op2_val->ok) {2943 if (!op1_val->ok || !op2_val->ok) {
2948 return g->builtin_types.entry_bool;2944 return g->builtin_types.entry_bool;
2949 }2945 }
...@@ -3637,6 +3633,17 @@ static TypeTableEntry *analyze_continue_expr(CodeGen *g, ImportTableEntry *impor...@@ -3637,6 +3633,17 @@ static TypeTableEntry *analyze_continue_expr(CodeGen *g, ImportTableEntry *impor
3637 return g->builtin_types.entry_unreachable;3633 return g->builtin_types.entry_unreachable;
3638}3634}
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
3640static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,3647static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockContext *parent_context,
3641 TypeTableEntry *expected_type, AstNode *node,3648 TypeTableEntry *expected_type, AstNode *node,
3642 AstNode **then_node, AstNode **else_node, bool cond_is_const, bool cond_bool_val)3649 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...@@ -3682,7 +3689,7 @@ static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockCon
3682 }3689 }
36833690
3684 if (!cond_is_const) {3691 if (!cond_is_const) {
3685 return result_type;3692 return add_error_if_type_is_num_lit(g, result_type, node);
3686 }3693 }
36873694
3688 ConstExprValue *other_const_val;3695 ConstExprValue *other_const_val;
...@@ -3692,7 +3699,7 @@ static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockCon...@@ -3692,7 +3699,7 @@ static TypeTableEntry *analyze_if(CodeGen *g, ImportTableEntry *import, BlockCon
3692 other_const_val = &get_resolved_expr(*else_node)->const_val;3699 other_const_val = &get_resolved_expr(*else_node)->const_val;
3693 }3700 }
3694 if (!other_const_val->ok) {3701 if (!other_const_val->ok) {
3695 return result_type;3702 return add_error_if_type_is_num_lit(g, result_type, node);
3696 }3703 }
36973704
3698 ConstExprValue *const_val = &get_resolved_expr(node)->const_val;3705 ConstExprValue *const_val = &get_resolved_expr(node)->const_val;