-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
I encountered E0642 which disallows patterns in functions without a body, particularly trait methods. It was not immediately clear to me that this meant one could still implement a trait method using patterns. Due to the fact that most trait implementations don't stray far from the provided argument names, it took me a moment to remember that the argument names in trait definitions are essentially filler.
I would expect others may have similar confusion with how to handle the error, and it could be avoided if the error explanation reminded the user that this only applies to the trait definition, not its implementations.
Currently, rustc --explain E0642
outputs:
Trait methods currently cannot take patterns as arguments.
Erroneous code example:
trait Foo {
fn foo((x, y): (i32, i32)); // error: patterns aren't allowed
// in trait methods
}
You can instead use a single name for the argument:
trait Foo {
fn foo(x_and_y: (i32, i32)); // ok!
}
I believe an addition like the following would aid clarity:
And you can use patterns in its implementations:
impl Foo for Bar {
fn foo((x, y): (i32, i32)) { /* ... */ } // ok!
}
compiler-errors and Ezrashaw
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.