Skip to content

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

Open
@eduardosm

Description

@eduardosm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions