rust-lang / rust-lang/rust

Confusing note about trait method signature.

Open
#117,844 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
pub struct Point<T> {
    x: T,
}
pub trait MyTrait1 {
    fn fun<T>(&self, p: Point<T>);
}
pub struct MyStruct;
impl<T> MyTrait1 for Point<T> {
    fn fun<T>(&self, p: Point<T>) {
        todo!();
    }
}

fn main() {
    todo!();
}
Current output
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
 --> test.rs:9:12
  |
8 | impl<T> MyTrait1 for Point<T> {
  |      - first use of `T`
9 |     fn fun<T>(&self, p: Point<T>) {
  |            ^ already used

error[E0053]: method `fun` has an incompatible type for trait
 --> test.rs:9:25
  |
8 | impl<T> MyTrait1 for Point<T> {
  |      - found type parameter
9 |     fn fun<T>(&self, p: Point<T>) {
  |            -            ^^^^^^^^
  |            |            |
  |            |            expected type parameter `T`, found a different type parameter `T`
  |            |            help: change the parameter type to match the trait: `Point<T>`
  |            expected type parameter
  |
note: type in trait
 --> test.rs:5:25
  |
5 |     fn fun<T>(&self, p: Point<T>);
  |                         ^^^^^^^^
  = note: expected signature `fn(&Point<T>, Point<T>)`
             found signature `fn(&Point<T>, Point<T>)`
  = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0053, E0403.
For more information about an error, try `rustc --explain E0053`.
Desired output
sry, have no idea...
Rationale and extra context

I was confused when I noticed the notes:

  • expected type parameter 'T', but a different type parameter 'T' was found.
  • note: expected signature fn(&Point<T>, Point<T>), but found signature fn(&Point<T>, Point<T>).

It tries to indicate that a different type is found. Based on its own explanation, however, the expected signature with type parameter T is the same as the found signature.

Is there anything that needs to be modified?

Other cases

No response

Anything else?

No response

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.

Research direction

Start with the Rust reproducer shown as test.rs and reproduce the E0403 and E0053 diagnostics. Read the emitted expected and found signatures alongside the type-parameter notes, then trace the diagnostic entry points for those errors. Done means the messages clearly distinguish the two type parameters or otherwise explain why the signatures differ.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.