Skip to content

Adding where clause already filled relax traits bounds in impl #141721

Open
@supersurviveur

Description

@supersurviveur

I tried this code:

trait Foo {
    type B;
}

struct A<T> {
    a: T
}

trait Bar<T> {
    fn bar(a: <A<T> as Foo>::B) {}
}

impl<T> Bar<T> for T
where
    A<T>: Foo
{
    fn bar(a: u32) {}
}

impl<T> Foo for A<T> {
    type B = u32;
}

I expected it to compile, but instead I get the error

error[E0053]: method `bar` has an incompatible type for trait
  --> src/main.rs:17:15
   |
17 |     fn bar(a: u32) {}
   |               ^^^ expected associated type, found `u32`
   |
note: type in trait
  --> src/main.rs:10:15
   |
10 |     fn bar(a: <A<T> as Foo>::B) {}
   |               ^^^^^^^^^^^^^^^^
   = note: expected signature `fn(<A<T> as Foo>::B)`
              found signature `fn(u32)`
help: change the parameter type to match the trait
   |
17 -     fn bar(a: u32) {}
17 +     fn bar(a: <A<T> as Foo>::B) {}

The error disappears when I remove the where clause in Bar implementation.
I expected both code to compile since the where clause should only add trait bounds, and A<T>::B is u32 in both cases.

rustc --version --verbose:

rustc 1.89.0-nightly (ce7e97f73 2025-05-11)
binary: rustc
commit-hash: ce7e97f7371af47e0786f74aa169f6ac9473ff4e
commit-date: 2025-05-11
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions