-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add compile error cases for error union switch
- Loading branch information
Showing
7 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
test/cases/compile_errors/switch_expression-duplicate_error_prong.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
fn f(n: Error!i32) i32 { | ||
if (n) |x| | ||
_ = x | ||
else |e| switch (e) { | ||
error.Foo => 1, | ||
error.Bar => 2, | ||
error.Baz => 3, | ||
error.Foo => 2, | ||
} | ||
} | ||
fn g(n: Error!i32) i32 { | ||
n catch |e| switch (e) { | ||
error.Foo => 1, | ||
error.Bar => 2, | ||
error.Baz => 3, | ||
error.Foo => 2, | ||
}; | ||
} | ||
|
||
const Error = error{ Foo, Bar, Baz }; | ||
|
||
export fn entry() usize { | ||
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g)); | ||
} | ||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :8:9: error: duplicate switch value | ||
// :5:9: note: previous value here | ||
// :16:9: error: duplicate switch value | ||
// :13:9: note: previous value here |
35 changes: 35 additions & 0 deletions
35
test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
fn f(n: Error!i32) i32 { | ||
if (n) |x| | ||
_ = x | ||
else |e| switch (e) { | ||
error.Foo => 1, | ||
error.Bar => 2, | ||
error.Baz => 3, | ||
error.Foo => 2, | ||
else => 10, | ||
} | ||
} | ||
fn g(n: Error!i32) i32 { | ||
n catch |e| switch (e) { | ||
error.Foo => 1, | ||
error.Bar => 2, | ||
error.Baz => 3, | ||
error.Foo => 2, | ||
else => 10, | ||
}; | ||
} | ||
|
||
const Error = error{ Foo, Bar, Baz }; | ||
|
||
export fn entry() usize { | ||
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&g)); | ||
} | ||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :8:9: error: duplicate switch value | ||
// :5:9: note: previous value here | ||
// :17:9: error: duplicate switch value | ||
// :14:9: note: previous value here |
33 changes: 33 additions & 0 deletions
33
test/cases/compile_errors/switch_expression-missing_error_prong.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const Error = error { | ||
One, | ||
Two, | ||
Three, | ||
Four, | ||
}; | ||
fn f(n: Error!i32) i32 { | ||
if (n) |x| x else |e| switch (e) { | ||
error.One => 1, | ||
error.Two => 2, | ||
error.Three => 3, | ||
} | ||
} | ||
fn h(n: Error!i32) i32 { | ||
n catch |e| switch (e) { | ||
error.One => 1, | ||
error.Two => 2, | ||
error.Three => 3, | ||
}; | ||
} | ||
|
||
export fn entry() usize { | ||
return @sizeOf(@TypeOf(&f)) + @sizeOf(@TypeOf(&h)); | ||
} | ||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :8:27: error: switch must handle all possibilities | ||
// :8:27: note: unhandled error value: 'error.Four' | ||
// :15:17: error: switch must handle all possibilities | ||
// :15:17: note: unhandled error value: 'error.Four' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
fn foo(x: u2) void { | ||
const y: Error!u2 = x; | ||
if (y) |_| {} else |e| switch (e) { | ||
error.Foo => {}, | ||
error.Bar => {}, | ||
error.Baz => {}, | ||
else => {}, | ||
} | ||
} | ||
|
||
fn bar(x: u2) void { | ||
const y: Error!u2 = x; | ||
y catch |e| switch (e) { | ||
error.Foo => {}, | ||
error.Bar => {}, | ||
error.Baz => {}, | ||
else => {}, | ||
}; | ||
} | ||
|
||
const Error = error{ Foo, Bar, Baz }; | ||
|
||
export fn entry() usize { | ||
return @sizeOf(@TypeOf(&foo)) + @sizeOf(@TypeOf(&bar)); | ||
} | ||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :7:14: error: unreachable else prong; all cases already handled | ||
// :17:14: error: unreachable else prong; all cases already handled |
12 changes: 12 additions & 0 deletions
12
test/cases/compile_errors/switch_on_error_union_discard.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export fn entry() void { | ||
const x: error{}!u32 = 0; | ||
if (x) |v| v else |_| switch (_) { | ||
} | ||
} | ||
|
||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :3:24: error: discard of error capture; omit it instead |
20 changes: 20 additions & 0 deletions
20
test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const Error = error{M}; | ||
|
||
export fn entry() void { | ||
const f: Error!void = void{}; | ||
if (f) {} else |e| switch (e) {} | ||
} | ||
|
||
export fn entry2() void { | ||
const f: Error!void = void{}; | ||
f catch |e| switch (e) {}; | ||
} | ||
|
||
// error | ||
// backend=stage2 | ||
// target=native | ||
// | ||
// :5:24: error: switch must handle all possibilities | ||
// :5:24: note: unhandled error value: 'error.M' | ||
// :10:17: error: switch must handle all possibilities | ||
// :10:17: note: unhandled error value: 'error.M' |