Skip to content

Commit

Permalink
Rollup merge of #128185 - surechen:fix_128042_2, r=compiler-errors
Browse files Browse the repository at this point in the history
Fix a span error when parsing a wrong param of function.

fixes #128042

Before this change, the span of param `*mut Self` in  `fn oof(*mut Self)` contains `(` before it, so the suggestion in E0424 will be error.
  • Loading branch information
matthiaskrgr authored Jul 25, 2024
2 parents 512277f + 4ac6060 commit 9fd5679
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
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`.

0 comments on commit 9fd5679

Please sign in to comment.