Fix let removal suggestion in struct
This commit is contained in:
parent
cde693cf96
commit
f9d3c83526
@ -1789,20 +1789,25 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut err = self.expected_ident_found();
|
let mut err = self.expected_ident_found();
|
||||||
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" {
|
if self.eat_keyword_noexpect(kw::Let)
|
||||||
self.bump(); // `let`
|
&& let removal_span = self.prev_token.span.until(self.token.span)
|
||||||
let span = self.prev_token.span.until(self.token.span);
|
&& let Ok(ident) = self.parse_ident_common(false)
|
||||||
|
// Cancel this error, we don't need it.
|
||||||
|
.map_err(|err| err.cancel())
|
||||||
|
&& self.token.kind == TokenKind::Colon
|
||||||
|
{
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
span,
|
removal_span,
|
||||||
"remove the let, the `let` keyword is not allowed in struct field definitions",
|
"remove this `let` keyword",
|
||||||
String::new(),
|
String::new(),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
err.note("the `let` keyword is not allowed in `struct` fields");
|
err.note("the `let` keyword is not allowed in `struct` fields");
|
||||||
err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
|
err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
|
||||||
err.emit();
|
err.emit();
|
||||||
self.bump();
|
|
||||||
return Ok(ident);
|
return Ok(ident);
|
||||||
|
} else {
|
||||||
|
self.restore_snapshot(snapshot);
|
||||||
}
|
}
|
||||||
err
|
err
|
||||||
};
|
};
|
||||||
|
6
src/test/ui/parser/bad-let-as-field.rs
Normal file
6
src/test/ui/parser/bad-let-as-field.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
struct Foo {
|
||||||
|
let: i32,
|
||||||
|
//~^ ERROR expected identifier, found keyword
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
15
src/test/ui/parser/bad-let-as-field.stderr
Normal file
15
src/test/ui/parser/bad-let-as-field.stderr
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
error: expected identifier, found keyword `let`
|
||||||
|
--> $DIR/bad-let-as-field.rs:2:5
|
||||||
|
|
|
||||||
|
LL | struct Foo {
|
||||||
|
| --- while parsing this struct
|
||||||
|
LL | let: i32,
|
||||||
|
| ^^^ expected identifier, found keyword
|
||||||
|
|
|
||||||
|
help: escape `let` to use it as an identifier
|
||||||
|
|
|
||||||
|
LL | r#let: i32,
|
||||||
|
| ++
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
12
src/test/ui/parser/removed-syntax-field-let-2.rs
Normal file
12
src/test/ui/parser/removed-syntax-field-let-2.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
struct Foo {
|
||||||
|
let x: i32,
|
||||||
|
//~^ ERROR expected identifier, found keyword
|
||||||
|
let y: i32,
|
||||||
|
//~^ ERROR expected identifier, found keyword
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = Foo {
|
||||||
|
//~^ ERROR missing fields `x` and `y` in initializer of `Foo`
|
||||||
|
};
|
||||||
|
}
|
33
src/test/ui/parser/removed-syntax-field-let-2.stderr
Normal file
33
src/test/ui/parser/removed-syntax-field-let-2.stderr
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
error: expected identifier, found keyword `let`
|
||||||
|
--> $DIR/removed-syntax-field-let-2.rs:2:5
|
||||||
|
|
|
||||||
|
LL | let x: i32,
|
||||||
|
| ^^^-
|
||||||
|
| |
|
||||||
|
| expected identifier, found keyword
|
||||||
|
| help: remove this `let` keyword
|
||||||
|
|
|
||||||
|
= note: the `let` keyword is not allowed in `struct` fields
|
||||||
|
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
|
||||||
|
|
||||||
|
error: expected identifier, found keyword `let`
|
||||||
|
--> $DIR/removed-syntax-field-let-2.rs:4:5
|
||||||
|
|
|
||||||
|
LL | let y: i32,
|
||||||
|
| ^^^-
|
||||||
|
| |
|
||||||
|
| expected identifier, found keyword
|
||||||
|
| help: remove this `let` keyword
|
||||||
|
|
|
||||||
|
= note: the `let` keyword is not allowed in `struct` fields
|
||||||
|
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
|
||||||
|
|
||||||
|
error[E0063]: missing fields `x` and `y` in initializer of `Foo`
|
||||||
|
--> $DIR/removed-syntax-field-let-2.rs:9:13
|
||||||
|
|
|
||||||
|
LL | let _ = Foo {
|
||||||
|
| ^^^ missing `x` and `y`
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0063`.
|
@ -2,15 +2,13 @@ error: expected identifier, found keyword `let`
|
|||||||
--> $DIR/removed-syntax-field-let.rs:2:5
|
--> $DIR/removed-syntax-field-let.rs:2:5
|
||||||
|
|
|
|
||||||
LL | let foo: (),
|
LL | let foo: (),
|
||||||
| ^^^ expected identifier, found keyword
|
| ^^^-
|
||||||
|
| |
|
||||||
|
| expected identifier, found keyword
|
||||||
|
| help: remove this `let` keyword
|
||||||
|
|
|
|
||||||
= note: the `let` keyword is not allowed in `struct` fields
|
= note: the `let` keyword is not allowed in `struct` fields
|
||||||
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
|
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information
|
||||||
help: remove the let, the `let` keyword is not allowed in struct field definitions
|
|
||||||
|
|
|
||||||
LL - let foo: (),
|
|
||||||
LL + foo: (),
|
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user