zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 3e25ff65c37739fbe095f9cdd3458d5f7c42739d (tree)
parent dab3ddab4539b79b1dee4bc82f78c1b3f41ffeb2
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Thu, 22 Dec 2016 08:05:37 -0500

IR: fix switch enum variable for void enum field

Diffstat:
Msrc/ir.cpp | 8++++++++
Mtest/cases3/switch.zig | 27+++++++++++++++++++++++++++
Mtest/self_hosted.zig | 27---------------------------
3 files changed, 35 insertions(+), 27 deletions(-)

diff --git 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 @@ -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 @@ -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);