diff --git a/src/ir.cpp b/src/ir.cpp index 8a041aa4ad..05427f75e8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8257,6 +8257,14 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr return ira->codegen->builtin_types.entry_invalid; TypeEnumField *field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint]; + if (prong_value->type_entry->id == TypeTableEntryIdEnumTag) { + field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint]; + } else if (prong_value->type_entry->id == TypeTableEntryIdEnum) { + field = &target_type->data.enumeration.fields[prong_val->data.x_enum.tag]; + } else { + zig_unreachable(); + } + if (instr_is_comptime(target_value_ptr)) { zig_panic("TODO comptime switch var"); } diff --git a/test/cases3/switch.zig b/test/cases3/switch.zig index c32a478b2e..8ae6735072 100644 --- a/test/cases3/switch.zig +++ b/test/cases3/switch.zig @@ -87,6 +87,33 @@ const SwitchStatmentFoo = enum { }; +fn switchProngWithVar() { + @setFnTest(this); + + switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); + switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); + switchProngWithVarFn(SwitchProngWithVarEnum.Meh); +} +const SwitchProngWithVarEnum = enum { + One: i32, + Two: f32, + Meh, +}; +fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { + switch(a) { + SwitchProngWithVarEnum.One => |x| { + if (x != 13) @unreachable(); + }, + SwitchProngWithVarEnum.Two => |x| { + if (x != 13.0) @unreachable(); + }, + SwitchProngWithVarEnum.Meh => |x| { + const v: void = x; + }, + } +} + + // TODO const assert = @import("std").debug.assert; diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 864d789c00..7aba790345 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -13,33 +13,6 @@ const test_enum_with_members = @import("cases/enum_with_members.zig"); const test_struct_contains_slice_of_itself = @import("cases/struct_contains_slice_of_itself.zig"); -fn switchProngWithVar() { - @setFnTest(this); - - switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); - switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); - switchProngWithVarFn(SwitchProngWithVarEnum.Meh); -} -const SwitchProngWithVarEnum = enum { - One: i32, - Two: f32, - Meh, -}; -fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { - switch(a) { - SwitchProngWithVarEnum.One => |x| { - if (x != 13) @unreachable(); - }, - SwitchProngWithVarEnum.Two => |x| { - if (x != 13.0) @unreachable(); - }, - SwitchProngWithVarEnum.Meh => |x| { - const v: void = x; - }, - } -} - - fn errReturnInAssignment() { @setFnTest(this, true);