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

fixes #128042
This commit is contained in:
surechen 2024-07-25 22:33:45 +08:00
parent 28e684b470
commit 4ac60601d3
3 changed files with 42 additions and 1 deletions

View File

@ -2773,7 +2773,14 @@ pub(super) fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, ThinV
let snapshot = p.create_snapshot_for_diagnostic(); let snapshot = p.create_snapshot_for_diagnostic();
let param = p.parse_param_general(req_name, first_param).or_else(|e| { let param = p.parse_param_general(req_name, first_param).or_else(|e| {
let guar = e.emit(); 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); p.restore_snapshot(snapshot);
// Skip every token until next possible arg or end. // Skip every token until next possible arg or end.
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]); p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);

View File

@ -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() {}

View File

@ -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`.