rust-lang / rust-lang/rust

Blanket impl of super trait with method returning associated type: "incompatible type for trait"

Open
#141,807 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-type-system C-discussion T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

pub trait Super {
    type X;

    // Change return type to `()` and everything is fine.
    fn method(&self) -> Self::X;
}

// Remove the `: Super` and everything is fine.
pub trait Helper: Super {}

impl<T: Helper> Super for T {
    type X = ();

    fn method(&self) {}
}

I expected to see this happen: The code compiles

Instead, this happened: The code fails to compile.

Maybe this is working as intended, but intuitively I wouldn't have expected the Helper: Super to make a difference for the impl Super for T.

The type error is a lot more confusing if the associated type is implicit, (i.e. -> impl Future or async fn), because that results in "expected signature fn(&_) -> X, found signature fn(&_) -> X"

error[E0053]: method `method` has an incompatible type for trait
  --> src/lib.rs:14:21
   |
14 |     fn method(&self) {}
   |                     ^ expected associated type, found `()`
   |
note: type in trait
  --> src/lib.rs:5:25
   |
5  |     fn method(&self) -> Self::X;
   |                         ^^^^^^^
   = note: expected signature `fn(&_) -> <T as Super>::X`
              found signature `fn(&_) -> ()`
Meta

rustc --version --verbose:

Build using the Nightly version: 1.89.0-nightly(2025-05-30 70b3f4666e24ce22fc32)

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 minimal reproduction in src/lib.rs, then compare the compiler behavior with the Helper: Super bound removed and with the method returning (). Investigate the associated-type and blanket-impl diagnostics, including the implicit return-type case described in the issue. Done means the intended behavior is confirmed or corrected and the incompatible-type error is accurately handled.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.