authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 08:05:37-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-22 08:05:37-05:00
log3e25ff65c37739fbe095f9cdd3458d5f7c42739d
tree0f368569dd7f7f5988578f26969da9b76f7be18a
parentdab3ddab4539b79b1dee4bc82f78c1b3f41ffeb2

IR: fix switch enum variable for void enum field


3 files changed, 35 insertions(+), 27 deletions(-)

src/ir.cpp+8
......@@ -8257,6 +8257,14 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
82578257 return ira->codegen->builtin_types.entry_invalid;
82588258
82598259 TypeEnumField *field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];
8260 if (prong_value->type_entry->id == TypeTableEntryIdEnumTag) {
8261 field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];
8262 } else if (prong_value->type_entry->id == TypeTableEntryIdEnum) {
8263 field = &target_type->data.enumeration.fields[prong_val->data.x_enum.tag];
8264 } else {
8265 zig_unreachable();
8266 }
8267
82608268 if (instr_is_comptime(target_value_ptr)) {
82618269 zig_panic("TODO comptime switch var");
82628270 }
test/cases3/switch.zig+27
......@@ -87,6 +87,33 @@ const SwitchStatmentFoo = enum {
8787};
8888
8989
90fn switchProngWithVar() {
91 @setFnTest(this);
92
93 switchProngWithVarFn(SwitchProngWithVarEnum.One {13});
94 switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0});
95 switchProngWithVarFn(SwitchProngWithVarEnum.Meh);
96}
97const SwitchProngWithVarEnum = enum {
98 One: i32,
99 Two: f32,
100 Meh,
101};
102fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
103 switch(a) {
104 SwitchProngWithVarEnum.One => |x| {
105 if (x != 13) @unreachable();
106 },
107 SwitchProngWithVarEnum.Two => |x| {
108 if (x != 13.0) @unreachable();
109 },
110 SwitchProngWithVarEnum.Meh => |x| {
111 const v: void = x;
112 },
113 }
114}
115
116
90117
91118
92119// TODO const assert = @import("std").debug.assert;
test/self_hosted.zig-27
......@@ -13,33 +13,6 @@ const test_enum_with_members = @import("cases/enum_with_members.zig");
1313const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig");
1414
1515
16fn switchProngWithVar() {
17 @setFnTest(this);
18
19 switchProngWithVarFn(SwitchProngWithVarEnum.One {13});
20 switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0});
21 switchProngWithVarFn(SwitchProngWithVarEnum.Meh);
22}
23const SwitchProngWithVarEnum = enum {
24 One: i32,
25 Two: f32,
26 Meh,
27};
28fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
29 switch(a) {
30 SwitchProngWithVarEnum.One => |x| {
31 if (x != 13) @unreachable();
32 },
33 SwitchProngWithVarEnum.Two => |x| {
34 if (x != 13.0) @unreachable();
35 },
36 SwitchProngWithVarEnum.Meh => |x| {
37 const v: void = x;
38 },
39 }
40}
41
42
4316fn errReturnInAssignment() {
4417 @setFnTest(this, true);
4518