Skip to content

Commit 6cd2f1c

Browse files
authored
Unrolled build for rust-lang#128185
Rollup merge of rust-lang#128185 - surechen:fix_128042_2, r=compiler-errors Fix a span error when parsing a wrong param of function. fixes rust-lang#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.
2 parents eb10639 + 4ac6060 commit 6cd2f1c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,14 @@ impl<'a> Parser<'a> {
27732773
let snapshot = p.create_snapshot_for_diagnostic();
27742774
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
27752775
let guar = e.emit();
2776-
let lo = p.prev_token.span;
2776+
// When parsing a param failed, we should check to make the span of the param
2777+
// not contain '(' before it.
2778+
// For example when parsing `*mut Self` in function `fn oof(*mut Self)`.
2779+
let lo = if let TokenKind::OpenDelim(Delimiter::Parenthesis) = p.prev_token.kind {
2780+
p.prev_token.span.shrink_to_hi()
2781+
} else {
2782+
p.prev_token.span
2783+
};
27772784
p.restore_snapshot(snapshot);
27782785
// Skip every token until next possible arg or end.
27792786
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct Thing {
2+
state: u8,
3+
}
4+
5+
impl Thing {
6+
fn oof(*mut Self) { //~ ERROR expected parameter name, found `*`
7+
self.state = 1;
8+
//~^ ERROR expected value, found module `self`
9+
}
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: expected parameter name, found `*`
2+
--> $DIR/suggest-add-self-issue-128042.rs:6:12
3+
|
4+
LL | fn oof(*mut Self) {
5+
| ^ expected parameter name
6+
7+
error[E0424]: expected value, found module `self`
8+
--> $DIR/suggest-add-self-issue-128042.rs:7:9
9+
|
10+
LL | fn oof(*mut Self) {
11+
| --- this function doesn't have a `self` parameter
12+
LL | self.state = 1;
13+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
14+
|
15+
help: add a `self` receiver parameter to make the associated `fn` a method
16+
|
17+
LL | fn oof(&self, *mut Self) {
18+
| ++++++
19+
20+
error: aborting due to 2 previous errors
21+
22+
For more information about this error, try `rustc --explain E0424`.

0 commit comments

Comments
 (0)