Skip to content

Commit 5d28d17

Browse files
committed
Sema: adjust result type of anyerror field access
Closes #13448
1 parent ea48f06 commit 5d28d17

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Sema.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22475,7 +22475,10 @@ fn fieldVal(
2247522475
} else (try sema.mod.getErrorValue(field_name)).key;
2247622476

2247722477
return sema.addConstant(
22478-
try child_type.copy(arena),
22478+
if (!child_type.isAnyError())
22479+
try child_type.copy(arena)
22480+
else
22481+
try Type.Tag.error_set_single.create(arena, name),
2247922482
try Value.Tag.@"error".create(arena, .{ .name = name }),
2248022483
);
2248122484
},
@@ -22686,7 +22689,10 @@ fn fieldPtr(
2268622689
var anon_decl = try block.startAnonDecl(src);
2268722690
defer anon_decl.deinit();
2268822691
return sema.analyzeDeclRef(try anon_decl.finish(
22689-
try child_type.copy(anon_decl.arena()),
22692+
if (!child_type.isAnyError())
22693+
try child_type.copy(anon_decl.arena())
22694+
else
22695+
try Type.Tag.error_set_single.create(anon_decl.arena(), name),
2269022696
try Value.Tag.@"error".create(anon_decl.arena(), .{ .name = name }),
2269122697
0, // default alignment
2269222698
));

test/behavior/error.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,13 @@ test "error from comptime string" {
855855
try expect(mem.eql(u8, name, @errorName(err)));
856856
}
857857
}
858+
859+
test "field access of anyerror results in smaller error set" {
860+
if (builtin.zig_backend == .stage1) return error.SkipZigTest;
861+
862+
const E1 = @TypeOf(error.Foo);
863+
try expect(@TypeOf(E1.Foo) == E1);
864+
const E2 = error{ A, B, C };
865+
try expect(@TypeOf(E2.A) == E2);
866+
try expect(@TypeOf(@field(anyerror, "NotFound")) == error{NotFound});
867+
}

0 commit comments

Comments
 (0)