Closed
Description
(Feel free to change to the title more appropriate title, I can't explain the problem in single sentence...)
Summary
Happened:
- The two codes below successfully compiles without NLL, but fails to comipile with NLL.
- They look very similar, but emits different error messages.
Expected result:
- Both compiles successfully, or both fails to compile.
- If they are the same error, same (or similar) message would be preferrable.
Meta
- Compiler version: 1.30.0-nightly (2018-08-05 73c7873)
Code to reproduce
Case 1: in impl
#![feature(nll)]
use std::borrow::Borrow;
struct OwnedWrap<B: ToOwned>(<B as ToOwned>::Owned);
impl<'a, B> OwnedWrap<B>
where
B: 'a + ToOwned,
&'a B: Into<B>,
{
pub fn test_borrow(&self) {
let OwnedWrap(ref owned) = *self;
let _: &B = owned.borrow();
}
}
fn main() {}
error with NLL:
Compiling playground v0.0.1 (file:///playground)
error: borrowed data escapes outside of function
--> src/main.rs:14:21
|
12 | pub fn test_borrow(&self) {
| ----- `self` is a reference that is only valid in the function body
13 | let OwnedWrap(ref owned) = *self;
14 | let _: &B = owned.borrow();
| ^^^^^^^^^^^^^^ `self` escapes the function body here
error: aborting due to previous error
error: Could not compile `playground`.
Case 2: toplevel fn
#![feature(nll)]
use std::borrow::Borrow;
struct OwnedWrap<B: ToOwned>(<B as ToOwned>::Owned);
fn test_borrow<'a, B>(wrap: &OwnedWrap<B>)
where
B: 'a + ToOwned,
&'a B: Into<B>,
{
let OwnedWrap(ref owned) = wrap;
let _: &B = owned.borrow();
}
fn main() {}
error with NLL:
Compiling playground v0.0.1 (file:///playground)
error[E0621]: explicit lifetime required in the type of `wrap`
--> src/main.rs:13:17
|
7 | fn test_borrow<'a, B>(wrap: &OwnedWrap<B>)
| ---- consider changing the type of `wrap` to `&'a OwnedWrap<B>`
...
13 | let _: &B = owned.borrow();
| ^^^^^^^^^^^^^^ lifetime `'a` required
error: aborting due to previous error
For more information about this error, try `rustc --explain E0621`.
error: Could not compile `playground`.