mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 00:52:52 +00:00
Sema: make @hasField
support tuples too
This commit is contained in:
parent
6a9c9afbae
commit
a2517117e7
@ -7795,6 +7795,10 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
|
||||
if (mem.eql(u8, name, field_name)) break true;
|
||||
} else false;
|
||||
}
|
||||
if (ty.isTuple()) {
|
||||
const field_index = std.fmt.parseUnsigned(u32, field_name, 10) catch break :hf false;
|
||||
break :hf field_index < ty.structFieldCount();
|
||||
}
|
||||
break :hf switch (ty.zigTypeTag()) {
|
||||
.Struct => ty.structFields().contains(field_name),
|
||||
.Union => ty.unionFields().contains(field_name),
|
||||
|
@ -38,4 +38,12 @@ test "@hasField" {
|
||||
const anon = @TypeOf(.{ .a = 1 });
|
||||
try expect(@hasField(anon, "a") == true);
|
||||
try expect(@hasField(anon, "b") == false);
|
||||
|
||||
const tuple = @TypeOf(.{ 1, 2 });
|
||||
try expect(@hasField(tuple, "a") == false);
|
||||
try expect(@hasField(tuple, "b") == false);
|
||||
try expect(@hasField(tuple, "0") == true);
|
||||
try expect(@hasField(tuple, "1") == true);
|
||||
try expect(@hasField(tuple, "2") == false);
|
||||
try expect(@hasField(tuple, "9999999999999999999999999") == false);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user