zig

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

commit 5b7ae86af488745fc93cdbaff11bf2438938274a (tree)
parent 517e8ea4269fb832f29d9eab642d57f1a8371149
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun, 21 Jan 2018 14:44:24 -0500

fix crash when switching on enum with 1 field and no switch prongs

closes #712

Diffstat:
Msrc/ir.cpp | 6+++++-
Mtest/compile_errors.zig | 9+++++++++
2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -4992,7 +4992,11 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode * ir_set_cursor_at_end_and_append_block(irb, end_block); assert(incoming_blocks.length == incoming_values.length); - return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items); + if (incoming_blocks.length == 0) { + return ir_build_const_void(irb, scope, node); + } else { + return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items); + } } static IrInstruction *ir_gen_comptime(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -1,6 +1,15 @@ const tests = @import("tests.zig"); pub fn addCases(cases: &tests.CompileErrorContext) { + cases.add("switch on enum with 1 field with no prongs", + \\const Foo = enum { M }; + \\ + \\export fn entry() { + \\ var f = Foo.M; + \\ switch (f) {} + \\} + , ".tmp_source.zig:5:5: error: enumeration value 'Foo.M' not handled in switch"); + cases.add("shift by negative comptime integer", \\comptime { \\ var a = 1 >> -1;