Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 64da379

Browse files
committedOct 25, 2015
libsyntax: better error for lifetimes in patterns
Previously, if you copied a signature from a trait definition such as: ``` fn foo<'a>(&'a Bar) -> bool {} ``` and moved it into an `impl`, there would be an error message: "unexpected token `'a`" Adding to the error message that a pattern is expected should help users to find the actual problem with using a lifetime here.
1 parent 04e497c commit 64da379

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎src/libsyntax/parse/parser.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,6 +3196,10 @@ impl<'a> Parser<'a> {
31963196
// Parse &pat / &mut pat
31973197
try!(self.expect_and());
31983198
let mutbl = try!(self.parse_mutability());
3199+
if let token::Lifetime(ident) = self.token {
3200+
return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
3201+
}
3202+
31993203
let subpat = try!(self.parse_pat_nopanic());
32003204
pat = PatRegion(subpat, mutbl);
32013205
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn test(&'a str) {
12+
//~^ ERROR unexpected lifetime `'a` in pattern
13+
}
14+
15+
fn main() {
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.