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

std.testing: improve compile error on untagged union equality #22852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mark-rushakoff
Copy link

With a simple test like this:

const MyStruct = struct {
    foo: u32,
    bar: union {
        a: u16,
        b: f32,
    },
};

const testing = @import("std").testing;
test {
    const x: MyStruct = .{
        .foo = 1,
        .bar = .{ .a = 9 },
    };
    const y: MyStruct = .{
        .foo = 2,
        .bar = .{ .b = 1.5 },
    };

    try testing.expectEqual(x, y);
}

Prior to this change, the compile error would look like:

$ zig test ./test.zig
/.../zig/lib/std/testing.zig:150:17: error: Unable to compare untagged union values
                @compileError("Unable to compare untagged union values");
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After this change, the compile error includes the name of the type containing the untagged union:

$ zig test ./test.zig
/.../zig/lib/std/testing.zig:150:17: error: Unable to compare untagged union values for type test.MyStruct__union_400
                @compileError("Unable to compare untagged union values for type " ++ @typeName(@TypeOf(actual)));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This particular compile error took me a little time to track down in a real world project; I would have saved a few minutes if the error message told me exactly what type to check.

I'm not sure if there is an existing pattern to add tests for compile errors in the standard library like this. I'm happy to add another commit with a test if anyone can point me at an example of the pattern to follow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant