Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `|`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something we should handle better :-/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the solution here is to make the type ascription optional syntactically. This would allow us to handle this in e.g. resolve or typeck to handle this well without any hacks.


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