rust-lang / rust-lang/rust

Associated Type Not Resolved Inside Trait Bound

Open
#156,998 1 comment 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 (playground link):

#![allow(unused)]

use std::fmt::Debug;
use std::marker::PhantomData;

struct Context<Key: Base> {
    key: PhantomData<Key>,
    output: PhantomData<Key::Output>,
}

trait Base {
    type Output;
}

trait Producer<Key: Base>: Base {
    fn produce(&self, ctx: &Context<Key>) -> Self::Output;
}

fn query<KSmall, KLarge>(
    ctx: &Context<KLarge>,
    key: KSmall,
) -> KSmall::Output
where
    KSmall: Base + Into<KLarge>,
    KLarge: Producer<KLarge>,
    KLarge::Output: TryInto<KSmall::Output> + Debug,
    <KLarge::Output as TryInto<KSmall::Output>>::Error: Debug,
{
    let key = key.into();
    let output = key.produce(ctx);
    output.try_into().unwrap()
}

struct Fib(u32);

impl Base for Fib {
    type Output = u32;
}
impl<KLarge> Producer<KLarge> for Fib
where
    Fib: Into<KLarge>,
    KLarge: Producer<KLarge>,
    KLarge::Output: TryInto<<Fib as Base>::Output>, // doesn't work??
    KLarge::Output: Debug,
    <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug,
{
    fn produce(&self, ctx: &Context<KLarge>) -> u32 {
        let n = self.0;
        if n == 0 || n == 1 {
            return 1;
        }
        
        let n_1 = query(ctx, Fib(n-1));
        let n_2 = query(ctx, Fib(n-2));
        
        n_1 + n_2
    }
}

I expected it to build without issue, because the bounds on the Producer<KLarge> for Fib implementation are exactly the same as the bounds on the query function.

Instead, I got the following output:

error[E0277]: the trait bound `u32: From<<KLarge as Base>::Output>` is not satisfied
  --> src/lib.rs:39:1
   |
39 | / impl<KLarge> Producer<KLarge> for Fib
40 | | where
41 | |     Fib: Into<KLarge>,
42 | |     KLarge: Producer<KLarge>,
...  |
46 | |     <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug,
   | |_____________________________________________________________________^ the trait `From<<KLarge as Base>::Output>` is not implemented for `u32`
   |
   = note: required for `<KLarge as Base>::Output` to implement `Into<u32>`
   = note: required for `u32` to implement `TryFrom<<KLarge as Base>::Output>`
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
   |
46 |     <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug, u32: From<<KLarge as Base>::Output>
   |                                                                       +++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `u32: From<<KLarge as Base>::Output>` is not satisfied
  --> src/lib.rs:48:5
   |
48 |     fn produce(&self, ctx: &Context<KLarge>) -> u32 {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<<KLarge as Base>::Output>` is not implemented for `u32`
   |
   = note: required for `<KLarge as Base>::Output` to implement `Into<u32>`
   = note: required for `u32` to implement `TryFrom<<KLarge as Base>::Output>`
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
   |
46 |     <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug, u32: From<<KLarge as Base>::Output>
   |                                                                       +++++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `u32: From<<KLarge as Base>::Output>` is not satisfied
  --> src/lib.rs:46:64
   |
46 |     <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug,
   |                                                                ^^^^^ the trait `From<<KLarge as Base>::Output>` is not implemented for `u32`
   |
   = note: required for `<KLarge as Base>::Output` to implement `Into<u32>`
   = note: required for `u32` to implement `TryFrom<<KLarge as Base>::Output>`
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
   |
46 |     <KLarge::Output as TryInto<<Fib as Base>::Output>>::Error: Debug, u32: From<<KLarge as Base>::Output>
   |                                                                       +++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.

What's interesting about this error is that, if we change the // doesn't work?? line to instead be KLarge::Output: TryInto<u32>, the code does compile without issue. This is what led me to the hypothesis in my issue title: that for some reason, the associated type isn't getting resolved when used as a type argument in a trait bound. Of course, this could be very off-the-mark as to the real root cause, but that's what I observe at least.

Meta

rustc --version --verbose:

rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: x86_64-unknown-linux-gnu
release: 1.95.0
LLVM version: 22.1.2

(in the playground, I tested this also reproduces on 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 with the linked Playground reproduction and the src/lib.rs locations shown in the diagnostics. Compare the failing TryInto<::Output> bound with the compiling TryInto variant, then inspect the reported E0277 requirements. Done means establishing whether associated-type projection in this trait bound is a compiler bug or an expected limitation, with the behavior clearly documented.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.