Skip to content

Trait method signature mismatch could suggest changing the signature inline #106999

@euclio

Description

@euclio

Code

use serde::Serialize;

trait Foo {
    fn bar(baz: i32, quux: String);
}

impl Foo for () {
    fn bar(baz: i32) {
        
    }
}

struct Bar;

impl Serialize for Bar {
    fn serialize<S>(&self) {
        
    }
}

fn main() {}

Current output

error[E0050]: method `bar` has 1 parameter but the declaration in trait `Foo::bar` has 2
 --> src/main.rs:8:17
  |
4 |     fn bar(baz: i32, quux: String);
  |                 ----------------- trait requires 2 parameters
...
8 |     fn bar(baz: i32) {
  |                 ^^^ expected 2 parameters, found 1

error[E0050]: method `serialize` has 1 parameter but the declaration in trait `serialize` has 2
  --> src/main.rs:16:21
   |
16 |     fn serialize<S>(&self) {
   |                     ^^^^^ expected 2 parameters, found 1
   |
   = note: `serialize` from trait: `fn(&Self, S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>`

For more information about this error, try `rustc --explain E0050`.

Desired output

error[E0050]: method `bar` has 1 parameter but the declaration in trait `Foo::bar` has 2
 --> src/main.rs:8:17
  |
4 |     fn bar(baz: i32, quux: String);
  |                 ----------------- trait requires 2 parameters
...
8 |     fn bar(baz: i32) {
  |                 ^^^ expected 2 parameters, found 1
help: modify the signature to match the trait definition
  |
8 |     fn bar(baz: i32, quux: String) {
  |                    ~~~~~~~~~~~~~~

error[E0050]: method `serialize` has 1 parameter but the declaration in trait `serialize` has 2
  --> src/main.rs:16:21
   |
16 |     fn serialize<S>(&self) {
   |                     ^^^^^ expected 2 parameters, found 1
   |
   = help: modify the signature to match the trait definition
   |
16 |     fn serialize(&self, serializer: S) -> Result<S::Ok, S::Error> {
   |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0050`.

Rationale and extra context

The current message is slightly different for local and external traits, but the idea is the same: instead of just writing out the signature, the message could instead suggest adding parameters to exactly match the trait definition.

This is similar to what we already do when a function call expression does not match the function parameters.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions