authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2026-05-19 19:18:43-04:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-09 10:09:42+02:00
log9195b36a6c55c2f2084d8a2edfd9a98c2e077e8c
tree3f4a740e7b9b6c7ac918ea24a3bbc6568369586f
parentb7b6d66675dc1eb20862b6a18ac9745287bc7e32

AstGen: move error capture asserts outside loop


6 files changed, 62 insertions(+), 64 deletions(-)

lib/std/zig/AstGen.zig+4-4
...@@ -7735,10 +7735,6 @@ fn switchExpr(...@@ -7735,10 +7735,6 @@ fn switchExpr(
7735 };7735 };
77367736
7737 if (capture != .none) assert(any_has_payload_capture);7737 if (capture != .none) assert(any_has_payload_capture);
7738 if (is_err_switch) {
7739 assert(!any_payload_is_ref); // should have failed by now
7740 assert(!any_has_tag_capture); // should have failed by now
7741 }
77427738
7743 prong_body: {7739 prong_body: {
7744 scratch_scope.instructions_top = parent_gz.instructions.items.len;7740 scratch_scope.instructions_top = parent_gz.instructions.items.len;
...@@ -7845,6 +7841,10 @@ fn switchExpr(...@@ -7845,6 +7841,10 @@ fn switchExpr(
7845 }7841 }
7846 }7842 }
7847 }7843 }
7844 if (is_err_switch) {
7845 assert(!any_payload_is_ref); // should have failed by now
7846 assert(!any_has_tag_capture); // should have failed by now
7847 }
7848 assert(scalar_case_index + multi_case_index + @intFromBool(has_else) == case_nodes.len);7848 assert(scalar_case_index + multi_case_index + @intFromBool(has_else) == case_nodes.len);
7849 assert(multi_items_infos_start + multi_item_offset == bodies_start);7849 assert(multi_items_infos_start + multi_item_offset == bodies_start);
78507850
test/cases/compile_errors/error_set_cannot_capture_by_reference.zig deleted-13
...@@ -1,13 +0,0 @@
1export fn entry() void {
2 const err: error{Foo} = error.Foo;
3
4 switch (err) {
5 error.Foo => |*foo| {
6 foo catch {};
7 },
8 }
9}
10
11// error
12//
13// :5:23: error: error set cannot be captured by reference
test/cases/compile_errors/switch_capture_error_by_ref.zig deleted-25
...@@ -1,25 +0,0 @@
1export fn entry1() void {
2 switch (@as(anyerror, error.MyError)) {
3 error.MyError, error.MyOtherError => |*err| _ = err,
4 else => {},
5 }
6}
7
8export fn entry2() void {
9 switch (@as(anyerror, error.MyError)) {
10 inline error.MyError, error.MyOtherError => |*err| _ = err,
11 else => {},
12 }
13}
14
15export fn entry3() void {
16 switch (@as(anyerror, error.MyError)) {
17 else => |*err| _ = err,
18 }
19}
20
21// error
22//
23// :3:47: error: error set cannot be captured by reference
24// :10:54: error: error set cannot be captured by reference
25// :17:18: error: error set cannot be captured by reference
test/cases/compile_errors/switch_on_error_bad_captures_astgen.zig created+37
...@@ -0,0 +1,37 @@
1const Error = error {OutOfMemory, ReadFailed};
2const result: Error!u64 = error.OutOfMemory;
3
4test {
5 _ = result catch |err| switch (err) {
6 error.ReadFailed => 2,
7 error.OutOfMemory => |*val| 1,
8 };
9}
10
11test {
12 _ = result catch |err| switch (err) {
13 error.ReadFailed => 2,
14 error.OutOfMemory => |_, tag| 1,
15 };
16}
17
18test {
19 _ = if (result) |n| n else |err| switch (err) {
20 error.ReadFailed => 2,
21 error.OutOfMemory => |*val| 1,
22 };
23}
24
25test {
26 _ = if (result) |n| n else |err| switch (err) {
27 error.ReadFailed => 2,
28 error.OutOfMemory => |_, tag| 1,
29 };
30}
31
32// error
33//
34// :7:32: error: error set cannot be captured by reference
35// :14:34: error: cannot capture tag of error union
36// :21:32: error: error set cannot be captured by reference
37// :28:34: error: cannot capture tag of error union
test/cases/compile_errors/switch_on_error_bad_captures_sema.zig created+21
...@@ -0,0 +1,21 @@
1const Error = error{ OutOfMemory, ReadFailed };
2const err: Error = error.OutOfMemory;
3
4export fn a() void {
5 _ = switch (err) {
6 error.ReadFailed => 2,
7 error.OutOfMemory => |*val| @intFromError(val.*),
8 };
9}
10
11export fn b() void {
12 _ = switch (err) {
13 error.ReadFailed => 2,
14 error.OutOfMemory => |_, tag| @intFromEnum(tag),
15 };
16}
17
18// error
19//
20// :7:31: error: error set cannot be captured by reference
21// :14:34: error: cannot capture tag of non-union type 'error{OutOfMemory,ReadFailed}'
test/cases/compile_errors/switch_on_error_with_capture_by_reference.zig deleted-22
...@@ -1,22 +0,0 @@
1comptime {
2 const e: error{Foo}!u32 = error.Foo;
3 e catch |err| switch (err) {
4 error.Foo => |*foo| {
5 foo catch {};
6 },
7 };
8}
9
10comptime {
11 const e: error{Foo}!u32 = error.Foo;
12 if (e) {} else |err| switch (err) {
13 error.Foo => |*foo| {
14 foo catch {};
15 },
16 }
17}
18
19// error
20//
21// :4:24: error: error set cannot be captured by reference
22// :13:24: error: error set cannot be captured by reference