| ... | @@ -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; |
| 763 | | 763 | |
| 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 | } |
| 771 | | 768 | |
| 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 { |
| 858 | | 855 | |
| 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 | } |
| 862 | | 868 | |
| 863 | test intToEnum { | 869 | test intToEnum { |