Skip to content

The existence of a lifetime parameter on a trait somehow prevents its associated types from having lifetime constraints that work #21183

Closed
@chris-morgan

Description

@chris-morgan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions