Skip to content

Commit

Permalink
test: add compile error cases for error union switch
Browse files Browse the repository at this point in the history
  • Loading branch information
dweiller committed Dec 14, 2023
1 parent 3ff78d1 commit 905abb9
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
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
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
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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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 test/cases/compile_errors/switch_on_error_union_discard.zig
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
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'

0 comments on commit 905abb9

Please sign in to comment.