]> git.lizzy.rs Git - rust.git/commitdiff
libsyntax: better error for lifetimes in patterns
authorKevin Butler <haqkrs@gmail.com>
Sat, 24 Oct 2015 23:57:42 +0000 (00:57 +0100)
committerKevin Butler <haqkrs@gmail.com>
Sun, 25 Oct 2015 00:28:00 +0000 (01:28 +0100)
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.

src/libsyntax/parse/parser.rs
src/test/parse-fail/lifetime-in-pattern.rs [new file with mode: 0644]

index fcebe03596103b128d1942ec97ccfc7fe4de6e31..092013a4753b55d138b9f35b67cfced573e38cfb 100644 (file)
@@ -3196,6 +3196,10 @@ pub fn parse_pat_nopanic(&mut self) -> PResult<P<Pat>> {
             // Parse &pat / &mut pat
             try!(self.expect_and());
             let mutbl = try!(self.parse_mutability());
+            if let token::Lifetime(ident) = self.token {
+                return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
+            }
+
             let subpat = try!(self.parse_pat_nopanic());
             pat = PatRegion(subpat, mutbl);
           }
diff --git a/src/test/parse-fail/lifetime-in-pattern.rs b/src/test/parse-fail/lifetime-in-pattern.rs
new file mode 100644 (file)
index 0000000..8802497
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn test(&'a str) {
+    //~^ ERROR unexpected lifetime `'a` in pattern
+}
+
+fn main() {
+}