Skip to content

Peer type resolution in expectEqual broke functionality #19449

Open
@ikskuh

Description

@ikskuh

Zig Version

zig-0.12.0-dev.3438+5c628312b

Steps to Reproduce and Observed Behavior

const std = @import("std");
test {
    const E = enum { a, b, c };
    const U = union(E) { a: u32, b: u32, c: u32 };

    const u: U = .{ .a = 10 };
    try std.testing.expectEqual(E.a, u);
}

yields

lib/std/testing.zig:58:32: error: coercion from enum 'test.test_0.E' to union 'test.test_0.U' must initialize 'u32' field 'a'
    return expectEqualInner(T, expected, actual);
                               ^~~~~~~~
/tmp/test.zig:4:26: note: field 'a' declared here
    const U = union(E) { a: u32, b: u32, c: u32 };
                         ^~~~~~
/tmp/test.zig:4:15: note: union declared here
    const U = union(E) { a: u32, b: u32, c: u32 };
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/test.zig:7:32: note: called from here
    try std.testing.expectEqual(E.a, u);
        ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

Expected Behavior

The test must fail, but compile.

The problem was introduced in e5994f5 where expectEqual was changed to:

pub inline fn expectEqual(expected: anytype, actual: anytype) !void {
    const T = @TypeOf(expected, actual);
    return expectEqualInner(T, expected, actual);
}

which now breaks a bunch of code that checked for expected return types and such:

try std.testing.expectEqual(@as(u8, 10), @as(i32, 10)); // won't compile before, does pass now

as well as the use case with the enum tag checking, which is now broken by the peer-type resolved type checking

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions