rust-lang / rust-lang/rust

Impl coherence false negative for associated types.

Open
#148,579 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

use std::ops::{Add, Mul};

pub struct Generic<A>(A);

impl<A, B> Mul<Generic<B>> for Generic<A>
where
    A: Add<B>,
{
    type Output = Generic<<A as Add<B>>::Output>;

    fn mul(self, rhs: Generic<B>) -> Self::Output {
        Generic(self.0.add(rhs.0))
    }
}

type A = <Generic<i32> as Mul<Generic<i32>>>::Output;
type B = <Generic<u32> as Mul<Generic<u32>>>::Output;

trait Trait {}

impl Trait for A {}

impl Trait for B {}

playground

I expected to see this happen: code to compile.

Associated type Mul::Output should expand to different concrete types - Generic<i32> and Generic<u32>, and as such impls should be coherent.

Instead, this happened: compiler reports conflicting implementations of Trait

error[E0119]: conflicting implementations of trait `Trait` for type `<Generic<i32> as Mul>::Output`
  --> src/mini.rs:23:1
   |
21 | impl Trait for A {}
   | ---------------- first implementation here
22 |
23 | impl Trait for B {}
   | ^^^^^^^^^^^^^^^^ conflicting implementation for `<Generic<i32> as Mul>::Output`

I've looked for similar bug reports, most similar one I've found was #99940. Discussion there however takes place in the context of intercrate interactions, here everything happens locally.

Here's a reduced version of the code in which I originally encountered this problem, playground.

Meta

The issue persists on beta and nightly.

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: aarch64-apple-darwin
release: 1.89.0
LLVM version: 20.1.7

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 by reproducing the reduced example from the issue and compare the compiler's behavior on stable, beta, and nightly. Trace the coherence check for the two associated-type implementations; done means the compiler accepts the impls because their associated outputs resolve to distinct concrete types.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.