From 3e74acb1390e7b18a1d0febf9540585e165b52bd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Mar 2022 08:22:42 -0700 Subject: [PATCH 1/2] AstGen: labeled blocks should always complete with a normal break They aren't inline blocks by nature of being labeled. Fixes #11213 --- src/AstGen.zig | 2 +- test/behavior.zig | 1 + test/behavior/bugs/11213.zig | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/behavior/bugs/11213.zig diff --git a/src/AstGen.zig b/src/AstGen.zig index 85f2288413..88b2b3544c 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2018,7 +2018,7 @@ fn labeledBlockExpr( try blockExprStmts(&block_scope, &block_scope.base, statements); if (!block_scope.endsWithNoReturn()) { - _ = try block_scope.addBreak(.break_inline, block_inst, .void_value); + _ = try block_scope.addBreak(.@"break", block_inst, .void_value); } if (!block_scope.label.?.used) { diff --git a/test/behavior.zig b/test/behavior.zig index 03a02c3ea4..f6ff8bd64d 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -70,6 +70,7 @@ test { _ = @import("behavior/bugs/11165.zig"); _ = @import("behavior/bugs/11181.zig"); _ = @import("behavior/bugs/11182.zig"); + _ = @import("behavior/bugs/11213.zig"); _ = @import("behavior/call.zig"); _ = @import("behavior/cast.zig"); _ = @import("behavior/comptime_memory.zig"); diff --git a/test/behavior/bugs/11213.zig b/test/behavior/bugs/11213.zig new file mode 100644 index 0000000000..d4e3580c07 --- /dev/null +++ b/test/behavior/bugs/11213.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const testing = std.testing; + +test { + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + const g: error{Test}!void = error.Test; + + var v: u32 = 0; + hash(&v, g); + try testing.expect(v == 1); +} + +fn hash(v: *u32, key: anytype) void { + const Key = @TypeOf(key); + + if (@typeInfo(Key) == .ErrorSet) { + v.* += 1; + return; + } + + switch (@typeInfo(Key)) { + .ErrorUnion => blk: { + const payload = key catch |err| { + hash(v, err); + break :blk; + }; + + hash(v, payload); + }, + + else => unreachable, + } +} From de8f3bc885c7bfd1a084478c1302f0814d98bb62 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 18 Mar 2022 11:49:18 -0700 Subject: [PATCH 2/2] AstGen: remove unused parameter This function took a parameter that was only ever used with one value, obscuring the fact that it was a regular `block` which should be used with `.break` and not `.break_inline`. --- src/AstGen.zig | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 88b2b3544c..742f13214c 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -1939,7 +1939,7 @@ fn blockExpr( if (token_tags[lbrace - 1] == .colon and token_tags[lbrace - 2] == .identifier) { - return labeledBlockExpr(gz, scope, rl, block_node, statements, .block); + return labeledBlockExpr(gz, scope, rl, block_node, statements); } try blockExprStmts(gz, scope, statements); @@ -1984,13 +1984,10 @@ fn labeledBlockExpr( rl: ResultLoc, block_node: Ast.Node.Index, statements: []const Ast.Node.Index, - zir_tag: Zir.Inst.Tag, ) InnerError!Zir.Inst.Ref { const tracy = trace(@src()); defer tracy.end(); - assert(zir_tag == .block); - const astgen = gz.astgen; const tree = astgen.tree; const main_tokens = tree.nodes.items(.main_token); @@ -2004,7 +2001,7 @@ fn labeledBlockExpr( // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct // so that break statements can reference it. - const block_inst = try gz.makeBlockInst(zir_tag, block_node); + const block_inst = try gz.makeBlockInst(.block, block_node); try gz.instructions.append(astgen.gpa, block_inst); var block_scope = gz.makeSubBlock(parent_scope);