Rollup merge of #86812 - FabianWolff:recover-dyn-mut, r=petrochenkov

Recover from `&dyn mut ...` parse errors

Consider this example:
```rust
fn main() {
    let r: &dyn mut Trait;
}
```
This currently leads to:
```
error: expected one of `!`, `(`, `;`, `=`, `?`, `for`, lifetime, or path, found keyword `mut`
 --> src/main.rs:2:17
  |
2 |     let r: &dyn mut Trait;
  |                 ^^^ expected one of 8 possible tokens

error: aborting due to previous error
```
However, especially for beginners, I think it is easy to get `&dyn mut` and `&mut dyn` confused. With my changes, I get a help message, and the parser even recovers:
```
error: `mut` must precede `dyn`
 --> test.rs:2:12
  |
2 |     let r: &dyn mut Trait;
  |            ^^^^^^^^ help: place `mut` before `dyn`: `&mut dyn`

error[E0405]: cannot find trait `Trait` in this scope
 --> test.rs:2:21
  |
2 |     let r: &dyn mut Trait;
  |                     ^^^^^ not found in this scope

error: aborting due to 2 previous errors
```
This commit is contained in:
Yuki Okushi 2021-07-08 10:44:30 +09:00 committed by GitHub
commit 165b520b89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View File

@ -393,7 +393,7 @@ impl<'a> Parser<'a> {
let and_span = self.prev_token.span;
let mut opt_lifetime =
if self.check_lifetime() { Some(self.expect_lifetime()) } else { None };
let mutbl = self.parse_mutability();
let mut mutbl = self.parse_mutability();
if self.token.is_lifetime() && mutbl == Mutability::Mut && opt_lifetime.is_none() {
// A lifetime is invalid here: it would be part of a bare trait bound, which requires
// it to be followed by a plus, but we disallow plus in the pointee type.
@ -417,6 +417,26 @@ impl<'a> Parser<'a> {
opt_lifetime = Some(self.expect_lifetime());
}
} else if self.token.is_keyword(kw::Dyn)
&& mutbl == Mutability::Not
&& self.look_ahead(1, |t| t.is_keyword(kw::Mut))
{
// We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
let span = and_span.to(self.look_ahead(1, |t| t.span));
let mut err = self.struct_span_err(span, "`mut` must precede `dyn`");
err.span_suggestion(
span,
"place `mut` before `dyn`",
"&mut dyn".to_string(),
Applicability::MachineApplicable,
);
err.emit();
// Recovery
mutbl = Mutability::Mut;
let (dyn_tok, dyn_tok_sp) = (self.token.clone(), self.token_spacing);
self.bump();
self.bump_with((dyn_tok, dyn_tok_sp));
}
let ty = self.parse_ty_no_plus()?;
Ok(TyKind::Rptr(opt_lifetime, MutTy { ty, mutbl }))

View File

@ -0,0 +1,9 @@
// Test that the parser detects `&dyn mut`, offers a help message, and
// recovers.
fn main() {
let r: &dyn mut Trait;
//~^ ERROR: `mut` must precede `dyn`
//~| HELP: place `mut` before `dyn`
//~| ERROR: cannot find trait `Trait` in this scope [E0405]
}

View File

@ -0,0 +1,15 @@
error: `mut` must precede `dyn`
--> $DIR/recover-ref-dyn-mut.rs:5:12
|
LL | let r: &dyn mut Trait;
| ^^^^^^^^ help: place `mut` before `dyn`: `&mut dyn`
error[E0405]: cannot find trait `Trait` in this scope
--> $DIR/recover-ref-dyn-mut.rs:5:21
|
LL | let r: &dyn mut Trait;
| ^^^^^ not found in this scope
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0405`.