Skip to content

Borrowing issue with impl Trait leads to unergonomic verbosity #37790

Closed

Description

@Boscop
#![feature(conservative_impl_trait)]

struct A {
    v: Vec<usize>
}
impl A {
	fn foo<'a>(&'a self) -> impl Iterator<Item=usize> + 'a {
		(0..self.v.len()).map(move |i| self.v[i])
	}
}

fn bar() -> A {
    A { v: vec![] }
}

fn baz() -> Vec<usize> {
    /* doesn't work:
    bar().foo().collect::<Vec<_>>()
    */
    
    /* also doesn't work:
    let x = bar();
    x.foo().collect::<Vec<_>>()
    */
    
    // you have to do this:
    let x = bar();
    let x = x.foo();
    x.collect::<Vec<_>>()
}

fn main() {}
error: borrowed value does not live long enough
  --> <anon>:18:5
   |
18 |     bar().foo().collect::<Vec<_>>()
   |     ^^^^^ temporary value created here
...
30 | }
   | - temporary value dropped before borrower
   |
   = note: values in a scope are dropped in the opposite order they are created

error: aborting due to previous error

Here on rust playground

Activity

  1. arielb1 commented on Nov 15, 2016

    @arielb1
    Contributor

    The destructors for temporaries in tail expressions run when the containing block ends. The intent is to make braces "transparent". We need good examples for that.

  2. jimblandy commented on Feb 7, 2017

    @jimblandy
    Contributor

    I think this is the same problem that we discussed in the user forum a while back.

    I believe the error message is incorrect. It is simply not true that the temporary value bar() is dropped before its borrower bar().foo(). If "values in a scope are dropped in the opposite order they are created", then since bar().foo() is necessarily created after bar(), the temporary holding bar().foo() should be dropped before the temporary holding bar(). So the ordering of the drops ought to be fine.

    The actual motivation for the error is that, for types that implement Drop, Rust doesn't consider it sufficient for borrowers to be dropped before the values they borrow. The lifetime of the latter must be strictly larger than that of the former. Rust considers the two temporaries holding bar() and bar().foo() to have identical lifetimes, hence the error.

    Note that if you change the types to be things without Drop impls, the temporaries' lifetimes are no problem:

    struct S(i32);
    struct T<'a>(&'a S);
    
    // Uncomment this impl, and the program fails to compile.
    // impl<'a> Drop for T<'a> { fn drop(&mut self) { } }
    
    fn foo() -> S { S(42) }
    
    impl S {
        fn bar(&self) -> T { T(self) }
    }
    
    impl<'a> T<'a> {
        fn get(&self) -> i32 { (self.0).0 }
    }
    
    fn main() {
        let _x = foo().bar().get();
    }
  3. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    on Jun 23, 2017
  4. estebank commented on Mar 6, 2019

    @estebank
    Contributor

    Triage: no change.

  5. estebank commented on Oct 11, 2019

    @estebank
    Contributor

    All of these cases now compile successfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions