Skip to content

Commit

Permalink
Rollup merge of rust-lang#67269 - Centril:recover-lt-deref-pat, r=est…
Browse files Browse the repository at this point in the history
…ebank

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

r? @estebank
  • Loading branch information
Centril authored Dec 13, 2019
2 parents d2d5b83 + 45c1e38 commit d25197c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/librustc_parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,26 @@ 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();
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 {
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
err.span_label(self.token.span, "unexpected lifetime");
return Err(err);
}
self.bump(); // `'a`

let subpat = self.parse_pat_with_range_pat(false, expected)?;
Ok(PatKind::Ref(subpat, mutbl))
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.
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/parser/lifetime-in-pattern-recover.rs
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions src/test/ui/parser/lifetime-in-pattern-recover.stderr
Original file line number Diff line number Diff line change
@@ -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`.
1 change: 1 addition & 0 deletions src/test/ui/parser/lifetime-in-pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fn test(&'a str) {
//~^ ERROR unexpected lifetime `'a` in pattern
//~| ERROR expected one of `:`, `@`, or `|`, found `)`
}

fn main() {
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/parser/lifetime-in-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/self/self-vs-path-ambiguity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d25197c

Please sign in to comment.