stage2: @hasField for anon structs

This commit is contained in:
Mitchell Hashimoto 2022-03-11 09:26:36 -08:00 committed by Andrew Kelley
parent 86a98b172b
commit 6a9c9afbae
2 changed files with 9 additions and 0 deletions

View File

@ -7790,6 +7790,11 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
if (mem.eql(u8, field_name, "len")) break :hf true; if (mem.eql(u8, field_name, "len")) break :hf true;
break :hf false; break :hf false;
} }
if (ty.castTag(.anon_struct)) |pl| {
break :hf for (pl.data.names) |name| {
if (mem.eql(u8, name, field_name)) break true;
} else false;
}
break :hf switch (ty.zigTypeTag()) { break :hf switch (ty.zigTypeTag()) {
.Struct => ty.structFields().contains(field_name), .Struct => ty.structFields().contains(field_name),
.Union => ty.unionFields().contains(field_name), .Union => ty.unionFields().contains(field_name),

View File

@ -34,4 +34,8 @@ test "@hasField" {
try expect(@hasField(enm, "b") == true); try expect(@hasField(enm, "b") == true);
try expect(@hasField(enm, "non-existant") == false); try expect(@hasField(enm, "non-existant") == false);
try expect(@hasField(enm, "nope") == false); try expect(@hasField(enm, "nope") == false);
const anon = @TypeOf(.{ .a = 1 });
try expect(@hasField(anon, "a") == true);
try expect(@hasField(anon, "b") == false);
} }