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 for incorrect 'for' input #40782

Closed
martinlindhe opened this issue Mar 24, 2017 · 3 comments
Closed

Improve error message for incorrect 'for' input #40782

martinlindhe opened this issue Mar 24, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics

Comments

@martinlindhe
Copy link
Contributor

martinlindhe commented Mar 24, 2017

Hello, with rustc 1.17.0-nightly (8c4f2c6 2017-03-22), the following code

fn main () {
    for i 0..2 {
        println!("{}", i);
    }
}

gives this error message:

error: expected one of `@` or `in`, found `0`
  --> src/disasm/mod.rs:27:15
   |
27 |         for i 0..2 {
   | 

An improved error message could add a hint : did you mean 'for i in 0..2'

@sanxiyn sanxiyn added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error: expected one of `@` or `in`, found `0`
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          -^ unexpected token
  |          |
  |          expected one of `@` or `in` here

How would you want to improve upon this? Adding a suggestion?

error: expected one of `@` or `in`, found `0`
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          -^ unexpected token
  |          |
  |          expected one of `@` or `in` here
  = note: did you mean to write `in` here?
2 |     for i in 0..2 {
  |           ^^

To do so, we could use the same strategy as in #42578: in parse_for_expr if the In keyword is not found, try to continue building the expression as if the In keyword had been found. If we find further errors, we revert the state of the compiler to right after trying to continue and return the error from expect_keyword. Otherwise we cancel that one and create a new custom diagnostic:

error: missing `in` in `for` loop
 --> src/main.rs:2:11
  |
2 |     for i 0..2 {
  |          ^ expected `in` here
  = note: did you mean to write `in` here?
2 |     for i in 0..2 {
  |           ^^

or just modify the existing error with the proposed span_suggestion (so it looks like the 2 above).

@estebank estebank added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Sep 21, 2017
@martinlindhe
Copy link
Contributor Author

Looks like current output already is an improvement over the reported one, but eithwr 2 or 3 would make it even more obvious i guess. No strong opinions here

@estebank estebank added the WG-diagnostics Working group: Diagnostics label Sep 22, 2017
@nikomatsakis
Copy link
Contributor

Seems to me that the third error is the most clear ("error: missing in in for loop"); I suspect people using fancy pattern features will figure out what's going on.

kennytm added a commit to kennytm/rust that referenced this issue Nov 4, 2017
Add a nicer error message for missing  in for loop, fixes rust-lang#40782.

As suggested by @estebank in issue rust-lang#40782, this works in the same way as rust-lang#42578: if the in keyword is missing, we continue parsing the expression and if this works correctly an adapted error message is produced. Otherwise we return the old error.

A specific test case has also been added.
This is my first PR on rust-lang/rust so any feedback is very welcome.
@bors bors closed this as completed in 6a62ea6 Nov 4, 2017
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 A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

5 participants