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

Fix a span error when parsing a wrong param of function. #128185

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2773,7 +2773,14 @@ impl<'a> Parser<'a> {
let snapshot = p.create_snapshot_for_diagnostic();
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
let guar = e.emit();
let lo = p.prev_token.span;
// When parsing a param failed, we should check to make the span of the param
// not contain '(' before it.
// For example when parsing `*mut Self` in function `fn oof(*mut Self)`.
let lo = if let TokenKind::OpenDelim(Delimiter::Parenthesis) = p.prev_token.kind {
p.prev_token.span.shrink_to_hi()
} else {
p.prev_token.span
};
p.restore_snapshot(snapshot);
// Skip every token until next possible arg or end.
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/suggestions/suggest-add-self-issue-128042.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct Thing {
state: u8,
}

impl Thing {
fn oof(*mut Self) { //~ ERROR expected parameter name, found `*`
self.state = 1;
//~^ ERROR expected value, found module `self`
}
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/suggestions/suggest-add-self-issue-128042.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: expected parameter name, found `*`
--> $DIR/suggest-add-self-issue-128042.rs:6:12
|
LL | fn oof(*mut Self) {
| ^ expected parameter name

error[E0424]: expected value, found module `self`
--> $DIR/suggest-add-self-issue-128042.rs:7:9
|
LL | fn oof(*mut Self) {
| --- this function doesn't have a `self` parameter
LL | self.state = 1;
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
|
help: add a `self` receiver parameter to make the associated `fn` a method
|
LL | fn oof(&self, *mut Self) {
| ++++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0424`.
Loading