authorgravatar for red.black.liquorice@gmail.comHila Friedman <red.black.liquorice@gmail.com> 2026-03-12 13:38:06+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-12 13:38:06+01:00
log80d84537f7c7002c29a39a429072f59303f7d0f7
tree0dc0b474f1fdee7fc915ca15c8c2d019cbed7f49
parent58d33f51ceee33e595ceddc2734bddc1ce7af4a7

allow `packed union` comparisons in `std.meta.eql` (#31479)

#31464 implemented equality testing for `packed union`s - this PR updates `std.meta.eql`'s implementation to accept them. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31479 Co-authored-by: Hila Friedman <red.black.liquorice@gmail.com> Co-committed-by: Hila Friedman <red.black.liquorice@gmail.com>

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

lib/std/meta.zig+16-16
...@@ -636,22 +636,22 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {...@@ -636,22 +636,22 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
636 }636 }
637 },637 },
638 .@"union" => |info| {638 .@"union" => |info| {
639 if (info.tag_type) |UnionTag| {639 if (info.layout == .@"packed") return a == b;
640 const tag_a: UnionTag = a;640 const UnionTag = info.tag_type orelse
641 const tag_b: UnionTag = b;641 @compileError("cannot compare untagged union type " ++ @typeName(T));
642 if (tag_a != tag_b) return false;642
643643 const tag_a: UnionTag = a;
644 return switch (a) {644 const tag_b: UnionTag = b;
645 inline else => |val, tag| return eql(val, @field(b, @tagName(tag))),645 if (tag_a != tag_b) return false;
646 };
647 }
648646
649 @compileError("cannot compare untagged union type " ++ @typeName(T));647 return switch (a) {
648 inline else => |val, tag| return eql(val, @field(b, @tagName(tag))),
649 };
650 },650 },
651 .array => {651 .array => {
652 if (a.len != b.len) return false;652 for (a, b) |x, y| {
653 for (a, 0..) |e, i|653 if (!eql(x, y)) return false;
654 if (!eql(e, b[i])) return false;654 }
655 return true;655 return true;
656 },656 },
657 .vector => return @reduce(.And, a == b),657 .vector => return @reduce(.And, a == b),
...@@ -662,9 +662,9 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {...@@ -662,9 +662,9 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
662 };662 };
663 },663 },
664 .optional => {664 .optional => {
665 if (a == null and b == null) return true;665 const some_a = a orelse return b == null;
666 if (a == null or b == null) return false;666 const some_b = b orelse return false;
667 return eql(a.?, b.?);667 return eql(some_a, some_b);
668 },668 },
669 else => return a == b,669 else => return a == b,
670 }670 }