Closed
Description
This is related to #20890 but is definitely not the same, because in this case adding the requested constraints as a where clause does nothing.
fn main() { }
trait InterestingTrait<'a> {
type Foo: 'static;
}
trait BlandTrait {
type Foo: 'static;
}
fn a<'a, T: InterestingTrait<'a>>(value: T::Foo) where T::Foo: 'static {
assert_lifetime::<'a>(value);
assert_lifetime::<'static>(value);
}
fn b<T: InterestingTrait<'static>>(value: T::Foo) where T::Foo: 'static {
assert_lifetime::<'static>(value);
}
fn c<T: BlandTrait>(value: T::Foo) where T::Foo: 'static {
assert_lifetime::<'static>(value);
}
fn assert_lifetime<'a, T: 'a>(_value: T) { }
This should all build fine (and the T::Foo: 'static
parts shouldn’t be necessary, but #20890 about that), but it doesn’t (writing <T as …>::Foo
instead of T::Foo
doesn’t help, either); while BlandTrait
’s Foo
is understood to be 'static
, the lifetime of InterestingTrait<'_>
’s Foo
does not appear to be anything, if these errors are anything to go by:
<anon>:12:5: 12:26 error: the associated type `<T as InterestingTrait<'_>>::Foo` may not live long enough
<anon>:12 assert_lifetime::<'a>(value);
^~~~~~~~~~~~~~~~~~~~~
<anon>:12:5: 12:26 help: consider adding an explicit lifetime bound `<T as InterestingTrait<'_>>::Foo: 'a`...
<anon>:12 assert_lifetime::<'a>(value);
^~~~~~~~~~~~~~~~~~~~~
<anon>:12:5: 12:26 note: ...so that the declared lifetime parameter bounds are satisfied
<anon>:12 assert_lifetime::<'a>(value);
^~~~~~~~~~~~~~~~~~~~~
<anon>:13:5: 13:31 error: the associated type `<T as InterestingTrait<'_>>::Foo` may not live long enough
<anon>:13 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:13:5: 13:31 help: consider adding an explicit lifetime bound `<T as InterestingTrait<'_>>::Foo: 'static`...
<anon>:13 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:13:5: 13:31 note: ...so that the declared lifetime parameter bounds are satisfied
<anon>:13 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:17:5: 17:31 error: the associated type `<T as InterestingTrait<'_>>::Foo` may not live long enough
<anon>:17 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:17:5: 17:31 help: consider adding an explicit lifetime bound `<T as InterestingTrait<'_>>::Foo: 'static`...
<anon>:17 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:17:5: 17:31 note: ...so that the declared lifetime parameter bounds are satisfied
<anon>:17 assert_lifetime::<'static>(value);
^~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors