Skip to content

Commit 5c814e2

Browse files
committed
Clean up and add extra tests
1 parent 49e9c5f commit 5c814e2

File tree

6 files changed

+26
-30
lines changed

6 files changed

+26
-30
lines changed

src/librustc_passes/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
342342
self.session.buffer_lint(
343343
lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY,
344344
trait_item.id, span,
345-
"patterns aren't allowed in trait methods");
345+
"patterns aren't allowed in methods without bodies");
346346
} else {
347347
struct_span_err!(self.session, span, E0642,
348-
"patterns aren't allowed in trait methods").emit();
348+
"patterns aren't allowed in methods without bodies").emit();
349349
}
350350
});
351351
}

src/libsyntax/parse/parser.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1754,16 +1754,7 @@ impl<'a> Parser<'a> {
17541754
} else {
17551755
debug!("parse_arg_general ident_to_pat");
17561756

1757-
// If we see `ident :`, then we know that the argument is not just of the
1758-
// form `type`, which means we won't need to recover from parsing a
1759-
// pattern and so we don't need to store a parser snapshot.
1760-
let parser_snapshot_before_pat = if
1761-
self.look_ahead(1, |t| t.is_ident()) &&
1762-
self.look_ahead(2, |t| t == &token::Colon) {
1763-
None
1764-
} else {
1765-
Some(self.clone())
1766-
};
1757+
let parser_snapshot_before_pat = self.clone();
17671758

17681759
// We're going to try parsing the argument as a pattern (even though it's not
17691760
// allowed). This way we can provide better errors to the user.
@@ -1777,7 +1768,7 @@ impl<'a> Parser<'a> {
17771768
Ok((pat, ty)) => {
17781769
let mut err = self.diagnostic().struct_span_err_with_code(
17791770
pat.span,
1780-
"patterns aren't allowed in trait methods",
1771+
"patterns aren't allowed in methods without bodies",
17811772
DiagnosticId::Error("E0642".into()),
17821773
);
17831774
err.span_suggestion_short_with_applicability(
@@ -1799,7 +1790,7 @@ impl<'a> Parser<'a> {
17991790
err.cancel();
18001791
// Recover from attempting to parse the argument as a pattern. This means
18011792
// the type is alone, with no name, e.g. `fn foo(u32)`.
1802-
mem::replace(self, parser_snapshot_before_pat.unwrap());
1793+
mem::replace(self, parser_snapshot_before_pat);
18031794
debug!("parse_arg_general ident_to_pat");
18041795
let ident = Ident::new(keywords::Invalid.name(), self.prev_span);
18051796
let ty = self.parse_ty()?;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#![deny(patterns_in_fns_without_body)]
1212

1313
trait Tr {
14-
fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in trait methods
14+
fn f1(mut arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
1515
//~^ WARN was previously accepted
16-
fn f2(&arg: u8); //~ ERROR patterns aren't allowed in trait methods
16+
fn f2(&arg: u8); //~ ERROR patterns aren't allowed in methods without bodies
1717
fn g1(arg: u8); // OK
1818
fn g2(_: u8); // OK
1919
#[allow(anonymous_parameters)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod bad_pat {
3030
m!((bad, pat));
3131
//~^ ERROR patterns aren't allowed in function pointer types
3232
//~| ERROR patterns aren't allowed in foreign function declarations
33-
//~| ERROR patterns aren't allowed in trait methods
33+
//~| ERROR patterns aren't allowed in methods without bodies
3434
}
3535

3636
fn main() {}

src/test/ui/E0642.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
trait Foo {
12-
fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in trait methods
13-
}
11+
#[derive(Clone, Copy)]
12+
struct S;
13+
14+
trait T {
15+
fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
16+
17+
fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
1418

15-
trait Bar {
16-
fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods
19+
fn f(&ident: &S) {} // ok
20+
fn g(&&ident: &&S) {} // ok
21+
fn h(mut ident: S) {} // ok
1722
}
1823

1924
fn main() {}

src/test/ui/E0642.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0642]: patterns aren't allowed in trait methods
2-
--> $DIR/E0642.rs:12:12
1+
error[E0642]: patterns aren't allowed in methods without bodies
2+
--> $DIR/E0642.rs:15:12
33
|
4-
LL | fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in trait methods
4+
LL | fn foo((x, y): (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
55
| ^^^^^^
66
help: give this argument a name or use an underscore to ignore it
77
|
8-
LL | fn foo(_: (i32, i32)); //~ ERROR patterns aren't allowed in trait methods
8+
LL | fn foo(_: (i32, i32)); //~ ERROR patterns aren't allowed in methods without bodies
99
| ^
1010

11-
error[E0642]: patterns aren't allowed in trait methods
12-
--> $DIR/E0642.rs:16:12
11+
error[E0642]: patterns aren't allowed in methods without bodies
12+
--> $DIR/E0642.rs:17:12
1313
|
14-
LL | fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods
14+
LL | fn bar((x, y): (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
1515
| ^^^^^^
1616
help: give this argument a name or use an underscore to ignore it
1717
|
18-
LL | fn bar(_: (i32, i32)) {} //~ ERROR patterns aren't allowed in trait methods
18+
LL | fn bar(_: (i32, i32)) {} //~ ERROR patterns aren't allowed in methods without bodies
1919
| ^
2020

2121
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)