From c9cf4993307b9623580b1fe7f12aa87df1225fb8 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 18 Sep 2018 12:18:31 +0200 Subject: [PATCH] Address following error from rustdoc tests: error[E0106]: missing lifetime specifier --> /checkout/obj/build/x86_64-unknown-linux-gnu/test/error-index.md:11424:23 | 9 | fn demo(s: &mut S) -> &mut String { let p = &mut *(*s).data; p } | ^ expected lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say which one of `s`'s 2 lifetimes it is borrowed from --- src/librustc_mir/diagnostics.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/diagnostics.rs b/src/librustc_mir/diagnostics.rs index a09eece0266..24197c9e4b8 100644 --- a/src/librustc_mir/diagnostics.rs +++ b/src/librustc_mir/diagnostics.rs @@ -2194,13 +2194,15 @@ lifetime of a type that implements the `Drop` trait. Example of erroneous code: ```compile_fail,E0713 +#![feature(nll)] + pub struct S<'a> { data: &'a mut String } impl<'a> Drop for S<'a> { fn drop(&mut self) { self.data.push_str("being dropped"); } } -fn demo(s: S) -> &mut String { let p = &mut *s.data; p } +fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; p } ``` Here, `demo` tries to borrow the string data held within its @@ -2222,14 +2224,24 @@ not run while the string-data is borrowed; for example by taking `S` by reference: ``` +#![feature(nll)] + pub struct S<'a> { data: &'a mut String } impl<'a> Drop for S<'a> { fn drop(&mut self) { self.data.push_str("being dropped"); } } -fn demo(s: &mut S) -> &mut String { let p = &mut *(*s).data; p } +fn demo<'a>(s: &'a mut S<'a>) -> &'a mut String { let p = &mut *(*s).data; p } ``` + +Note that this approach needs a reference to S with lifetime `'a`. +Nothing shorter than `'a` will suffice: a shorter lifetime would imply +that after `demo` finishes excuting, something else (such as the +destructor!) could access `s.data` after the end of that shorter +lifetime, which would again violate the `&mut`-borrow's exclusive +access. + "##, }