rust-lang / rust-lang/rust

Associated Type Equality Bounds are not properly resolved

Open
#139,682 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello!

Given the following definitions (minimized from my actual use case):

trait DeviceIdContext {
    type DeviceId;
}

trait BindingsContext<E> {}

trait EventContext<E> {
    type OtherEventRepr;
}

trait CoreContext<BC>: DeviceIdContext + EventContext<Self::DeviceId> {}

struct Foo<D> {
    d: D,
}

I'd like to be able to write the following implementation:

impl<D> Foo<D> {
    fn foo<BC, CC>(self, _core_ctx: CC, _bindings_ctx: BC)
    where 
        BC: BindingsContext<CC::OtherEventRepr>,
        CC: CoreContext<BC, DeviceId = D> {}
}

Compilation fails with error:

CC: EventContext<D> is not satisfied

However, that's incorrect. CC must implement EventContext. I've bounded its associated type DeviceId to equal D. Along with the compilation error, comes the suggestion to add an explicit EventContext<D> bound. That would look like the following:

impl<D> Foo<D> {
    fn foo<BC, CC>(self, _core_ctx: CC, _bindings_ctx: BC)
    where 
        BC: BindingsContext<CC::OtherEventRepr>,
        CC: CoreContext<BC, DeviceId = D> + EventContext<D> {}
}

That still fails to compile, this time for a different reason:

ambiguous associated type OtherEventRepr in bounds of CC
| ambiguous OtherEventRepr from EventContext<D>
| ambiguous OtherEventRepr from EventContext<<CC as DeviceIdContext>::DeviceId>

Interestingly, If I rework this implementation to be a free function, everything compiles:

fn foo<
    BC: BindingsContext<CC::OtherEventRepr>,
    CC: CoreContext<BC>
>(core_ctx: CC, bindings_ctx: BC, foo: Foo<CC::DeviceId>) {}

It seems to me that the DeviceId = D bound I've written isn't able to be reasoned about properly in examples 1 & 2. Checkout this Rust Playground with all the examples from above: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=91049b46781fb054e583994d7dde5966

I observe this issue on Stable, Beta & Nightly.

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 running the minimized examples in the linked Rust Playground, comparing the method implementation with the free-function version. Investigate why the DeviceId = D equality bound is not used to resolve EventContext<D> and why adding that bound makes OtherEventRepr ambiguous. Done means the reported method form compiles without redundant bounds or ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.