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

better error when a keyword is used as identifier #236

Open
mimoo opened this issue Nov 20, 2024 · 2 comments
Open

better error when a keyword is used as identifier #236

mimoo opened this issue Nov 20, 2024 · 2 comments
Labels

Comments

@mimoo
Copy link
Contributor

mimoo commented Nov 20, 2024

imagine that we try to create a struct with a field called pub:

struct Thing {
  pub: Field
}

this won't work as pub is a reserved keyword:

Error:   × Looks like something went wrong in parser
   ╭─[tmp/src/main.no:3:1]
 3 │ struct Thing {
 4 │   pub: Field
   ·   ─┬─
   ·    ╰── here
 5 │ }
   ╰────
  help: invalid token, expected: a lowercase alphanumeric (including
        underscore) string starting with a letter

The problem here is that the error doesn't help us understand why pub doesn't work. It would be good to write what we got instead.

Perhaps this can be done everywhere by also including the obtained token in the error? So something like this:

_ => Err(ctx.error(
                ErrorKind::ExpectedToken(TokenKind::Identifier("".to_string())),
                token.span,
            )),

could become

_ => Err(ctx.error(
                ErrorKind::ExpectedToken(TokenKind::Identifier("".to_string()), TokenKind::Keyword(...)),
                token.span,
            )),

for example

@mimoo mimoo added the easy label Nov 20, 2024
@Lynette7
Copy link

Lynette7 commented Dec 1, 2024

Hello, I would love to contribute to this issue.

@zedar
Copy link

zedar commented Dec 3, 2024

Hello, please find my PR for this issue. @Lynette7 maybe you can base on my PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants