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

Mundane HRTB causes constraint satisfaction failure #97487

Open
CJKay opened this issue May 28, 2022 · 0 comments
Open

Mundane HRTB causes constraint satisfaction failure #97487

CJKay opened this issue May 28, 2022 · 0 comments
Labels
C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@CJKay
Copy link

CJKay commented May 28, 2022

I'm trying to understand an error I'm seeing with some project code, which I've minimised down to a test case that I'm trying to get my head around.

The following two snippets build perfectly fine, as I would expect:

trait A {
    type X;
}

trait B: A<X = u32> {
    /* ... */
}

impl<T> A for T where T: B {
    type X = u32;
}

(https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=052cefcd91b153d1958b8452a9ddbfe7)

trait A {
    type X;
}

trait B: for<'a> A {
    /* ... */
}

impl<T> A for T where T: B {
    type X = u32;
}

(https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=302cd29650a304e40d8ca65bb9d7521d)

However, this third snippet, which combines both the mundane HRTB and the associated type requirement, fails with an obscure error:

trait A {
    type X;
}

trait B: for<'a> A<X = u32> {
    /* ... */
}

impl<T> A for T where T: B {
    type X = u32;
}

(https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=56ad9194355f412f7b22c121ff6479d3)

Giving:

error[[E0284]](https://doc.rust-lang.org/nightly/error-index.html#E0284): type annotations needed: cannot satisfy `<T as A>::X == _`
  --> src/lib.rs:10:14
   |
10 |     type X = u32;
   |              ^^^ cannot satisfy `<T as A>::X == _`

For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground` due to previous error

What is it about the HRTB that causes the compiler to suddenly become unable to determine that the type of X must be u32? Am I fundamentally misunderstanding HRTBs?

I ran into this because I needed to impl a supertrait with GATs on a type with an existing impl of the sub-trait, a la:

#![feature(generic_associated_types)]

trait A {
    type X<'a>;
}

trait B: for<'a> A<X<'a> = Self::Y<'a>> {
    type Y<'a>;
}

impl<T> A for T where T: B {
    type X<'a> = T::Y<'a>;
}

(https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=74172d3948e66032f917f18fc8cfaf2a)

@CJKay CJKay added the C-bug Category: This is a bug. label May 28, 2022
@compiler-errors compiler-errors added F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs 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

2 participants