Clean up and add extra tests
This commit is contained in:
parent
49e9c5fe90
commit
5c814e2e4e
6 changed files with 26 additions and 30 deletions
|
|
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()?;
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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() {}
|
||||
|
|
|
|||
|
|
@ -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() {}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue