Skip to content

Borrow::borrow() causes compile error with NLL, but compiles without NLL #53151

Closed
@lo48576

Description

@lo48576

(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

https://play.rust-lang.org/?gist=8a28dd1ee97d6bf3bd7c2e6976a14613&version=nightly&mode=debug&edition=2015

#![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

https://play.rust-lang.org/?gist=f6775d0b5e36911ff5c21603908f2bd2&version=nightly&mode=debug&edition=2015

#![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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)NLL-completeWorking towards the "valid code works" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions