rust-lang / rust-lang/rust

Compiler can't tell that two types are equal in a blanket supertrait impl

Open
#129,846 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

trait TraitA: TraitB {
    type Foo;
    const FOO: Self::Foo;
}

trait TraitB {
    type Bar;
    const BAR: Self::Bar;
}

impl<T: TraitA> TraitB for T {
    type Bar = <T as TraitA>::Foo;
    const BAR: <T as TraitB>::Bar = <T as TraitA>::FOO;
}

I expected the code to compile. Instead, I got the following error:

error[E0308]: mismatched types
  --> src/lib.rs:13:37
   |
13 |     const BAR: <T as TraitB>::Bar = <T as TraitA>::FOO;
   |                                     ^^^^^^^^^^^^^^^^^^ expected `TraitB::Bar`, found `TraitA::Foo`
   |
   = note: expected associated type `<T as TraitB>::Bar`
              found associated type `<T as TraitA>::Foo`
   = note: an associated type was expected, but a different one was found

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error

Note that removing the supertrait requirement causes the code to compile:

Code without supertrait requirement
trait TraitA {
    type Foo;
    const FOO: Self::Foo;
}

trait TraitB {
    type Bar;
    const BAR: Self::Bar;
}

impl<T: TraitA> TraitB for T {
    type Bar = <T as TraitA>::Foo;
    const BAR: <T as TraitB>::Bar = <T as TraitA>::FOO;
}

Also, adding the associated type requirement to the supertrait requirement causes the code to compile:

Code with the associated type requirement in the supertrait requirement
trait TraitA: TraitB<Bar = Self::Foo> {
    type Foo;
    const FOO: Self::Foo;
}

trait TraitB {
    type Bar;
    const BAR: Self::Bar;
}

impl<T: TraitA> TraitB for T {
    type Bar = <T as TraitA>::Foo;
    const BAR: <T as TraitB>::Bar = <T as TraitA>::FOO;
}

Discovered by esper8989 on the community discord.

Meta

The issue reproduces on the playground on "Stable version: 1.80.1" and "Nightly version: 1.82.0-nightly (2024-08-31 a7399ba69d37b019677a)"

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 reproducer in the issue and compare the failing blanket supertrait implementation with the two variants that compile. Investigate how the compiler handles associated-type equality through supertraits; done means the original case is handled consistently with the explicitly constrained and non-supertrait cases.

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.