Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Trait return value causes an over-restricted lifetime requirement #51069

Closed
cuviper opened this issue May 25, 2018 · 6 comments
Closed

impl Trait return value causes an over-restricted lifetime requirement #51069

cuviper opened this issue May 25, 2018 · 6 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cuviper
Copy link
Member

cuviper commented May 25, 2018

Reduced from an example given on the users forum.

This code (playground) fails to compile:

fn main() {
    let v = vec![""];
    runner(&v);
}

fn runner<'p>(v: &Vec<&'p str>) -> impl Iterator<Item = &'p str> {
    find(&vec![0], v)
}

fn find<'n, 'p, I, S>(_: &'n I, _: &Vec<&'p str>) -> impl Iterator<Item = &'p str>
where
    &'n I: IntoIterator<Item = S>,
{
    std::iter::empty()
}
error[E0597]: borrowed value does not live long enough
 --> src/main.rs:7:11
  |
7 |     find(&vec![0], v)
  |           ^^^^^^^ temporary value does not live long enough
8 | }
  | - temporary value only lives until here
  |
note: borrowed value must be valid for the lifetime 'p as defined on the function body at 6:1...
 --> src/main.rs:6:1
  |
6 | fn runner<'p>(v: &Vec<&'p str>) -> impl Iterator<Item = &'p str> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

It seems the 'n lifetime is getting tied into the return type somehow.

It works if you make find return an explicit type:

fn find<'n, 'p, I, S>(_: &'n I, _: &Vec<&'p str>) -> std::iter::Empty<&'p str>
where
    &'n I: IntoIterator<Item = S>,
{
    std::iter::empty()
}

It also works if you instead remove the S parameter:

fn find<'n, 'p, I>(_: &'n I, _: &Vec<&'p str>) -> impl Iterator<Item = &'p str>
where
    &'n I: IntoIterator,
{
    std::iter::empty()
}
@cuviper cuviper added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. labels May 25, 2018
@andrewhickman
Copy link
Contributor

andrewhickman commented Aug 4, 2018

I've also just run into this. Alternate example:

trait Future {}
impl Future for () {}

fn cache<I>(_: I) -> impl Future + 'static
where
    I: IntoIterator<Item = ()>,
{
    ()
}

fn cache_slice<'a>(glyphs: &[()]) -> impl Future + 'static {
    cache(glyphs.iter().cloned())
}

This tells me the lifetime of glyphs needs to be static.

@Nemo157
Copy link
Member

Nemo157 commented Aug 4, 2018

@andrewhickman I think that's a valid error, in the call to cache inside cache_slice I = std::iter::Cloned<std::slice::Iter<'(lifetime of glyphs), ()>>, an impl Trait return type is defined over all input type parameters, so for the impl Future returned from cache to meet its requested bound of 'static it requires I: 'static, which requires '(lifetime of glyphs): 'static.

Notice in @cuviper's example that the &'n I: IntoIterator avoids having the lifetime as part of the input type parameter I.

@andrewhickman
Copy link
Contributor

Is there no way to express that the output type is independent of the input type?

@Nemo157
Copy link
Member

Nemo157 commented Aug 4, 2018

There will be with named existential types, you should even be able to have a generic existential type that depends on only some of the input type parameters, here's a playground

#![feature(existential_type)]

trait Future {}
impl Future for () {}

existential type CacheFuture: Future + 'static;

fn cache<I>(_: I) -> CacheFuture
where
    I: IntoIterator<Item = ()>,
{
    ()
}

pub fn cache_slice<'a>(glyphs: &[()]) -> impl Future + 'static {
    cache(glyphs.iter().cloned())
}

@cuviper
Copy link
Member Author

cuviper commented Aug 6, 2018

an impl Trait return type is defined over all input type parameters

In my example, that would include S for the impl Iterator. I suspect &'n I: IntoIterator<Item = S> is figuring out that this particular Item depends on 'n, and by equality S depends on 'n too, therefore this propagates 'n to the impl Iterator.

If that's the case, it still feels like a bug, but I'm not sure of the rules here.

@Arnavion
Copy link

Arnavion commented Aug 6, 2018

Duplicate of #42940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants