Skip to content

Commit 1055bdf

Browse files
committed
Accept interpolated patterns in trait method parameters
Remove some outdated messages from "no patterns allowed" errors
1 parent 69ee5a8 commit 1055bdf

File tree

4 files changed

+56
-46
lines changed

4 files changed

+56
-46
lines changed

src/librustc_passes/ast_validation.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ impl<'a> AstValidator<'a> {
6767
}
6868
}
6969

70-
fn check_decl_no_pat<ReportFn: Fn(Span, bool)>(&self, decl: &FnDecl, report_err: ReportFn) {
70+
fn check_decl_no_pat<ReportFn: Fn(Span)>(&self, decl: &FnDecl, report_err: ReportFn) {
7171
for arg in &decl.inputs {
7272
match arg.pat.node {
7373
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
7474
PatKind::Wild => {}
75-
PatKind::Ident(..) => report_err(arg.pat.span, true),
76-
_ => report_err(arg.pat.span, false),
75+
_ => report_err(arg.pat.span),
7776
}
7877
}
7978
}
@@ -150,15 +149,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
150149
fn visit_ty(&mut self, ty: &'a Ty) {
151150
match ty.node {
152151
TyKind::BareFn(ref bfty) => {
153-
self.check_decl_no_pat(&bfty.decl, |span, _| {
154-
let mut err = struct_span_err!(self.session,
155-
span,
156-
E0561,
157-
"patterns aren't allowed in function pointer \
158-
types");
159-
err.span_note(span,
160-
"this is a recent error, see issue #35203 for more details");
161-
err.emit();
152+
self.check_decl_no_pat(&bfty.decl, |span| {
153+
struct_span_err!(self.session, span, E0561,
154+
"patterns aren't allowed in function pointer types").emit();
162155
});
163156
}
164157
TyKind::TraitObject(ref bounds, ..) => {
@@ -260,7 +253,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
260253
if let TraitItemKind::Method(ref sig, ref block) = trait_item.node {
261254
self.check_trait_fn_not_const(sig.constness);
262255
if block.is_none() {
263-
self.check_decl_no_pat(&sig.decl, |span, _| {
256+
self.check_decl_no_pat(&sig.decl, |span| {
264257
self.session.buffer_lint(
265258
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
266259
trait_item.id, span,
@@ -299,18 +292,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
299292
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
300293
match fi.node {
301294
ForeignItemKind::Fn(ref decl, _) => {
302-
self.check_decl_no_pat(decl, |span, is_recent| {
303-
let mut err = struct_span_err!(self.session,
304-
span,
305-
E0130,
306-
"patterns aren't allowed in foreign function \
307-
declarations");
308-
err.span_label(span, "pattern not allowed in foreign function");
309-
if is_recent {
310-
err.span_note(span,
311-
"this is a recent error, see issue #35203 for more details");
312-
}
313-
err.emit();
295+
self.check_decl_no_pat(decl, |span| {
296+
struct_span_err!(self.session, span, E0130,
297+
"patterns aren't allowed in foreign function declarations")
298+
.span_label(span, "pattern not allowed in foreign function").emit();
314299
});
315300
}
316301
ForeignItemKind::Static(..) | ForeignItemKind::Ty => {}

src/libsyntax/parse/parser.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,6 @@ impl TokenType {
360360
}
361361
}
362362

363-
fn is_ident_or_underscore(t: &token::Token) -> bool {
364-
t.is_ident() || *t == token::Underscore
365-
}
366-
367363
// Returns true if `IDENT t` can start a type - `IDENT::a::b`, `IDENT<u8, u8>`,
368364
// `IDENT<<u8 as Trait>::AssocTy>`, `IDENT(u8, u8) -> u8`.
369365
fn can_continue_type_after_ident(t: &token::Token) -> bool {
@@ -1625,23 +1621,19 @@ impl<'a> Parser<'a> {
16251621
Ok(MutTy { ty: t, mutbl: mutbl })
16261622
}
16271623

1628-
pub fn is_named_argument(&mut self) -> bool {
1624+
fn is_named_argument(&mut self) -> bool {
16291625
let offset = match self.token {
1630-
token::BinOp(token::And) |
1631-
token::AndAnd => 1,
1626+
token::Interpolated(ref nt) => match nt.0 {
1627+
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
1628+
_ => 0,
1629+
}
1630+
token::BinOp(token::And) | token::AndAnd => 1,
16321631
_ if self.token.is_keyword(keywords::Mut) => 1,
1633-
_ => 0
1632+
_ => 0,
16341633
};
16351634

1636-
debug!("parser is_named_argument offset:{}", offset);
1637-
1638-
if offset == 0 {
1639-
is_ident_or_underscore(&self.token)
1640-
&& self.look_ahead(1, |t| *t == token::Colon)
1641-
} else {
1642-
self.look_ahead(offset, |t| is_ident_or_underscore(t))
1643-
&& self.look_ahead(offset + 1, |t| *t == token::Colon)
1644-
}
1635+
self.look_ahead(offset, |t| t.is_ident() || t == &token::Underscore) &&
1636+
self.look_ahead(offset + 1, |t| t == &token::Colon)
16451637
}
16461638

16471639
/// This version of parse arg doesn't necessarily require
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2017 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+
macro_rules! m {
12+
($pat: pat) => {
13+
trait Tr {
14+
fn trait_method($pat: u8);
15+
}
16+
17+
type A = fn($pat: u8);
18+
19+
extern {
20+
fn foreign_fn($pat: u8);
21+
}
22+
}
23+
}
24+
25+
mod good_pat {
26+
m!(good_pat); // OK
27+
}
28+
29+
mod bad_pat {
30+
m!((bad, pat));
31+
//~^ ERROR patterns aren't allowed in function pointer types
32+
//~| ERROR patterns aren't allowed in foreign function declarations
33+
//~| WARN patterns aren't allowed in methods without bodies
34+
//~| WARN this was previously accepted
35+
}
36+
37+
fn main() {}

src/test/compile-fail/no-patterns-in-args.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@
1111
extern {
1212
fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in foreign function declarations
1313
//~^ NOTE pattern not allowed in foreign function
14-
//~| NOTE this is a recent error
1514
fn f2(&arg: u8); //~ ERROR patterns aren't allowed in foreign function declarations
1615
//~^ NOTE pattern not allowed in foreign function
1716
fn f3(arg @ _: u8); //~ ERROR patterns aren't allowed in foreign function declarations
1817
//~^ NOTE pattern not allowed in foreign function
19-
//~| NOTE this is a recent error
2018
fn g1(arg: u8); // OK
2119
fn g2(_: u8); // OK
2220
// fn g3(u8); // Not yet
2321
}
2422

2523
type A1 = fn(mut arg: u8); //~ ERROR patterns aren't allowed in function pointer types
26-
//~^ NOTE this is a recent error
2724
type A2 = fn(&arg: u8); //~ ERROR patterns aren't allowed in function pointer types
28-
//~^ NOTE this is a recent error
2925
type B1 = fn(arg: u8); // OK
3026
type B2 = fn(_: u8); // OK
3127
type B3 = fn(u8); // OK

0 commit comments

Comments
 (0)