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

Improve error message: No rules expected the token 'a' #49370

Closed
sunjay opened this issue Mar 26, 2018 · 0 comments
Closed

Improve error message: No rules expected the token 'a' #49370

sunjay opened this issue Mar 26, 2018 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sunjay
Copy link
Member

sunjay commented Mar 26, 2018

Let's say you are a beginner to Rust and you forget a comma while using println!. (This could happen to anyone, but let's specifically imagine that you do not know the language and its features well.)

fn main() {
    let a = 1;
    println!("{}" a);
}

Here is the error you would get:

error: no rules expected the token `a`
 --> src/main.rs:3:19
  |
3 |     println!("{}" a);
  |                   ^

error: aborting due to previous error

error: Could not compile `playground`.

Since you're a beginner, it is likely that macros are going to be one of the last features you touch. So "no rules expected the token" would not make any sense to you.

Now let's look at what happens if you forget a comma in a regular function:

fn main() {
    let a = 1;
    foo("{}" a);
}

fn foo(q: &str, x: i32) {
}

Here is the error that you get:

error: expected one of `,`, `.`, `?`, or an operator, found `a`
 --> src/main.rs:3:14
  |
3 |     foo("{}" a);
  |              ^ expected one of `,`, `.`, `?`, or an operator here

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

This is much better because it makes it clear that we forgot one of the options presented and it is pretty obvious how to fix it.

Since we know exactly which tokens a macro expects, wouldn't it be possible to generate a similar looking (or different but informative) message for macros too? I think this would really enhance the experience especially for beginners who may not understand "no rules expected the token" errors in macros.

Please pardon me if this is a duplicate. I reported this because when I encountered it, I was confused for a second. If I can get confused by this error message after 3 years of programming in Rust, a beginner is certainly likely to be stumped for much longer. It would be awesome if this could improve! 😄

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 27, 2018
bors added a commit that referenced this issue Aug 7, 2018
Suggest comma when writing `println!("{}" a);`

Fix #49370.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants