Sema: do not emit @errorCast safety check when dest is adhoc inferred error set

Closes #17354
This commit is contained in:
Veikka Tuominen 2024-01-29 11:57:29 +02:00
parent abb8e7478d
commit f782202910
2 changed files with 16 additions and 1 deletions

View File

@ -22368,7 +22368,10 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData
try sema.requireRuntimeBlock(block, src, operand_src);
const err_int_ty = try mod.errorIntType();
if (block.wantSafety() and !dest_ty.isAnyError(mod) and sema.mod.backendSupportsFeature(.error_set_has_value)) {
if (block.wantSafety() and !dest_ty.isAnyError(mod) and
dest_ty.toIntern() != .adhoc_inferred_error_set_type and
sema.mod.backendSupportsFeature(.error_set_has_value))
{
if (dest_tag == .ErrorUnion) {
const err_code = try sema.analyzeErrUnionCode(block, operand_src, operand);
const err_int = try block.addBitCast(err_int_ty, err_code);

View File

@ -1027,3 +1027,15 @@ test "generic type constructed from inferred error set of unresolved function" {
};
_ = std.io.multiWriter(.{S.writer()});
}
test "errorCast to adhoc inferred error set" {
const S = struct {
inline fn baz() !i32 {
return @errorCast(err());
}
fn err() anyerror!i32 {
return 1234;
}
};
try std.testing.expect((try S.baz()) == 1234);
}