parser: recover on `&'lifetime mut $pat`.

This commit is contained in:
Mazdak Farrokhzad 2019-12-13 00:59:33 +01:00
parent e9469a6aec
commit 45c1e38147
6 changed files with 56 additions and 10 deletions

View File

@ -459,18 +459,28 @@ impl<'a> Parser<'a> {
/// Parse `&pat` / `&mut pat`.
fn parse_pat_deref(&mut self, expected: Expected) -> PResult<'a, PatKind> {
self.expect_and()?;
self.recover_lifetime_in_deref_pat();
let mutbl = self.parse_mutability();
if let token::Lifetime(name) = self.token.kind {
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
err.span_label(self.token.span, "unexpected lifetime");
return Err(err);
}
let subpat = self.parse_pat_with_range_pat(false, expected)?;
Ok(PatKind::Ref(subpat, mutbl))
}
fn recover_lifetime_in_deref_pat(&mut self) {
if let token::Lifetime(name) = self.token.kind {
self.bump(); // `'a`
let span = self.prev_span;
self.struct_span_err(span, &format!("unexpected lifetime `{}` in pattern", name))
.span_suggestion(
span,
"remove the lifetime",
String::new(),
Applicability::MachineApplicable,
)
.emit();
}
}
/// Parse a tuple or parenthesis pattern.
fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| p.parse_pat_with_or_inner())?;

View File

@ -0,0 +1,6 @@
fn main() {
let &'a x = &0; //~ ERROR unexpected lifetime `'a` in pattern
let &'a mut y = &mut 0; //~ ERROR unexpected lifetime `'a` in pattern
let _recovery_witness: () = 0; //~ ERROR mismatched types
}

View File

@ -0,0 +1,23 @@
error: unexpected lifetime `'a` in pattern
--> $DIR/lifetime-in-pattern-recover.rs:2:10
|
LL | let &'a x = &0;
| ^^ help: remove the lifetime
error: unexpected lifetime `'a` in pattern
--> $DIR/lifetime-in-pattern-recover.rs:3:10
|
LL | let &'a mut y = &mut 0;
| ^^ help: remove the lifetime
error[E0308]: mismatched types
--> $DIR/lifetime-in-pattern-recover.rs:5:33
|
LL | let _recovery_witness: () = 0;
| -- ^ expected `()`, found integer
| |
| expected due to this
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@ -1,5 +1,6 @@
fn test(&'a str) {
//~^ ERROR unexpected lifetime `'a` in pattern
//~| ERROR expected one of `:`, `@`, or `|`, found `)`
}
fn main() {

View File

@ -2,7 +2,13 @@ error: unexpected lifetime `'a` in pattern
--> $DIR/lifetime-in-pattern.rs:1:10
|
LL | fn test(&'a str) {
| ^^ unexpected lifetime
| ^^ help: remove the lifetime
error: aborting due to previous error
error: expected one of `:`, `@`, or `|`, found `)`
--> $DIR/lifetime-in-pattern.rs:1:16
|
LL | fn test(&'a str) {
| ^ expected one of `:`, `@`, or `|`
error: aborting due to 2 previous errors

View File

@ -2,7 +2,7 @@ error: unexpected lifetime `'a` in pattern
--> $DIR/self-vs-path-ambiguity.rs:9:11
|
LL | fn i(&'a self::S: &S) {}
| ^^ unexpected lifetime
| ^^ help: remove the lifetime
error: aborting due to previous error