Skip to content

Commit f9d3c83

Browse files
Fix let removal suggestion in struct
1 parent cde693c commit f9d3c83

6 files changed

+81
-12
lines changed

compiler/rustc_parse/src/parser/item.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1789,20 +1789,25 @@ impl<'a> Parser<'a> {
17891789
}
17901790
} else {
17911791
let mut err = self.expected_ident_found();
1792-
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" {
1793-
self.bump(); // `let`
1794-
let span = self.prev_token.span.until(self.token.span);
1792+
if self.eat_keyword_noexpect(kw::Let)
1793+
&& let removal_span = self.prev_token.span.until(self.token.span)
1794+
&& let Ok(ident) = self.parse_ident_common(false)
1795+
// Cancel this error, we don't need it.
1796+
.map_err(|err| err.cancel())
1797+
&& self.token.kind == TokenKind::Colon
1798+
{
17951799
err.span_suggestion(
1796-
span,
1797-
"remove the let, the `let` keyword is not allowed in struct field definitions",
1800+
removal_span,
1801+
"remove this `let` keyword",
17981802
String::new(),
17991803
Applicability::MachineApplicable,
18001804
);
18011805
err.note("the `let` keyword is not allowed in `struct` fields");
18021806
err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
18031807
err.emit();
1804-
self.bump();
18051808
return Ok(ident);
1809+
} else {
1810+
self.restore_snapshot(snapshot);
18061811
}
18071812
err
18081813
};
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
struct Foo {
2+
let: i32,
3+
//~^ ERROR expected identifier, found keyword
4+
}
5+
6+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: expected identifier, found keyword `let`
2+
--> $DIR/bad-let-as-field.rs:2:5
3+
|
4+
LL | struct Foo {
5+
| --- while parsing this struct
6+
LL | let: i32,
7+
| ^^^ expected identifier, found keyword
8+
|
9+
help: escape `let` to use it as an identifier
10+
|
11+
LL | r#let: i32,
12+
| ++
13+
14+
error: aborting due to previous error
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct Foo {
2+
let x: i32,
3+
//~^ ERROR expected identifier, found keyword
4+
let y: i32,
5+
//~^ ERROR expected identifier, found keyword
6+
}
7+
8+
fn main() {
9+
let _ = Foo {
10+
//~^ ERROR missing fields `x` and `y` in initializer of `Foo`
11+
};
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: expected identifier, found keyword `let`
2+
--> $DIR/removed-syntax-field-let-2.rs:2:5
3+
|
4+
LL | let x: i32,
5+
| ^^^-
6+
| |
7+
| expected identifier, found keyword
8+
| help: remove this `let` keyword
9+
|
10+
= note: the `let` keyword is not allowed in `struct` fields
11+
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
12+
13+
error: expected identifier, found keyword `let`
14+
--> $DIR/removed-syntax-field-let-2.rs:4:5
15+
|
16+
LL | let y: i32,
17+
| ^^^-
18+
| |
19+
| expected identifier, found keyword
20+
| help: remove this `let` keyword
21+
|
22+
= note: the `let` keyword is not allowed in `struct` fields
23+
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
24+
25+
error[E0063]: missing fields `x` and `y` in initializer of `Foo`
26+
--> $DIR/removed-syntax-field-let-2.rs:9:13
27+
|
28+
LL | let _ = Foo {
29+
| ^^^ missing `x` and `y`
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0063`.

src/test/ui/parser/removed-syntax-field-let.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ error: expected identifier, found keyword `let`
22
--> $DIR/removed-syntax-field-let.rs:2:5
33
|
44
LL | let foo: (),
5-
| ^^^ expected identifier, found keyword
5+
| ^^^-
6+
| |
7+
| expected identifier, found keyword
8+
| help: remove this `let` keyword
69
|
710
= note: the `let` keyword is not allowed in `struct` fields
811
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
9-
help: remove the let, the `let` keyword is not allowed in struct field definitions
10-
|
11-
LL - let foo: (),
12-
LL + foo: (),
13-
|
1412

1513
error: aborting due to previous error
1614

0 commit comments

Comments
 (0)