Skip to content

Commit c9cf499

Browse files
committed
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
1 parent e3b611f commit c9cf499

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/librustc_mir/diagnostics.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -2194,13 +2194,15 @@ lifetime of a type that implements the `Drop` trait.
21942194
Example of erroneous code:
21952195
21962196
```compile_fail,E0713
2197+
#![feature(nll)]
2198+
21972199
pub struct S<'a> { data: &'a mut String }
21982200
21992201
impl<'a> Drop for S<'a> {
22002202
fn drop(&mut self) { self.data.push_str("being dropped"); }
22012203
}
22022204
2203-
fn demo(s: S) -> &mut String { let p = &mut *s.data; p }
2205+
fn demo<'a>(s: S<'a>) -> &'a mut String { let p = &mut *s.data; p }
22042206
```
22052207
22062208
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`
22222224
by reference:
22232225
22242226
```
2227+
#![feature(nll)]
2228+
22252229
pub struct S<'a> { data: &'a mut String }
22262230
22272231
impl<'a> Drop for S<'a> {
22282232
fn drop(&mut self) { self.data.push_str("being dropped"); }
22292233
}
22302234
2231-
fn demo(s: &mut S) -> &mut String { let p = &mut *(*s).data; p }
2235+
fn demo<'a>(s: &'a mut S<'a>) -> &'a mut String { let p = &mut *(*s).data; p }
22322236
```
2237+
2238+
Note that this approach needs a reference to S with lifetime `'a`.
2239+
Nothing shorter than `'a` will suffice: a shorter lifetime would imply
2240+
that after `demo` finishes excuting, something else (such as the
2241+
destructor!) could access `s.data` after the end of that shorter
2242+
lifetime, which would again violate the `&mut`-borrow's exclusive
2243+
access.
2244+
22332245
"##,
22342246

22352247
}

0 commit comments

Comments
 (0)