From e323cf1264f390911dcc2efea71d46be1d631d92 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 27 Aug 2022 18:48:01 +0300 Subject: stage2: change how defers are stored in Zir Storing defers this way has the benefits that the defer doesn't get analyzed multiple times in AstGen, it takes up less space, and it makes Sema aware of defers allowing for 'unreachable else prong' error on error sets in generic code. The disadvantage is that it is a bit more complex and errdefers with payloads now emit a placeholder instruction (but those are rare). Sema.zig before: Total ZIR bytes: 3.7794370651245117MiB Instructions: 238996 (2.051319122314453MiB) String Table Bytes: 89.2802734375KiB Extra Data Items: 430144 (1.640869140625MiB) Sema.zig after: Total ZIR bytes: 3.3344192504882812MiB Instructions: 211829 (1.8181428909301758MiB) String Table Bytes: 89.2802734375KiB Extra Data Items: 374611 (1.4290275573730469MiB) --- test/behavior/defer.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'test/behavior') diff --git a/test/behavior/defer.zig b/test/behavior/defer.zig index 70053ba01f..7bf66496a4 100644 --- a/test/behavior/defer.zig +++ b/test/behavior/defer.zig @@ -127,3 +127,20 @@ test "errdefer with payload" { try S.doTheTest(); comptime try S.doTheTest(); } + +test "simple else prong doesn't emit an error for unreachable else prong" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + + const S = struct { + fn foo() error{Foo}!void { + return error.Foo; + } + }; + var a: u32 = 0; + defer a += 1; + S.foo() catch |err| switch (err) { + error.Foo => a += 1, + else => |e| return e, + }; + try expect(a == 1); +} -- cgit v1.2.3