-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Improve parse item fallback #125388
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
Open
oriongonza
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
oriongonza:improve-parse-item-fallback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−18
Open
Improve parse item fallback #125388
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ use tracing::debug; | |
use super::diagnostics::{ConsumeClosingDelim, dummy_arg}; | ||
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; | ||
use super::{ | ||
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Trailing, UsePreAttrPos, | ||
AttemptLocalParseRecovery, AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, | ||
Trailing, UsePreAttrPos, | ||
}; | ||
use crate::errors::{self, MacroExpandsToAdtField}; | ||
use crate::{fluent_generated as fluent, maybe_whole}; | ||
|
@@ -74,21 +75,11 @@ impl<'a> Parser<'a> { | |
items.push(item); | ||
} | ||
|
||
// The last token should be `term`: either EOF or `}`. If it's not that means that we've had an error | ||
// parsing an item | ||
if !self.eat(term) { | ||
let token_str = super::token_descr(&self.token); | ||
if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) { | ||
let msg = format!("expected item, found {token_str}"); | ||
let mut err = self.dcx().struct_span_err(self.token.span, msg); | ||
let span = self.token.span; | ||
if self.is_kw_followed_by_ident(kw::Let) { | ||
err.span_label( | ||
span, | ||
"consider using `const` or `static` instead of `let` for global variables", | ||
); | ||
} else { | ||
err.span_label(span, "expected item") | ||
.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>"); | ||
}; | ||
let err = self.fallback_incorrect_item(); | ||
return Err(err); | ||
} | ||
} | ||
|
@@ -97,6 +88,65 @@ impl<'a> Parser<'a> { | |
let mod_spans = ModSpans { inner_span: lo.to(self.prev_token.span), inject_use_span }; | ||
Ok((attrs, items, mod_spans)) | ||
} | ||
|
||
/// Tries to parse the item as a statement to provide further diagnostics. | ||
fn fallback_incorrect_item(&mut self) -> rustc_errors::Diag<'a> { | ||
let token_str = super::token_descr(&self.token); | ||
let token_span = self.token.span; | ||
let mut err = | ||
self.dcx().struct_span_err(token_span, format!("expected item, found {token_str}")); | ||
|
||
let mut do_default_diag = true; | ||
|
||
match self.parse_full_stmt(AttemptLocalParseRecovery::No) { | ||
Ok(Some(stmt)) => { | ||
do_default_diag = false; | ||
let span = stmt.span; | ||
match &stmt.kind { | ||
StmtKind::Let(_) => { | ||
err.span_label(span, "unexpected `let` binding outside of a function") | ||
.help(format!("consider using `const` or `static` instead of `let` for global variables, or put it inside of a function: fn foo() {{ {} }}", | ||
pprust::stmt_to_string(&stmt))); | ||
} | ||
StmtKind::Semi(expr) => { | ||
err.span_label(span, "unexpected expression").help(format!( | ||
"consider putting it inside a function: fn foo() {{ {}; }}", | ||
pprust::expr_to_string(expr) | ||
)); | ||
} | ||
StmtKind::Expr(expr) => { | ||
err.span_label(span, "unexpected expression").help(format!( | ||
"consider putting it inside a function: fn foo() {{ {} }}", | ||
pprust::expr_to_string(expr) | ||
)); | ||
} | ||
StmtKind::Empty => { | ||
unreachable!( | ||
"Should have been handled by maybe_consume_incorrect_semicolon" | ||
); | ||
} | ||
StmtKind::Item(_) | StmtKind::MacCall(_) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Item should be unreachable, right? |
||
// These typically are valid items after an error, use the default message. | ||
do_default_diag = true; | ||
} | ||
}; | ||
} | ||
// It's not a statement, we can't do much recovery. | ||
Ok(None) => {} | ||
Err(e) => { | ||
// We don't really care about an error parsing this statement. | ||
e.cancel(); | ||
} | ||
} | ||
|
||
if do_default_diag { | ||
err.span_label(token_span, "expected item"); | ||
} | ||
|
||
err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>"); | ||
|
||
err | ||
} | ||
} | ||
|
||
pub(super) type ItemInfo = (Ident, ItemKind); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod m { | ||
pub let answer = 42; | ||
//~^ ERROR visibility `pub` is not followed by an item | ||
//~| ERROR expected item, found keyword `let` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error: visibility `pub` is not followed by an item | ||
--> $DIR/pub-let-outside-fn.rs:2:5 | ||
| | ||
LL | pub let answer = 42; | ||
| ^^^ the visibility | ||
| | ||
= help: you likely meant to define an item, e.g., `pub fn foo() {}` | ||
|
||
error: expected item, found keyword `let` | ||
--> $DIR/pub-let-outside-fn.rs:2:9 | ||
| | ||
LL | pub let answer = 42; | ||
| ^^^------------- | ||
| | | ||
| unexpected `let` binding outside of a function | ||
| | ||
= help: consider using `const` or `static` instead of `let` for global variables, or put it inside of a function: fn foo() { let answer = 42; } | ||
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html> | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ error: expected item, found `[` | |
--> $DIR/shebang-doc-comment.rs:2:1 | ||
| | ||
LL | [allow(unused_variables)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a code path for array expressions and suggest adding a |
||
| ^ expected item | ||
| ^------------------------ | ||
| | | ||
| unexpected expression | ||
| | ||
= help: consider putting it inside a function: fn foo() { [allow(unused_variables)] } | ||
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html> | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diagnostic can potentially become very big. Either use structured suggestions, or do not put a suggestion into the diagnostic message itself.