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
Spurious "borrowed value does not live long enough" when calling a function through a function pointer #49073
The following code (edited to an even smaller version provided by occultus):
use std::string::ToString;fnmain(){let to_string = ToString::to_string;let i = 1;let b = &i;assert_eq!("", to_string(&b));}
yields the following compiler output:
error[E0597]: `i` does not live long enough
--> src/main.rs:6:14
|
6 | let b = &i;
| ^ borrowed value does not live long enough
7 | assert_eq!("", to_string(&b));
8 | }
| - `i` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
Moving let to_string down one line "fixes" it. So does #![feature(nll)].
In case you wonder how one ends up with code like this, it's because this is test code, and to_string is actually <Trait as ToString>::to_string, which is a lot of boilerplate.
Triage: now compiles on 2018 edition.
2015 errors:
error[E0597]: `i` does not live long enough
--> src/main.rs:6:14
|
6 | let b = &i;
| ^ borrowed value does not live long enough
7 | assert_eq!("", to_string(&b));
8 | }
| - `i` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
Activity
durka commentedon Mar 16, 2018
For bonus points, try annotating the type of
to_string.hanna-kruppe commentedon Mar 16, 2018
Apparently the code was edited, but the error message was updated to the new code? Very confusing!
glandium commentedon Mar 16, 2018
Sorry, I forgot to update the line numbers. Fixed.
memoryruins commentedon Jan 30, 2019
Triage: now compiles on 2018 edition.
2015 errors:
rustc: 1.32.0Auto merge of #59114 - matthewjasper:enable-migate-2015, r=<try>
Auto merge of #59114 - matthewjasper:enable-migate-2015, r=pnkfelix
Auto merge of #59114 - matthewjasper:enable-migate-2015, r=pnkfelix