Skip to content
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

Bounds Inference Failure with Nested Associated Types #121325

Closed
preston-evans98 opened this issue Feb 20, 2024 · 3 comments
Closed

Bounds Inference Failure with Nested Associated Types #121325

preston-evans98 opened this issue Feb 20, 2024 · 3 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-type-system Area: Type system C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@preston-evans98
Copy link

I tried this code:

pub trait BaseTrait {
    type MyType;
}

pub trait BaseTraitExt: BaseTrait
where Self::MyType: std::fmt::Debug {}

pub trait Container { 
    type Extended: BaseTraitExt;
}

I expected the code to compile successfully.

Instead, compilation fails with the following error:

error[E0277]: `<<Self as Container>::Extended as BaseTrait>::MyType` doesn't implement `Debug`
 --> src/main.rs:9:20
  |
9 |     type Extended: BaseTraitExt;
  |                    ^^^^^^^^^^^^ `<<Self as Container>::Extended as BaseTrait>::MyType` cannot be formatted using `{:?}` because it doesn't implement `Debug`
  |
  = help: the trait `Debug` is not implemented for `<<Self as Container>::Extended as BaseTrait>::MyType`
note: required by a bound in `BaseTraitExt`
 --> src/main.rs:6:21
  |
5 | pub trait BaseTraitExt: BaseTrait
  |           ------------ required by a bound in this trait
6 | where Self::MyType: std::fmt::Debug {}
  |                     ^^^^^^^^^^^^^^^ required by this bound in `BaseTraitExt`
help: consider further restricting the associated type
  |
8 | pub trait Container where <<Self as Container>::Extended as BaseTrait>::MyType: Debug { 
  |                     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6
@preston-evans98 preston-evans98 added the C-bug Category: This is a bug. label Feb 20, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 20, 2024
@jieyouxu jieyouxu added A-type-system Area: Type system A-associated-items Area: Associated items (types, constants & functions) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 20, 2024
@kadiwa4
Copy link
Contributor

kadiwa4 commented Feb 20, 2024

Very similar to #103387 and #109325

@compiler-errors
Copy link
Member

Yes, this is a duplicate of both of these.

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
@preston-evans98
Copy link
Author

In case anyone comes across this issue, here's the workaround (thanks to Eric Langlois on StackOverflow).

pub trait BaseTrait {
    type MyType;
}
pub trait MyTypeExt: core::fmt::Debug {}
trait PrivateHelper: BaseTrait<MyType = <Self as PrivateHelper>::MyType> {
    type MyType: MyTypeExt;
}
trait BaseTraitExt: PrivateHelper {}

impl<T: core::fmt::Debug> MyTypeExt for T {}
impl<T> PrivateHelper for T
where
    T: BaseTrait,
    T::MyType: MyTypeExt,
{
    type MyType = <T as BaseTrait>::MyType;
}
impl<T> BaseTraitExt for T where T: PrivateHelper {}

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-type-system Area: Type system C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants