rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.99k stars 12.54k forks source link

Overflow evaluating the requirement when using GATs #112097

Open momvart opened 1 year ago

momvart commented 1 year ago

While #39959 and #111313 are discussing the same issue, in this case, it looks like there's a problem with GATs (generic associated types) or probably HRTBs (higher-rank trait bounds). Try removing the generic lifetime 'a and the problem will be solved.

Code

trait Foo {
    type A<'a>;
}

trait Alias
where
    for<'a> Self: Foo<A<'a> = <Self as Alias>::AliasA<'a>>,
{
    type AliasA<'a>: From<u8>;
}

impl<T> Alias for T
where
    T: Foo,
    for<'a> <T as Foo>::A<'a>: From<u8>,
{
    type AliasA<'a> = <T as Foo>::A<'a>;
}

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (e77366b57 2023-05-16)
binary: rustc
commit-hash: e77366b57b799dfa3ce1fcb850c068723a3213ee
commit-date: 2023-05-16
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

Error output

error[E0275]: overflow evaluating the requirement `Self: Alias`
  --> examples/generic_error2.rs:5:1
   |
5  | trait Alias
   | ^^^^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`generic_error2`)
note: required for `Self` to implement `Alias`
  --> examples/generic_error2.rs:12:9
   |
12 | impl<T> Alias for T
   |         ^^^^^     ^
...
15 |     for<'a> <T as Foo>::A<'a>: From<u8>,
   |                                -------- unsatisfied trait bound introduced here
   = note: 63 redundant requirements hidden
   = note: required for `Self` to implement `Alias`
fmease commented 1 year ago

@rustbot label -I-ICE

npuichigo commented 1 year ago

When built with rustc +nightly -Ztrait-solver=next, the error becomes

error[E0271]: type mismatch resolving `<T as Foo>::A<'a> == <T as Alias>::AliasA<'a>`
  --> main.rs:17:23
   |
17 |     type AliasA<'a> = <T as Foo>::A<'a>;
   |                       ^^^^^^^^^^^^^^^^^ types differ
   |
note: required by a bound in `Alias`
  --> main.rs:7:23
   |
5  | trait Alias
   |       ----- required by a bound in this trait
6  | where
7  |     for<'a> Self: Foo<A<'a> = <Self as Alias>::AliasA<'a>>,
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Alias`

error: aborting due to previous error

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