Skip to content

Commit 278cc2f

Browse files
committed
Auto merge of #29287 - Ryman:fn_nopat, r=alexcrichton
Previously, if you copied a signature from a trait definition such as: ```rust 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.
2 parents c5d650c + 1a23527 commit 278cc2f

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/libsyntax/parse/parser.rs

+13-15
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
}
@@ -3272,23 +3276,17 @@ impl<'a> Parser<'a> {
32723276
}
32733277
token::OpenDelim(token::Brace) => {
32743278
if qself.is_some() {
3275-
let span = self.span;
3276-
self.span_err(span,
3277-
"unexpected `{` after qualified path");
3278-
self.abort_if_errors();
3279+
return Err(self.fatal("unexpected `{` after qualified path"));
32793280
}
3280-
// Parse struct pattern
3281+
// Parse struct pattern
32813282
try!(self.bump());
32823283
let (fields, etc) = try!(self.parse_pat_fields());
32833284
try!(self.bump());
32843285
pat = PatStruct(path, fields, etc);
32853286
}
32863287
token::OpenDelim(token::Paren) => {
32873288
if qself.is_some() {
3288-
let span = self.span;
3289-
self.span_err(span,
3290-
"unexpected `(` after qualified path");
3291-
self.abort_if_errors();
3289+
return Err(self.fatal("unexpected `(` after qualified path"));
32923290
}
32933291
// Parse tuple struct or enum pattern
32943292
if self.look_ahead(1, |t| *t == token::DotDot) {
@@ -3306,13 +3304,13 @@ impl<'a> Parser<'a> {
33063304
pat = PatEnum(path, Some(args));
33073305
}
33083306
}
3309-
_ if qself.is_some() => {
3310-
// Parse qualified path
3311-
pat = PatQPath(qself.unwrap(), path);
3312-
}
33133307
_ => {
3314-
// Parse nullary enum
3315-
pat = PatEnum(path, Some(vec![]));
3308+
pat = match qself {
3309+
// Parse qualified path
3310+
Some(qself) => PatQPath(qself, path),
3311+
// Parse nullary enum
3312+
None => PatEnum(path, Some(vec![]))
3313+
};
33163314
}
33173315
}
33183316
}
+16
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)