From 64da379c8c6c967a451817dc4909eeb0055351e7 Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Sun, 25 Oct 2015 00:57:42 +0100 Subject: [PATCH 1/3] 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. --- src/libsyntax/parse/parser.rs | 4 ++++ src/test/parse-fail/lifetime-in-pattern.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/test/parse-fail/lifetime-in-pattern.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index fcebe03596103..092013a4753b5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3196,6 +3196,10 @@ impl<'a> Parser<'a> { // 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 index 0000000000000..8802497ae1bfd --- /dev/null +++ b/src/test/parse-fail/lifetime-in-pattern.rs @@ -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 or the MIT license +// , 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() { +} From 998914f5da91bd6e07a0bdf45612dcd446e6fa94 Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Sun, 25 Oct 2015 01:02:08 +0100 Subject: [PATCH 2/3] libsyntax: remove panics from Parser::parse_pat_nopanic --- src/libsyntax/parse/parser.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 092013a4753b5..7bc9441fb9dbb 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3276,12 +3276,9 @@ impl<'a> Parser<'a> { } token::OpenDelim(token::Brace) => { if qself.is_some() { - let span = self.span; - self.span_err(span, - "unexpected `{` after qualified path"); - self.abort_if_errors(); + return Err(self.fatal("unexpected `{` after qualified path")); } - // Parse struct pattern + // Parse struct pattern try!(self.bump()); let (fields, etc) = try!(self.parse_pat_fields()); try!(self.bump()); @@ -3289,10 +3286,7 @@ impl<'a> Parser<'a> { } token::OpenDelim(token::Paren) => { if qself.is_some() { - let span = self.span; - self.span_err(span, - "unexpected `(` after qualified path"); - self.abort_if_errors(); + return Err(self.fatal("unexpected `(` after qualified path")); } // Parse tuple struct or enum pattern if self.look_ahead(1, |t| *t == token::DotDot) { From 1a235274a1aec01d8bf7f30e1631fba62e2ca411 Mon Sep 17 00:00:00 2001 From: Kevin Butler Date: Sun, 25 Oct 2015 01:07:12 +0100 Subject: [PATCH 3/3] libsyntax: Consolidate branch to benefit from exhaustive checking instead of unwrapping --- src/libsyntax/parse/parser.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7bc9441fb9dbb..02faf315f01ca 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3304,13 +3304,13 @@ impl<'a> Parser<'a> { pat = PatEnum(path, Some(args)); } } - _ if qself.is_some() => { - // Parse qualified path - pat = PatQPath(qself.unwrap(), path); - } _ => { - // Parse nullary enum - pat = PatEnum(path, Some(vec![])); + pat = match qself { + // Parse qualified path + Some(qself) => PatQPath(qself, path), + // Parse nullary enum + None => PatEnum(path, Some(vec![])) + }; } } }