Skip to content

Commit

Permalink
stage2: error set switch validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g-w1 committed Nov 25, 2020
1 parent 4a67c29 commit 2c3cfdd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/zir_sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
return mod.fail(scope, item.src, "expected type '{}', found '{}'", .{ gotten_err_set.err_single, gotten_err_set.err_single });
} else if (gotten_err_set == .multiple) { // we know it is an actual error set
if (gotten_err_set.multiple.get(err_name)) |_| {} else {
return mod.fail(scope, item.src, "'{}' not a member of destination error set {}", .{ err_name, gotten_err_set });
return mod.fail(scope, item.src, "'{}' not a member of destination error set", .{err_name});
}
}
}
Expand Down
81 changes: 75 additions & 6 deletions test/stage2/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,86 @@ pub fn addCases(ctx: *TestContext) !void {
"Hello, World!\n",
);
}

{
var case = ctx.exe("switch and coerce error sets", linux_x64);
case.addCompareOutput(
\\export fn _start() noreturn {
\\ const T = error{ A, B, C, D };
\\ const e = T.B;
\\
\\ switch (e) {
\\ error.B => condPrint(),
\\ else => unreachable,
\\ }
\\ exit();
\\}
\\
\\fn condPrint() void {
\\ asm volatile ("syscall"
\\ :
\\ : [number] "{rax}" (1),
\\ [arg1] "{rdi}" (1),
\\ [arg2] "{rsi}" (@ptrToInt("Reached\n")),
\\ [arg3] "{rdx}" (21)
\\ : "rcx", "r11", "memory"
\\ );
\\ return;
\\}
\\
\\fn exit() noreturn {
\\ asm volatile ("syscall"
\\ :
\\ : [number] "{rax}" (231),
\\ [arg1] "{rdi}" (0)
\\ : "rcx", "r11", "memory"
\\ );
\\ unreachable;
\\}
, "Reached\n");
case.addError(
\\export fn _start() noreturn {
\\ const T = error{ A, B, C, D };
\\ const e: T = T.B;
\\
\\ switch (e) {
\\ error.Z => {},
\\ }
\\ unreachable; // because it will give error above
\\}
, &[_][]const u8{":8:12: error: 'error.Z' not a member of destination error set"});
case.addError(
\\export fn _start() noreturn {
\\ const T = error{ A, B, C, D };
\\ const e: T = T.B;
\\
\\ switch (e) {
\\ error.B => {},
\\ }
\\ unreachable; // because it will give error above
\\}
, &[_][]const u8{":7:6: error: switch must handle all possibilities"});
case.addError(
\\export fn _start() noreturn {
\\ const T = error{ A, B, C, D };
\\ const e: anyerror = T.B;
\\
\\ switch (e) {
\\ error.B => {},
\\ }
\\ unreachable; // because it will give error above
\\}
, &[_][]const u8{":7:6: error: else prong required when switching on type 'anyerror'"});
}
{
var case = ctx.exe("merge error sets", linux_x64);
case.addCompareOutput(
\\export fn _start() noreturn {
\\ const b = error{ A, B, D } || error { A, B, C } == error { A, B, C, D };
\\
\\ if (b) {
\\ condPrint();
\\ }
\\
\\
\\ exit();
\\}
\\
\\fn condPrint() void {
\\ asm volatile ("syscall"
\\ :
Expand All @@ -342,7 +407,6 @@ pub fn addCases(ctx: *TestContext) !void {
\\ );
\\ return;
\\}
\\
\\fn exit() noreturn {
\\ asm volatile ("syscall"
\\ :
Expand All @@ -359,10 +423,15 @@ pub fn addCases(ctx: *TestContext) !void {
\\export fn _start() noreturn {
\\ const T: type = error{ A, B, D } || error { A, B, C };
\\ const x: T = error.D;
\\ assert(T == error { A, B, C, D });
\\
\\ exit();
\\}
\\
\\pub fn assert(ok: bool) void {
\\ if (!ok) unreachable; // assertion failure
\\}
\\
\\fn condPrint() void {
\\ asm volatile ("syscall"
\\ :
Expand Down

0 comments on commit 2c3cfdd

Please sign in to comment.