Is this a lifetime BUG in compiler?

⚓ Rust    📅 2026-07-14    👤 surdeus    👁️ 1      

surdeus

For the following code:

trait L1<'a> {
    type A1;
    type B1;
}

trait L2<'a>: L1<'a, A1 = Self::A2, B1 = Self::B2> {
    type A2;
    type B2;
}

impl<'a, T, A, B> L2<'a> for T
where
    T: L1<'a, A1 = A, B1 = B>,
{
    type A2 = A;
    type B2 = B;
}

trait L3: for<'a> L2<'a, A2 = Self::A3<'a>, B2 = Self::B3> {
    type A3<'a>;
    type B3;
}

impl<T, B> L3 for T
where
    T: for<'a> L2<'a, B2 = B>,
{
    type A3<'a> = <T as L2<'a>>::A2;
    type B3 = B;
}

When it is compiled, an error will be reported:

error[E0275]: overflow evaluating the requirement `for<'a> Self: L2<'a>`
  --> src/lib.rs:34:5
   |
34 |     trait L3: for<'a> L2<'a, A2 = Self::A3<'a>, B2 = Self::B3> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`playground`)
note: required for `Self` to implement `L3`
  --> src/lib.rs:38:16
   |
38 |     impl<T, B> L3 for T
   |                ^^     ^
39 |     where
40 |         T: for<'a> L2<'a, B2 = B>,
   |            ---------------------- unsatisfied trait bound introduced here
note: required for `Self` to implement `for<'a> L2<'a>`
  --> src/lib.rs:26:23
   |
26 |     impl<'a, T, A, B> L2<'a> for T
   |                       ^^^^^^     ^
27 |     where
28 |         T: L1<'a, A1 = A, B1 = B>,
   |                   ------ unsatisfied trait bound introduced here
   = note: 61 redundant requirements hidden
   = note: required for `Self` to implement `L3`

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

And when the last implementation condition is changed from L2 to L1:

impl<T, B> L3 for T
where
    T: for<'a> L1<'a, B1 = B>,
{
    type A3<'a> = <T as L1<'a>>::A1;
    type B3 = B;
}

An error will be reported as:

error[E0308]: mismatched types
  --> src/lib.rs:38:23
   |
38 |     impl<T, B> L3 for T
   |                       ^ lifetime mismatch
   |
   = note: expected associated type `<T as L1<'a>>::A1`
              found associated type `<T as L1<'_>>::A1`
   = note: an associated type was expected, but a different one was found
note: the lifetime requirement is introduced here
  --> src/lib.rs:34:15
   |
34 |     trait L3: for<'a> L2<'a, A2 = Self::A3<'a>, B2 = Self::B3> {
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

for<'a> currently works like "for any 'a" and "∀'a". It seems that the type system has some BUGs for the lifetime in for<'a>.

1 post - 1 participant

Read full topic

🏷️ Rust_feed