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