You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Borrowing issue with impl Trait leads to unergonomic verbosity #37790
#![feature(conservative_impl_trait)]structA{v:Vec<usize>}implA{fnfoo<'a>(&'aself) -> implIterator<Item=usize> + 'a{(0..self.v.len()).map(move |i| self.v[i])}}fnbar() -> A{A{v:vec![]}}fnbaz() -> 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<_>>()}fnmain(){}
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
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.
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:
structS(i32);structT<'a>(&'aS);// Uncomment this impl, and the program fails to compile.// impl<'a> Drop for T<'a> { fn drop(&mut self) { } }fnfoo() -> S{S(42)}implS{fnbar(&self) -> T{T(self)}}impl<'a>T<'a>{fnget(&self) -> i32{(self.0).0}}fnmain(){let _x = foo().bar().get();}
Activity
arielb1 commentedon Nov 15, 2016
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.
jimblandy commentedon Feb 7, 2017
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 borrowerbar().foo(). If "values in a scope are dropped in the opposite order they are created", then sincebar().foo()is necessarily created afterbar(), the temporary holdingbar().foo()should be dropped before the temporary holdingbar(). 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 holdingbar()andbar().foo()to have identical lifetimes, hence the error.Note that if you change the types to be things without
Dropimpls, the temporaries' lifetimes are no problem:estebank commentedon Mar 6, 2019
Triage: no change.
estebank commentedon Oct 11, 2019
All of these cases now compile successfully.