authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-29 11:57:29+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-29 13:48:45+02:00
logf7822029100de9f91c9a7758ffb32d95f7768f70
tree551ee2c7a6600facd02e27c62650483f89ba0986
parentabb8e7478d365088301a9390c12f86b2ad381ce9

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

Closes #17354

2 files changed, 16 insertions(+), 1 deletions(-)

src/Sema.zig+4-1
......@@ -22368,7 +22368,10 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData
2236822368
2236922369 try sema.requireRuntimeBlock(block, src, operand_src);
2237022370 const err_int_ty = try mod.errorIntType();
22371 if (block.wantSafety() and !dest_ty.isAnyError(mod) and sema.mod.backendSupportsFeature(.error_set_has_value)) {
22371 if (block.wantSafety() and !dest_ty.isAnyError(mod) and
22372 dest_ty.toIntern() != .adhoc_inferred_error_set_type and
22373 sema.mod.backendSupportsFeature(.error_set_has_value))
22374 {
2237222375 if (dest_tag == .ErrorUnion) {
2237322376 const err_code = try sema.analyzeErrUnionCode(block, operand_src, operand);
2237422377 const err_int = try block.addBitCast(err_int_ty, err_code);
test/behavior/error.zig+12
......@@ -1027,3 +1027,15 @@ test "generic type constructed from inferred error set of unresolved function" {
10271027 };
10281028 _ = std.io.multiWriter(.{S.writer()});
10291029}
1030
1031test "errorCast to adhoc inferred error set" {
1032 const S = struct {
1033 inline fn baz() !i32 {
1034 return @errorCast(err());
1035 }
1036 fn err() anyerror!i32 {
1037 return 1234;
1038 }
1039 };
1040 try std.testing.expect((try S.baz()) == 1234);
1041}