| ... | ... | @@ -757,16 +757,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool { |
| 757 | 757 | }, |
| 758 | 758 | .Union => |info| { |
| 759 | 759 | if (info.tag_type) |UnionTag| { |
| 760 | | const tag_a = activeTag(a); |
| 761 | | const tag_b = activeTag(b); |
| 760 | const tag_a: UnionTag = a; |
| 761 | const tag_b: UnionTag = b; |
| 762 | 762 | if (tag_a != tag_b) return false; |
| 763 | 763 | |
| 764 | | inline for (info.fields) |field_info| { |
| 765 | | if (@field(UnionTag, field_info.name) == tag_a) { |
| 766 | | return eql(@field(a, field_info.name), @field(b, field_info.name)); |
| 767 | | } |
| 768 | | } |
| 769 | | return false; |
| 764 | return switch (a) { |
| 765 | inline else => |val, tag| return eql(val, @field(b, @tagName(tag))), |
| 766 | }; |
| 770 | 767 | } |
| 771 | 768 | |
| 772 | 769 | @compileError("cannot compare untagged union type " ++ @typeName(T)); |
| ... | ... | @@ -858,6 +855,15 @@ test eql { |
| 858 | 855 | |
| 859 | 856 | try testing.expect(eql(v1, v2)); |
| 860 | 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 | 869 | test intToEnum { |