authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-07 13:21:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-07 13:21:53-04:00
log11d8a8cc7b6984f2c79923a5e3f763003f2b558f
tree20151d02f9d0157abefa0e006dff282af5f64a1d
parent818a0a26291cf456cfaa955401b1aa8219737d6c

fix comptime switch on enum with ref payload

See #43

2 files changed, 22 insertions(+), 9 deletions(-)

src/ir.cpp+5-9
......@@ -11202,15 +11202,11 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
1120211202 return ira->codegen->builtin_types.entry_invalid;
1120311203
1120411204 ConstExprValue *pointee_val = const_ptr_pointee(ira->codegen, target_val_ptr);
11205 if (pointee_val->type->id == TypeTableEntryIdEnum) {
11206 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11207 out_val->data.x_ptr.special = ConstPtrSpecialRef;
11208 out_val->data.x_ptr.data.ref.pointee = pointee_val->data.x_enum.payload;
11209 return get_pointer_to_type(ira->codegen, pointee_val->type,
11210 target_value_ptr->value.type->data.pointer.is_const);
11211 } else {
11212 zig_panic("TODO comptime switch var");
11213 }
11205 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11206 out_val->data.x_ptr.special = ConstPtrSpecialRef;
11207 out_val->data.x_ptr.mut = target_val_ptr->data.x_ptr.mut;
11208 out_val->data.x_ptr.data.ref.pointee = pointee_val->data.x_enum.payload;
11209 return get_pointer_to_type(ira->codegen, field->type_entry, target_val_ptr->type->data.pointer.is_const);
1121411210 }
1121511211
1121611212 ir_build_enum_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);
test/cases/switch.zig+17
......@@ -106,6 +106,22 @@ fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) {
106106 }
107107}
108108
109test "switch on enum using pointer capture" {
110 testSwitchEnumPtrCapture();
111 comptime testSwitchEnumPtrCapture();
112}
113
114fn testSwitchEnumPtrCapture() {
115 var value = SwitchProngWithVarEnum.One { 1234 };
116 switch (value) {
117 SwitchProngWithVarEnum.One => |*x| *x += 1,
118 else => unreachable,
119 }
120 switch (value) {
121 SwitchProngWithVarEnum.One => |x| assert(x == 1235),
122 else => unreachable,
123 }
124}
109125
110126test "switch with multiple expressions" {
111127 const x = switch (returnsFive()) {
......@@ -188,3 +204,4 @@ fn testSwitchHandleAllCasesRange(x: u8) -> u8 {
188204 204 ... 255 => 3,
189205 }
190206}
207