rust-lang / rust-lang/rust

"the trait bound is not satisfied" when using traits from associated type

Open
#138,119 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

I tried this code:

trait Trait1 {
    type Ty1;
}

trait Trait2 {
    type Ty2: Default + Into<u8>;
}

trait Trait3: Trait1 {
    type Ty3: Trait2<Ty2 = Self::Ty1>;
}

fn default<T: Trait3>() -> u8 {
    <<<T as Trait3>::Ty3 as Trait2>::Ty2 as Default>::default().into()
}

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=95dd0d2d1db3207a2a4c1b3ff5ec977a

I expected to see this happen: It should compile correctly, since Trait2::Ty2 implements Default + Into<u8>.

Instead, this happened:

error[E0277]: the trait bound `<T as Trait1>::Ty1: Default` is not satisfied
  --> src/lib.rs:14:6
   |
14 |     <<<T as Trait3>::Ty3 as Trait2>::Ty2 as Default>::default().into()
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `<T as Trait1>::Ty1`.
   |
help: consider further restricting the associated type
   |
13 | fn default<T: Trait3>() -> u8 where <T as Trait1>::Ty1: Default {
   |                               +++++++++++++++++++++++++++++++++

error[E0277]: the trait bound `u8: From<<T as Trait1>::Ty1>` is not satisfied
  --> src/lib.rs:14:65
   |
14 |     <<<T as Trait3>::Ty3 as Trait2>::Ty2 as Default>::default().into()
   |                                                                 ^^^^ the trait `From<<T as Trait1>::Ty1>` is not implemented for `u8`
   |
   = note: required for `<T as Trait1>::Ty1` to implement `Into<u8>`
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
   |
13 | fn default<T: Trait3>() -> u8 where u8: From<<T as Trait1>::Ty1> {
   |                               ++++++++++++++++++++++++++++++++++

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

Builds correctly if I remove Ty2 = Self::Ty1 from Trait3::Ty3 or add Default + Into<u8> to Trait1::Ty1

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.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 running the linked Rust Playground reproducer with the reported stable compiler version and compare behavior across compiler versions. Investigate trait solving for the associated-type equality Ty2 = Self::Ty1 and the resulting Default and Into<u8> bounds. Done means the valid example compiles without extra bounds and the behavior is covered by an appropriate compiler regression test.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.