Rollup merge of #102927 - compiler-errors:let, r=davidtwco
Fix `let` keyword removal suggestion in structs (1.) Fixes a bug where, given this code: ```rust struct Foo { let x: i32, } ``` We were parsing the field name as `let` instead of `x`, which causes issues later on in the type-checking phase. (2.) Also, suggestions for `let: i32` as a field regressed, displaying this extra `help:` which is removed by this PR ``` help: remove the let, the `let` keyword is not allowed in struct field definitions | 2 - let: i32, 2 + : i32, ``` (3.) Makes the suggestion text a bit more succinct, since we don't need to re-explain that `let` is not allowed in this position (since it's in a note that follows). This causes the suggestion to render inline as well. cc `@gimbles,` this addresses a few nits I mentioned in your PR.
This commit is contained in:
commit
a9a5529eac
6 changed files with 81 additions and 12 deletions
|
@ -1789,20 +1789,25 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
} else {
|
||||
let mut err = self.expected_ident_found();
|
||||
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" {
|
||||
self.bump(); // `let`
|
||||
let span = self.prev_token.span.until(self.token.span);
|
||||
if self.eat_keyword_noexpect(kw::Let)
|
||||
&& let removal_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(
|
||||
span,
|
||||
"remove the let, the `let` keyword is not allowed in struct field definitions",
|
||||
removal_span,
|
||||
"remove this `let` keyword",
|
||||
String::new(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
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.emit();
|
||||
self.bump();
|
||||
return Ok(ident);
|
||||
} else {
|
||||
self.restore_snapshot(snapshot);
|
||||
}
|
||||
err
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue