Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix std.testing.expectEqual, std.meta.eql for comptime-only unions #20343

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
rohlem committed Jun 18, 2024
commit 0ffeec4b4d3c0321cac618557e829571fa997881
22 changes: 14 additions & 8 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -757,16 +757,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
},
.Union => |info| {
if (info.tag_type) |UnionTag| {
const tag_a = activeTag(a);
const tag_b = activeTag(b);
const tag_a: UnionTag = a;
const tag_b: UnionTag = b;
if (tag_a != tag_b) return false;

inline for (info.fields) |field_info| {
if (@field(UnionTag, field_info.name) == tag_a) {
return eql(@field(a, field_info.name), @field(b, field_info.name));
}
}
return false;
return switch (a) {
inline else => |val, tag| return eql(val, @field(b, @tagName(tag))),
};
}

@compileError("cannot compare untagged union type " ++ @typeName(T));
Expand Down Expand Up @@ -858,6 +855,15 @@ test eql {

try testing.expect(eql(v1, v2));
try testing.expect(!eql(v1, v3));

const CU = union(enum) {
a: void,
b: void,
c: comptime_int,
};

try testing.expect(eql(CU{ .a = {} }, .a));
try testing.expect(!eql(CU{ .a = {} }, .b));
}

test intToEnum {
Expand Down
Loading