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

Largely insufficient diagnostics with undefined decls in generics #20294

Open
vadim-za opened this issue Jun 14, 2024 · 0 comments
Open

Largely insufficient diagnostics with undefined decls in generics #20294

vadim-za opened this issue Jun 14, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@vadim-za
Copy link

Zig Version

0.13.0

Steps to Reproduce and Observed Output

Compile the following code. Use -freference-trace is you like - it doesn't seem to change the results.

const std = @import("std");

const ContainerA = struct {
    //const Item = struct {};
};

fn View(comptime Container: type) type {
    //if (comptime !@hasDecl(Container, "Item"))
    //    @compileError("Container must define Item");
    return struct {
        item: ?*Container.Item = null,
    };
}

fn ViewHost(comptime Container: type) type {
    return struct {
        view: View(Container) = .{},
    };
}

pub fn main() void {
    _ = ViewHost(ContainerA){};
}

The diagnostics says:

src\main.zig:11:26: error: struct 'main.ContainerA' has no member named 'Item'
        item: ?*Container.Item = null,
                ~~~~~~~~~^~~~~
src\main.zig:3:20: note: struct declared here
const ContainerA = struct {
                   ^~~~~~

It is absolutely unclear what caused ContainerA to be used as a parameter to View(). In a large or even medium codebase it might be impossible to find (unless the statement is literally View(ContainerA), which is not necessarily the case).

Now we can attempt to improve the diagnostics by uncommenting the two lines in the View function and compile again. The diagnostics now says

src\main.zig:9:9: error: Container must define Item
        @compileError("Container must define Item");
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\main.zig:17:19: note: called from here
        view: View(Container) = .{},
              ~~~~^~~~~~~~~~~

We are one step further, but still have no information on where ViewHost(ContainerA) was instantiated.

Expected Output

The error message's reference trace is expected to go all the way to the "root cause" position, which in both cases is the line in the main function.

@vadim-za vadim-za added the error message This issue points out an error message that is unhelpful and should be improved. label Jun 14, 2024
@Vexu Vexu added this to the 0.15.0 milestone Jun 15, 2024
@Vexu Vexu added the bug Observed behavior contradicts documented or intended behavior label Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

2 participants