mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
Sema: add @errorCast which works for both error sets and error unions
Closes #17343
This commit is contained in:
+18
-2
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user