rust-lang / rust-lang/rust

(edition 2015) "patterns aren't allowed in methods without bodies" even though the method has a body

Open
#143,113 1 comment 0 reactions 1 assignee View on GitHub

@Ezrashaw is already working on this.

Since Jul 4, 2025.

A-diagnostics A-edition-2015 D-edition D-incorrect T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
trait T {
    fn f((): ()) {
        println!("hello world, this is a method body")
    }
}
type P = fn((): ());
Current output
error[E0642]: patterns aren't allowed in methods without bodies
 --> src/main.rs:2:10
  |
2 |     fn f((): ()) {
  |          ^^
  |
help: give this argument a name or use an underscore to ignore it
  |
2 -     fn f((): ()) {
2 +     fn f(_: ()) {
  |

error[E0642]: patterns aren't allowed in methods without bodies
 --> src/main.rs:6:13
  |
6 | type P = fn((): ());
  |             ^^
  |
help: give this argument a name or use an underscore to ignore it
  |
6 - type P = fn((): ());
6 + type P = fn(_: ());
  |
Desired output
error[E0642]: patterns aren't allowed in trait methods in this edition (Rust 2015)
 --> src/main.rs:2:10
  |
2 |     fn f((): ()) {
  |          ^^
  |
note: this code is perfectly fine in Rust 2018 and onwards
help: in this edition, give this argument a name or use an underscore to ignore it
  |
2 -     fn f((): ()) {
2 +     fn f(_: ()) {
  |

error[E0561]: patterns aren't allowed in function pointer types
 --> src/main.rs:6:13
  |
6 | type P = fn((): ());
  |             ^^
Rationale and extra context

this error only exists because, in edition 2015, trait functions can be like fn f(()); without even giving a pattern. to account for the syntactic ambiguity of a "pattern or a type in this position", the patterns are very restricted. but, even if a body is provided, the pattern is still optional. fn f(u32) {} is perfectly fine in this edition, even though you can't name that parameter in the function body.

the error message incorrectly assumes that this ambiguity can only exist if there is no method body, when in fact it exists in both cases.

additionally, the same error message is printed in a function pointer type like fn((): ()), where it is ALSO wrong. however, fn(&_: ()) (or with any pattern that is allowed in an edition-2015-trait-body) will print a much better error message (patterns aren't allowed in function pointer types); so the fn((): ()) case should be changed to match the fn(&_: ()) case.

Rust Version
rustc 1.90.0-nightly (b03b3a7ec 2025-06-26)
binary: rustc
commit-hash: b03b3a7ec92682be2917540b679478d41c95a30c
commit-date: 2025-06-26
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.