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

Misleading error message when keyword used as identifier #22401

Open
danielchasehooper opened this issue Jan 3, 2025 · 0 comments
Open

Misleading error message when keyword used as identifier #22401

danielchasehooper opened this issue Jan 3, 2025 · 0 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@danielchasehooper
Copy link
Contributor

danielchasehooper commented Jan 3, 2025

Zig Version

0.14.0-dev.2198+e5f5229fd

Steps to Reproduce and Observed Behavior

The compiler produces poor error messages in these situations:

echo 'const align = 16;' > temp.zig && zig run temp.zig
temp.zig:1:7: error: expected 'an identifier', found 'align'
const align = 16;
      ^~~~~
echo 'pub fn resume() u8 { return 1; }' > temp.zig && zig run temp.zig
temp.zig:1:8: error: expected '(', found 'resume'
pub fn resume() u8 { return 1; }
       ^~~~~~
$ echo 'pub fn main() !void { return error.test; }' > temp.zig && zig run temp.zig
temp.zig:1:32: error: expected 'an identifier', found 'test'
pub fn main() !void { return error.test; }
                                   ^~~~

Expected Behavior

I originally encountered this by trying to return error.test from a function - I thought the error message was trying to tell me that all error values had to have an uppercase first letter, since the documentation for errors uses uppercase names, and switching to error.Test fixed the error. But it's actually because test is a keyword!

It would be helpful if the compiler told you that the identifier you're trying to use is a keyword and you need to either rename it or use the @"" syntax.

I'd be happy to make this change if someone from the core team gave me the go ahead.

@danielchasehooper danielchasehooper added the bug Observed behavior contradicts documented or intended behavior label Jan 3, 2025
@danielchasehooper danielchasehooper changed the title Misleading error message for keywords used in place of identifier Misleading error message when keyword used as identifier Jan 3, 2025
@Vexu Vexu added error message This issue points out an error message that is unhelpful and should be improved. and removed bug Observed behavior contradicts documented or intended behavior labels Jan 3, 2025
@Vexu Vexu added this to the 0.15.0 milestone Jan 3, 2025
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

2 participants