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

ZON unexpected field error underscors wrong code #22942

Open
AndrewKraevskii opened this issue Feb 19, 2025 · 3 comments
Open

ZON unexpected field error underscors wrong code #22942

AndrewKraevskii opened this issue Feb 19, 2025 · 3 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.

Comments

@AndrewKraevskii
Copy link
Contributor

Zig Version

0.14.0-dev.3267+59dc15fa0

Steps to Reproduce and Observed Output

// zon.zon
.{
    .baz = "baz",
}
// main.zig
comptime {
    _ = @as(struct { bar: []const u8 }, @import("zon.zon"));
}
> zig test main.zig
zon.zon:2:12: error: unexpected field 'baz'
    .baz = "baz",
           ^~~~~
main.zig:2:49: note: imported here
    _ = @as(struct { bar: []const u8 }, @import("zon.zon"));

Expected Output

I expect to get this error

> zig test main.zig
zon.zon:2:5: error: unexpected field 'baz'
    .baz = "baz",
    ^~~~
main.zig:2:49: note: imported here
    _ = @as(struct { bar: []const u8 }, @import("zon.zon"));
@AndrewKraevskii AndrewKraevskii added the error message This issue points out an error message that is unhelpful and should be improved. label Feb 19, 2025
@alexrp
Copy link
Member

alexrp commented Feb 20, 2025

cc @MasonRemaley

@MasonRemaley
Copy link
Contributor

Thanks for the report!

@mlugg I started to fix this to underline the right part of the source, but then I noticed that you've managed to get things set up nicely where LowerZon doesn't directly access the AST anymore.

Would it cause any problems for me to cause getTree on error in order to determine the correct error location here?

@mlugg
Copy link
Member

mlugg commented Feb 22, 2025

@MasonRemaley Rather than using getTree... situations like this are exactly what LazySrcLoc is made to deal with! The idea is that you create a LazySrcLoc which represents the location you're trying to actually point to, without actually doing the work of computing it. That work is deferred for later.

In this case, what you want is a LazySrcLoc with .offset = .{ .node_offset_field_name_init = the_init }. That field of Offset takes the source node of the RHS of = in a struct initializer, and turns it into the span of the token to the left of = -- that is, the field name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants