Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TimNN opened this issue May 31, 2025 · 1 comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@TimNN
Copy link
Contributor

TimNN commented May 31, 2025

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)
@TimNN TimNN added the C-bug Category: This is a bug. label May 31, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 31, 2025
@QuineDot
Copy link

QuineDot commented Jun 1, 2025

You can also make the associated type part of the super trait bound.

pub trait Helper: Super<X = ()> {}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=11562fb9c7a961b5cebbf05b4868ab24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

3 participants