Skip to content

"parameter type declared here" should additionally be provided for unions #22128

@andrewrk

Description

@andrewrk

Zig Version

0.14.0-dev.2370+5c6b25d9b

Steps to Reproduce and Observed Output

In this example we get desired output for enums:

const A = struct {
    x: Index,

    const Index = enum(u32) { none = 0, _ };

    fn init(x: Index) A {
        return .{ .x = x };
    }
};

const B = struct {
    x: Index,

    const Index = enum(u32) { none = 0, _ };

    fn init(x: Index) B {
        return .{ .x = x };
    }
};

test "example" {
    var a = mystery();
    a.field = .init(A.Index.none);
}

// If the below source is in a different file, one must rely on the compile
// error to tell which function is being called.

const C = struct {
    field: B,
};

fn mystery() C {
    return undefined;
}
test.zig:23:28: error: expected type 'test.B.Index', found 'test.A.Index'
    a.field = .init(A.Index.none);
                    ~~~~~~~^~~~~
test.zig:4:19: note: enum declared here
    const Index = enum(u32) { none = 0, _ };
                  ^~~~~~~~~~~~~~~~~~~~~~~~~
test.zig:14:19: note: enum declared here
    const Index = enum(u32) { none = 0, _ };
                  ^~~~~~~~~~~~~~~~~~~~~~~~~
test.zig:16:16: note: parameter type declared here
    fn init(x: Index) B {
               ^~~~~

✔️ Good

However, if you change both enums to unions, then we no longer have a note that tells which function is being called:

-    const Index = enum(u32) { none = 0, _ };
+    const Index = union(enum(u32)) { none = 0, _ };
test.zig:23:28: error: expected type '@typeInfo(test.B.Index).@"union".tag_type.?', found '@typeInfo(test.A.Index).@"union".tag_type.?'
    a.field = .init(A.Index.none);
                    ~~~~~~~^~~~~
test.zig:4:19: note: enum declared here
    const Index = union(enum(u32)) { none = 0, _ };
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.zig:14:19: note: enum declared here
    const Index = union(enum(u32)) { none = 0, _ };
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

❌ Missing function definition note

Expected Output

The additional error note that mentions the function definition source location:

test.zig:16:16: note: parameter type declared here
    fn init(x: Index) B {
               ^~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.error messageThis issue points out an error message that is unhelpful and should be improved.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions