From 5c814e2e4e0649972ec6a18c7dbf57259edf2210 Mon Sep 17 00:00:00 2001 From: varkor Date: Sat, 11 Aug 2018 21:25:48 +0100 Subject: [PATCH] Clean up and add extra tests --- src/librustc_passes/ast_validation.rs | 4 ++-- src/libsyntax/parse/parser.rs | 15 +++------------ src/test/compile-fail/no-patterns-in-args-2.rs | 4 ++-- .../compile-fail/no-patterns-in-args-macro.rs | 2 +- src/test/ui/E0642.rs | 15 ++++++++++----- src/test/ui/E0642.stderr | 16 ++++++++-------- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index e15dab404f47..0ea90e745319 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -342,10 +342,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.session.buffer_lint( lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY, trait_item.id, span, - "patterns aren't allowed in trait methods"); + "patterns aren't allowed in methods without bodies"); } else { struct_span_err!(self.session, span, E0642, - "patterns aren't allowed in trait methods").emit(); + "patterns aren't allowed in methods without bodies").emit(); } }); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 14026c5bede6..746e03d771a8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1754,16 +1754,7 @@ impl<'a> Parser<'a> { } else { debug!("parse_arg_general ident_to_pat"); - // If we see `ident :`, then we know that the argument is not just of the - // form `type`, which means we won't need to recover from parsing a - // pattern and so we don't need to store a parser snapshot. - let parser_snapshot_before_pat = if - self.look_ahead(1, |t| t.is_ident()) && - self.look_ahead(2, |t| t == &token::Colon) { - None - } else { - Some(self.clone()) - }; + let parser_snapshot_before_pat = self.clone(); // We're going to try parsing the argument as a pattern (even though it's not // allowed). This way we can provide better errors to the user. @@ -1777,7 +1768,7 @@ impl<'a> Parser<'a> { Ok((pat, ty)) => { let mut err = self.diagnostic().struct_span_err_with_code( pat.span, - "patterns aren't allowed in trait methods", + "patterns aren't allowed in methods without bodies", DiagnosticId::Error("E0642".into()), ); err.span_suggestion_short_with_applicability( @@ -1799,7 +1790,7 @@ impl<'a> Parser<'a> { err.cancel(); // Recover from attempting to parse the argument as a pattern. This means // the type is alone, with no name, e.g. `fn foo(u32)`. - mem::replace(self, parser_snapshot_before_pat.unwrap()); + mem::replace(self, parser_snapshot_before_pat); debug!("parse_arg_general ident_to_pat"); let ident = Ident::new(keywords::Invalid.name(), self.prev_span); let ty = self.parse_ty()?; diff --git a/src/test/compile-fail/no-patterns-in-args-2.rs b/src/test/compile-fail/no-patterns-in-args-2.rs index 80e696798011..4d2412c34a5f 100644 --- a/src/test/compile-fail/no-patterns-in-args-2.rs +++ b/src/test/compile-fail/no-patterns-in-args-2.rs @@ -11,9 +11,9 @@ #![deny(patterns_in_fns_without_body)] trait Tr { - fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in trait methods + fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in methods without bodies //~^ WARN was previously accepted - fn f2(&arg: u8); //~ ERROR patterns aren't allowed in trait methods + fn f2(&arg: u8); //~ ERROR patterns aren't allowed in methods without bodies fn g1(arg: u8); // OK fn g2(_: u8); // OK #[allow(anonymous_parameters)] diff --git a/src/test/compile-fail/no-patterns-in-args-macro.rs b/src/test/compile-fail/no-patterns-in-args-macro.rs index 546c40ecbd04..f85ce8f57ea7 100644 --- a/src/test/compile-fail/no-patterns-in-args-macro.rs +++ b/src/test/compile-fail/no-patterns-in-args-macro.rs @@ -30,7 +30,7 @@ mod bad_pat { m!((bad, pat)); //~^ ERROR patterns aren't allowed in function pointer types //~| ERROR patterns aren't allowed in foreign function declarations - //~| ERROR patterns aren't allowed in trait methods + //~| ERROR patterns aren't allowed in methods without bodies } fn main() {} diff --git a/src/test/ui/E0642.rs b/src/test/ui/E0642.rs index 837a9365271e..58ccfc56ab79 100644 --- a/src/test/ui/E0642.rs +++ b/src/test/ui/E0642.rs @@ -8,12 +8,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -trait Foo { - fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in trait methods -} +#[derive(Clone, Copy)] +struct S; -trait Bar { - fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods +trait T { + fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies + + fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies + + fn f(&ident: &S) {} // ok + fn g(&&ident: &&S) {} // ok + fn h(mut ident: S) {} // ok } fn main() {} diff --git a/src/test/ui/E0642.stderr b/src/test/ui/E0642.stderr index 1723e97b45e4..34c163e21097 100644 --- a/src/test/ui/E0642.stderr +++ b/src/test/ui/E0642.stderr @@ -1,21 +1,21 @@ -error[E0642]: patterns aren't allowed in trait methods - --> $DIR/E0642.rs:12:12 +error[E0642]: patterns aren't allowed in methods without bodies + --> $DIR/E0642.rs:15:12 | -LL | fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in trait methods +LL | fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies | ^^^^^^ help: give this argument a name or use an underscore to ignore it | -LL | fn foo(_: (i32, i32)); //~ ERROR patterns aren't allowed in trait methods +LL | fn foo(_: (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies | ^ -error[E0642]: patterns aren't allowed in trait methods - --> $DIR/E0642.rs:16:12 +error[E0642]: patterns aren't allowed in methods without bodies + --> $DIR/E0642.rs:17:12 | -LL | fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods +LL | fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies | ^^^^^^ help: give this argument a name or use an underscore to ignore it | -LL | fn bar(_: (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods +LL | fn bar(_: (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies | ^ error: aborting due to 2 previous errors