mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 03:03:09 +00:00
AstGen: update @errorCast to maybe eval to err
Consequently, `AstGen.ret()` now passes the error code to `.defer_error_code`. Previously, the error union value was passed. closes #20371
This commit is contained in:
parent
242d268a06
commit
7a4d69983a
@ -482,7 +482,7 @@ pub const list = list: {
|
|||||||
"@errorCast",
|
"@errorCast",
|
||||||
.{
|
.{
|
||||||
.tag = .error_cast,
|
.tag = .error_cast,
|
||||||
.eval_to_error = .always,
|
.eval_to_error = .maybe,
|
||||||
.param_count = 1,
|
.param_count = 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -197,3 +197,40 @@ const defer_assign = switch (block: {
|
|||||||
comptime {
|
comptime {
|
||||||
if (defer_assign != 0) @compileError("defer_assign failed!");
|
if (defer_assign != 0) @compileError("defer_assign failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "errdefer capture" {
|
||||||
|
const S = struct {
|
||||||
|
fail: bool = undefined,
|
||||||
|
fn bar0(self: *@This()) error{a}!void {
|
||||||
|
self.fail = false;
|
||||||
|
errdefer |err| if (@TypeOf(err) != error{a}) {
|
||||||
|
self.fail = true;
|
||||||
|
};
|
||||||
|
return error.a;
|
||||||
|
}
|
||||||
|
fn bar1(self: *@This()) error{a}!void {
|
||||||
|
self.fail = false;
|
||||||
|
errdefer |err| if (@TypeOf(err) != error{a}) {
|
||||||
|
self.fail = true;
|
||||||
|
};
|
||||||
|
const rv: error{a}!void = @errorCast(@as(error{a}!void, error.a));
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
// https://github.com/ziglang/zig/issues/20371
|
||||||
|
fn bar2(self: *@This()) error{a}!void {
|
||||||
|
self.fail = false;
|
||||||
|
errdefer |err| if (@TypeOf(err) != error{a}) {
|
||||||
|
self.fail = true;
|
||||||
|
};
|
||||||
|
return @errorCast(@as(error{a}!void, error.a));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var s: S = .{};
|
||||||
|
s.bar0() catch {};
|
||||||
|
if (s.fail) return error.TestExpectedError;
|
||||||
|
s.bar1() catch {};
|
||||||
|
if (s.fail) return error.TestExpectedError;
|
||||||
|
s.bar2() catch {};
|
||||||
|
if (s.fail) return error.TestExpectedError;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user