Sema: add @errorCast which works for both error sets and error unions

Closes #17343
This commit is contained in:
Veikka Tuominen
2023-10-01 13:16:02 +03:00
parent d8bfbbbf25
commit 63bd2bff12
14 changed files with 90 additions and 46 deletions
+18 -2
View File
@@ -228,13 +228,29 @@ const Set1 = error{ A, B };
const Set2 = error{ A, C };
fn testExplicitErrorSetCast(set1: Set1) !void {
var x = @as(Set2, @errSetCast(set1));
var x = @as(Set2, @errorCast(set1));
try expect(@TypeOf(x) == Set2);
var y = @as(Set1, @errSetCast(x));
var y = @as(Set1, @errorCast(x));
try expect(@TypeOf(y) == Set1);
try expect(y == error.A);
}
test "@errorCast on error unions" {
const S = struct {
fn doTheTest() !void {
const casted: error{Bad}!i32 = @errorCast(retErrUnion());
try expect((try casted) == 1234);
}
fn retErrUnion() anyerror!i32 {
return 1234;
}
};
try S.doTheTest();
try comptime S.doTheTest();
}
test "comptime test error for empty error set" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO