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

parser should recover from seeing , when ; is expected #48160

Closed
pnkfelix opened this issue Feb 12, 2018 · 3 comments · Fixed by #65640
Closed

parser should recover from seeing , when ; is expected #48160

pnkfelix opened this issue Feb 12, 2018 · 3 comments · Fixed by #65640
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. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

Right now, if rustc is parsing a function body and sees a , when it expected a ;, it will give up on parsing the rest of the current function body.

Meanwhile, later phases of the compiler still run. Which means, for example, if the remainder of that erroneous function body contains the sole references to certain imports, then you get false warnings about those imports being unused.

Here is a concrete example (playground):

use std::iter::Iterator;

fn main() {
    struct S;
    let _x = 3,
    impl Iterator for S {
        type Item = S;
        fn next(&mut self) -> Option<S> { Some(S) }
    }
}

which yields the following diagnostic output on nightly and beta:

error: expected one of `.`, `;`, `?`, or an operator, found `,`
 --> src/main.rs:5:15
  |
5 |     let _x = 3,
  |               ^ expected one of `.`, `;`, `?`, or an operator here

warning: unused import: `std::iter::Iterator`
 --> src/main.rs:1:5
  |
1 | use std::iter::Iterator;
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error: aborting due to previous error

This seems to affect the current nightly and beta, but not stable (I.e., the stable channel just presents the parse error, not the erroneous lint about the supposedly unused import.)

@pnkfelix
Copy link
Member Author

pnkfelix commented Feb 12, 2018

(Okay, to be fair, its expecting any of ., ;, or ?, which means you cannot recover by replacing , with ; in the general case of a single mistaken character. But my guess is that if the user has a , there, it is often because they cut-and-pasted an expression for some input argument from some other line, and I'm betting ; is a pretty good choice for recovery...)

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. labels Feb 12, 2018
@estebank
Copy link
Contributor

estebank commented Feb 12, 2018

The parser should do a better job of recovering one char typos. At the same time, we should be doing a better job of poisoning blocks that have had parse errors to avoid typechecking them afterwards. I've made some small steps in this direction in the past, but more work is needed (and help is more than welcome).

The parser should also recover on cases where : is used instead of ;, when a ; or , is missing (#29586), when : is used instead of :: (#47666), and when a block is expected but not found (#30035, #36611).

@estebank
Copy link
Contributor

CC #44767

@estebank estebank added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
@bors bors closed this as completed in 2fe6f22 Oct 29, 2019
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. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants