Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
eduardosm opened this issue Mar 6, 2025 · 1 comment
Open
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@eduardosm
Copy link
Contributor

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
@eduardosm eduardosm added the C-bug Category: This is a bug. label Mar 6, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 6, 2025
@lolbinarycat lolbinarycat added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 10, 2025
@fmease fmease added T-types Relevant to the types team, which will review and decide on the PR/issue. A-associated-items Area: Associated items (types, constants & functions) and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 18, 2025
@fmease
Copy link
Member

fmease commented May 18, 2025

Basically duplicate of one of #109845, #137185, …

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants