"lifetime parameter is not constrained..." when using lifetime param to derive assoc. type from reference type trait impl

db7f82d
Opened by Collin J. Sutton at 2024-12-21 05:10:35

This works (playpen):

use std::marker::PhantomData;
trait X {
    type Q;
}
trait Y { } 

struct Z<T> { t: PhantomData<T> }

impl<'a, T> X for Z<T> where T: 'a, &'a T: Y {
    type Q = u8;
}

If instead we add an associated type Z to Y and try to write type Q = <&'a T as Y>::Z; in the X implementation, it fails with "error[E0207]: the lifetime parameter 'a is not constrained by the impl trait, self type, or predicates" (playpen:

use std::marker::PhantomData;
trait X {
    type Q;
}
trait Y {
    type Z;
} 

struct Z<T> { t: PhantomData<T> }

impl<'a, T> X for Z<T> where T: 'a, &'a T: Y {
    type Q = <&'a T as Y>::Z;
}

Rust version: doesn't seem to matter; all of stable, beta, and nightly on play.rlo produce the same results.

Thanks to @QuietMisdreavus on IRC for helping with the reduced example.

  1. Triage: no change

    Steve Klabnik at 2020-01-09 14:14:17

  2. According to the Error Codes Index, this is working as intended.

    Any unconstrained lifetime parameter of an impl is not supported if the lifetime parameter is used by an associated type.

    When the lifetime is constrained, it builds.

    Ben Schulz at 2023-02-13 15:06:35