Skip to content

"Outlived by" bound for higher-ranked lifetimes #1409

@apasel422

Description

@apasel422

The following code yields a compilation error:

trait Foo<'a> {
    fn foo(&'a mut self);
}

impl<'a, T: 'a> Foo<'a> for Vec<T> {
    fn foo(&'a mut self) {}
}

fn bar<F>(x: &mut F) where F: for<'a> Foo<'a> {
    x.foo();
}

fn main() {
    let a = ();
    let mut v = vec![&a]; //~  ERROR `a` does not live long enough
                          //~^ reference must be valid for the static lifetime
    bar(&mut v);
}

This is the correct behavior, as v clearly does not implement Foo<'a> for all 'a.

I would like to use something like the following instead, but there is no way to achieve it:

fn bar<'x, F>(x: &'x mut F) where F: for<'a outlived by 'x> Foo<'a> { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions