Skip to content

Commit 599ca96

Browse files
committed
test: add tests for switch_block_err_union
1 parent 9f1dcc1 commit 599ca96

8 files changed

+935
-0
lines changed

test/behavior/switch_on_captured_error.zig

Lines changed: 750 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fn f(n: Error!i32) i32 {
2+
if (n) |x|
3+
_ = x
4+
else |e| switch (e) {
5+
error.Foo => 1,
6+
error.Bar => 2,
7+
error.Baz => 3,
8+
error.Foo => 2,
9+
}
10+
}
11+
fn g(n: Error!i32) i32 {
12+
n catch |e| switch (e) {
13+
error.Foo => 1,
14+
error.Bar => 2,
15+
error.Baz => 3,
16+
error.Foo => 2,
17+
};
18+
}
19+
20+
const Error = error{ Foo, Bar, Baz };
21+
22+
export fn entry() usize {
23+
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g));
24+
}
25+
26+
// error
27+
// backend=stage2
28+
// target=native
29+
//
30+
// :8:9: error: duplicate switch value
31+
// :5:9: note: previous value here
32+
// :16:9: error: duplicate switch value
33+
// :13:9: note: previous value here
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fn f(n: Error!i32) i32 {
2+
if (n) |x|
3+
_ = x
4+
else |e| switch (e) {
5+
error.Foo => 1,
6+
error.Bar => 2,
7+
error.Baz => 3,
8+
error.Foo => 2,
9+
else => 10,
10+
}
11+
}
12+
fn g(n: Error!i32) i32 {
13+
n catch |e| switch (e) {
14+
error.Foo => 1,
15+
error.Bar => 2,
16+
error.Baz => 3,
17+
error.Foo => 2,
18+
else => 10,
19+
};
20+
}
21+
22+
const Error = error{ Foo, Bar, Baz };
23+
24+
export fn entry() usize {
25+
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g));
26+
}
27+
28+
// error
29+
// backend=stage2
30+
// target=native
31+
//
32+
// :8:9: error: duplicate switch value
33+
// :5:9: note: previous value here
34+
// :17:9: error: duplicate switch value
35+
// :14:9: note: previous value here
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Error = error {
2+
One,
3+
Two,
4+
Three,
5+
Four,
6+
};
7+
fn f(n: Error!i32) i32 {
8+
if (n) |x| x else |e| switch (e) {
9+
error.One => 1,
10+
error.Two => 2,
11+
error.Three => 3,
12+
}
13+
}
14+
fn h(n: Error!i32) i32 {
15+
n catch |e| switch (e) {
16+
error.One => 1,
17+
error.Two => 2,
18+
error.Three => 3,
19+
};
20+
}
21+
22+
export fn entry() usize {
23+
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&h));
24+
}
25+
26+
// error
27+
// backend=stage2
28+
// target=native
29+
//
30+
// :8:27: error: switch must handle all possibilities
31+
// :8:27: note: unhandled error value: 'error.Four'
32+
// :15:17: error: switch must handle all possibilities
33+
// :15:17: note: unhandled error value: 'error.Four'

test/cases/compile_errors/switch_expression-multiple_else_prongs.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,24 @@ fn f(x: u32) void {
55
else => true,
66
};
77
}
8+
fn g(x: error{Foo, Bar, Baz}!u32) void {
9+
const value: bool = if (x) |_| true else |e| switch (e) {
10+
error.Foo => false,
11+
else => true,
12+
else => true,
13+
};
14+
}
15+
fn h(x: error{Foo, Bar, Baz}!u32) void {
16+
const value: u32 = x catch |e| switch (e) {
17+
error.Foo => 1,
18+
else => 2,
19+
else => 3,
20+
};
21+
}
822
export fn entry() void {
923
f(1234);
24+
g(1234);
25+
h(1234);
1026
}
1127

1228
// error
@@ -15,3 +31,7 @@ export fn entry() void {
1531
//
1632
// :5:9: error: multiple else prongs in switch expression
1733
// :4:9: note: previous else prong here
34+
// :12:9: error: multiple else prongs in switch expression
35+
// :11:9: note: previous else prong here
36+
// :19:9: error: multiple else prongs in switch expression
37+
// :18:9: note: previous else prong here
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
fn foo(x: u2) void {
2+
const y: Error!u2 = x;
3+
if (y) |_| {} else |e| switch (e) {
4+
error.Foo => {},
5+
error.Bar => {},
6+
error.Baz => {},
7+
else => {},
8+
}
9+
}
10+
11+
fn bar(x: u2) void {
12+
const y: Error!u2 = x;
13+
y catch |e| switch (e) {
14+
error.Foo => {},
15+
error.Bar => {},
16+
error.Baz => {},
17+
else => {},
18+
};
19+
}
20+
21+
const Error = error{ Foo, Bar, Baz };
22+
23+
export fn entry() usize {
24+
return @sizeOf(@TypeOf(&foo)) + @sizeOf(@TypeOf(&bar));
25+
}
26+
27+
// error
28+
// backend=stage2
29+
// target=native
30+
//
31+
// :7:14: error: unreachable else prong; all cases already handled
32+
// :17:14: error: unreachable else prong; all cases already handled
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export fn entry() void {
2+
const x: error{}!u32 = 0;
3+
if (x) |v| v else |_| switch (_) {
4+
}
5+
}
6+
7+
8+
// error
9+
// backend=stage2
10+
// target=native
11+
//
12+
// :3:24: error: discard of error capture; omit it instead
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Error = error{M};
2+
3+
export fn entry() void {
4+
const f: Error!void = void{};
5+
if (f) {} else |e| switch (e) {}
6+
}
7+
8+
export fn entry2() void {
9+
const f: Error!void = void{};
10+
f catch |e| switch (e) {};
11+
}
12+
13+
// error
14+
// backend=stage2
15+
// target=native
16+
//
17+
// :5:24: error: switch must handle all possibilities
18+
// :5:24: note: unhandled error value: 'error.M'
19+
// :10:17: error: switch must handle all possibilities
20+
// :10:17: note: unhandled error value: 'error.M'

0 commit comments

Comments
 (0)