From 905abb99e72c6a667bd5c2c47b22e21f5e563dc8 Mon Sep 17 00:00:00 2001 From: dweiller <4678790+dweiller@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:32:59 +1100 Subject: [PATCH] test: add compile error cases for error union switch --- ...witch_expression-duplicate_error_prong.zig | 33 +++++++++++++++++ ...uplicate_error_prong_when_else_present.zig | 35 +++++++++++++++++++ .../switch_expression-missing_error_prong.zig | 33 +++++++++++++++++ ...switch_expression-multiple_else_prongs.zig | 20 +++++++++++ ...xpression-unreachable_else_prong_error.zig | 32 +++++++++++++++++ .../switch_on_error_union_discard.zig | 12 +++++++ ...h_on_error_with_1_field_with_no_prongs.zig | 20 +++++++++++ 7 files changed, 185 insertions(+) create mode 100644 test/cases/compile_errors/switch_expression-duplicate_error_prong.zig create mode 100644 test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig create mode 100644 test/cases/compile_errors/switch_expression-missing_error_prong.zig create mode 100644 test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig create mode 100644 test/cases/compile_errors/switch_on_error_union_discard.zig create mode 100644 test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig diff --git a/test/cases/compile_errors/switch_expression-duplicate_error_prong.zig b/test/cases/compile_errors/switch_expression-duplicate_error_prong.zig new file mode 100644 index 000000000000..bcebd25a5849 --- /dev/null +++ b/test/cases/compile_errors/switch_expression-duplicate_error_prong.zig @@ -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 diff --git a/test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig b/test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig new file mode 100644 index 000000000000..ca156183e056 --- /dev/null +++ b/test/cases/compile_errors/switch_expression-duplicate_error_prong_when_else_present.zig @@ -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 diff --git a/test/cases/compile_errors/switch_expression-missing_error_prong.zig b/test/cases/compile_errors/switch_expression-missing_error_prong.zig new file mode 100644 index 000000000000..ee28057c4362 --- /dev/null +++ b/test/cases/compile_errors/switch_expression-missing_error_prong.zig @@ -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' diff --git a/test/cases/compile_errors/switch_expression-multiple_else_prongs.zig b/test/cases/compile_errors/switch_expression-multiple_else_prongs.zig index f4cdb3b125c4..a6bb48db1740 100644 --- a/test/cases/compile_errors/switch_expression-multiple_else_prongs.zig +++ b/test/cases/compile_errors/switch_expression-multiple_else_prongs.zig @@ -5,8 +5,24 @@ fn f(x: u32) void { else => true, }; } +fn g(x: error{Foo, Bar, Baz}!u32) void { + const value: bool = if (x) |_| true else |e| switch (e) { + error.Foo => false, + else => true, + else => true, + }; +} +fn h(x: error{Foo, Bar, Baz}!u32) void { + const value: u32 = x catch |e| switch (e) { + error.Foo => 1, + else => 2, + else => 3, + }; +} export fn entry() void { f(1234); + g(1234); + h(1234); } // error @@ -15,3 +31,7 @@ export fn entry() void { // // :5:9: error: multiple else prongs in switch expression // :4:9: note: previous else prong here +// :12:9: error: multiple else prongs in switch expression +// :11:9: note: previous else prong here +// :19:9: error: multiple else prongs in switch expression +// :18:9: note: previous else prong here diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig new file mode 100644 index 000000000000..520723d9278a --- /dev/null +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_error.zig @@ -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 diff --git a/test/cases/compile_errors/switch_on_error_union_discard.zig b/test/cases/compile_errors/switch_on_error_union_discard.zig new file mode 100644 index 000000000000..34a607cfef2e --- /dev/null +++ b/test/cases/compile_errors/switch_on_error_union_discard.zig @@ -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 diff --git a/test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig b/test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig new file mode 100644 index 000000000000..504df0374f69 --- /dev/null +++ b/test/cases/compile_errors/switch_on_error_with_1_field_with_no_prongs.zig @@ -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'