authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-11 16:15:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-11 16:15:08-07:00
log08eb19456b319cc5b0c287773d52850f12f2bcb5
tree99188572c1ba6c53b9cc8fbd92bb007f3f85cfb6
parenta180168871b6e32a8c0825b1eb309587c3e0d179

add compile error for missing enumeration value in switch


2 files changed, 42 insertions(+), 0 deletions(-)

src/analyze.cpp+25
......@@ -4241,6 +4241,8 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
42414241 return resolve_expr_const_val_as_bool(g, node, g->is_release_build, true);
42424242 } else if (buf_eql_str(&var_name, "is_test")) {
42434243 return resolve_expr_const_val_as_bool(g, node, g->is_test_build, true);
4244 } else if (buf_eql_str(&var_name, "os")) {
4245 zig_panic("TODO");
42444246 } else {
42454247 add_node_error(g, *str_node,
42464248 buf_sprintf("unrecognized compile variable: '%s'", buf_ptr(&var_name)));
......@@ -4650,6 +4652,11 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
46504652 buf_sprintf("switch on unreachable expression not allowed"));
46514653 return g->builtin_types.entry_invalid;
46524654 } else {
4655 int *field_use_counts = nullptr;
4656 if (expr_type->id == TypeTableEntryIdEnum) {
4657 field_use_counts = allocate<int>(expr_type->data.enumeration.field_count);
4658 }
4659
46534660 AstNode *else_prong = nullptr;
46544661 for (int prong_i = 0; prong_i < prong_count; prong_i += 1) {
46554662 AstNode *prong_node = node->data.switch_expr.prongs.at(prong_i);
......@@ -4686,6 +4693,14 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
46864693 if (type_enum_field->type_entry != var_type) {
46874694 all_agree_on_var_type = false;
46884695 }
4696 uint32_t field_index = type_enum_field->value;
4697 assert(field_use_counts);
4698 field_use_counts[field_index] += 1;
4699 if (field_use_counts[field_index] > 1) {
4700 add_node_error(g, item_node,
4701 buf_sprintf("duplicate switch value: '%s'",
4702 buf_ptr(type_enum_field->name)));
4703 }
46894704 } else {
46904705 add_node_error(g, item_node,
46914706 buf_sprintf("enum '%s' has no field '%s'",
......@@ -4729,6 +4744,16 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
47294744 prong_node->data.switch_prong.expr);
47304745 peer_nodes[prong_i] = prong_node->data.switch_prong.expr;
47314746 }
4747
4748 if (expr_type->id == TypeTableEntryIdEnum && !else_prong) {
4749 for (uint32_t i = 0; i < expr_type->data.enumeration.field_count; i += 1) {
4750 if (field_use_counts[i] == 0) {
4751 add_node_error(g, node,
4752 buf_sprintf("enumeration value '%s' not handled in switch",
4753 buf_ptr(expr_type->data.enumeration.fields[i].name)));
4754 }
4755 }
4756 }
47324757 }
47334758 return resolve_peer_type_compatibility(g, import, context, node, peer_nodes, peer_types, prong_count);
47344759}
test/run_tests.cpp+17
......@@ -1760,6 +1760,23 @@ const float_x = f32(1.0) / f32(0.0);
17601760 ".tmp_source.zig:3:25: error: division by zero is undefined",
17611761 ".tmp_source.zig:4:22: error: division by zero is undefined",
17621762 ".tmp_source.zig:5:26: error: division by zero is undefined");
1763
1764
1765 add_compile_fail_case("missing switch prong", R"SOURCE(
1766enum Number {
1767 One,
1768 Two,
1769 Three,
1770 Four,
1771}
1772fn f(n: Number) -> i32 {
1773 switch (n) {
1774 One => 1,
1775 Two => 2,
1776 Three => 3,
1777 }
1778}
1779 )SOURCE", 1, ".tmp_source.zig:9:5: error: enumeration value 'Four' not handled in switch");
17631780}
17641781
17651782//////////////////////////////////////////////////////////////////////////////