diff options
| author | surechen <chenshuo17@huawei.com> | 2024-07-25 22:33:45 +0800 |
|---|---|---|
| committer | surechen <chenshuo17@huawei.com> | 2024-07-25 22:33:45 +0800 |
| commit | 4ac60601d32d96688494c4bb404a04205501a02b (patch) | |
| tree | 62ad23fb3b49e5a797d1bcbbe8d99ee6d3114236 | |
| parent | 28e684b47000d4ed4cdb5d982331e5ff8141c1ce (diff) | |
| download | rust-4ac60601d32d96688494c4bb404a04205501a02b.tar.gz rust-4ac60601d32d96688494c4bb404a04205501a02b.zip | |
Fix a span error when parsing a wrong param of function.
fixes #128042
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 9 | ||||
| -rw-r--r-- | tests/ui/suggestions/suggest-add-self-issue-128042.rs | 12 | ||||
| -rw-r--r-- | tests/ui/suggestions/suggest-add-self-issue-128042.stderr | 22 |
3 files changed, 42 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index fbc5b914600..9aaf4b99243 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -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)]); diff --git a/tests/ui/suggestions/suggest-add-self-issue-128042.rs b/tests/ui/suggestions/suggest-add-self-issue-128042.rs new file mode 100644 index 00000000000..f94d166c496 --- /dev/null +++ b/tests/ui/suggestions/suggest-add-self-issue-128042.rs @@ -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() {} diff --git a/tests/ui/suggestions/suggest-add-self-issue-128042.stderr b/tests/ui/suggestions/suggest-add-self-issue-128042.stderr new file mode 100644 index 00000000000..49ac1563250 --- /dev/null +++ b/tests/ui/suggestions/suggest-add-self-issue-128042.stderr @@ -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`. |
