authorgravatar for rohlemF@gmail.comrohlem <rohlemF@gmail.com> 2024-06-18 19:04:16+02:00
committergravatar for rohlemF@gmail.comrohlem <rohlemF@gmail.com> 2024-06-18 19:04:16+02:00
log0ffeec4b4d3c0321cac618557e829571fa997881
tree4c10fee4af96038ac3325f1817dc73847cc92bf6
parent17ce3e5a17e617bd83d1cf2f5f464fb098b92ec7

fix std.meta.eql for comptime-only union

switch from `inline for` with `std.mem.eql` to `inline else` and tag comparison. add previously-failing test code.

1 files changed, 14 insertions(+), 8 deletions(-)

lib/std/meta.zig+14-8
...@@ -757,16 +757,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {...@@ -757,16 +757,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
757 },757 },
758 .Union => |info| {758 .Union => |info| {
759 if (info.tag_type) |UnionTag| {759 if (info.tag_type) |UnionTag| {
760 const tag_a = activeTag(a);760 const tag_a: UnionTag = a;
761 const tag_b = activeTag(b);761 const tag_b: UnionTag = b;
762 if (tag_a != tag_b) return false;762 if (tag_a != tag_b) return false;
763763
764 inline for (info.fields) |field_info| {764 return switch (a) {
765 if (@field(UnionTag, field_info.name) == tag_a) {765 inline else => |val, tag| return eql(val, @field(b, @tagName(tag))),
766 return eql(@field(a, field_info.name), @field(b, field_info.name));766 };
767 }
768 }
769 return false;
770 }767 }
771768
772 @compileError("cannot compare untagged union type " ++ @typeName(T));769 @compileError("cannot compare untagged union type " ++ @typeName(T));
...@@ -858,6 +855,15 @@ test eql {...@@ -858,6 +855,15 @@ test eql {
858855
859 try testing.expect(eql(v1, v2));856 try testing.expect(eql(v1, v2));
860 try testing.expect(!eql(v1, v3));857 try testing.expect(!eql(v1, v3));
858
859 const CU = union(enum) {
860 a: void,
861 b: void,
862 c: comptime_int,
863 };
864
865 try testing.expect(eql(CU{ .a = {} }, .a));
866 try testing.expect(!eql(CU{ .a = {} }, .b));
861}867}
862868
863test intToEnum {869test intToEnum {