authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-31 12:43:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-31 12:43:21-04:00
log25ac2fe8cd817977f3d5a8677cbe067ad2684d66
tree4282bf4c22820ce357f68e06472bdc405ce5e04b
parent62af7018042953f00d307508b448d42aa9d3bc65
signaturelock-open Commit is signed but in an unrecognized format.

fix anon enum literal used with switch on union(enum)

closes #2141 closes #2142

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

src/ir.cpp+9-2
...@@ -17188,11 +17188,18 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru...@@ -17188,11 +17188,18 @@ static IrInstruction *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstru
17188 assert(target_value_ptr->value.type->id == ZigTypeIdPointer);17188 assert(target_value_ptr->value.type->id == ZigTypeIdPointer);
17189 ZigType *target_type = target_value_ptr->value.type->data.pointer.child_type;17189 ZigType *target_type = target_value_ptr->value.type->data.pointer.child_type;
17190 if (target_type->id == ZigTypeIdUnion) {17190 if (target_type->id == ZigTypeIdUnion) {
17191 ConstExprValue *prong_val = ir_resolve_const(ira, prong_value, UndefBad);17191 ZigType *enum_type = target_type->data.unionation.tag_type;
17192 assert(enum_type != nullptr);
17193 assert(enum_type->id == ZigTypeIdEnum);
17194
17195 IrInstruction *casted_prong_value = ir_implicit_cast(ira, prong_value, enum_type);
17196 if (type_is_invalid(casted_prong_value->value.type))
17197 return ira->codegen->invalid_instruction;
17198
17199 ConstExprValue *prong_val = ir_resolve_const(ira, casted_prong_value, UndefBad);
17192 if (!prong_val)17200 if (!prong_val)
17193 return ira->codegen->invalid_instruction;17201 return ira->codegen->invalid_instruction;
1719417202
17195 assert(prong_value->value.type->id == ZigTypeIdEnum);
17196 TypeUnionField *field = find_union_field_by_tag(target_type, &prong_val->data.x_enum_tag);17203 TypeUnionField *field = find_union_field_by_tag(target_type, &prong_val->data.x_enum_tag);
1719717204
17198 if (instr_is_comptime(target_value_ptr)) {17205 if (instr_is_comptime(target_value_ptr)) {
test/stage1/behavior/switch.zig+13
...@@ -283,3 +283,16 @@ test "undefined.u0" {...@@ -283,3 +283,16 @@ test "undefined.u0" {
283 0 => expect(val == 0),283 0 => expect(val == 0),
284 }284 }
285}285}
286
287test "anon enum literal used in switch on union enum" {
288 const Foo = union(enum) {
289 a: i32,
290 };
291
292 var foo = Foo{ .a = 1234 };
293 switch (foo) {
294 .a => |x| {
295 expect(x == 1234);
296 },
297 }
298}