Skip to content

NLL: lost "borrowed value needs to live until here" (diagnostic regression compared to AST-borrowck) #54382

Closed

Description

@pnkfelix

Spun off of my investigation of #21114, #54556

Consider this code (play):

#![cfg_attr(use_nll, feature(nll))]

fn main() {
    {
        let mut _thing1 = D(Box::new("thing1"));
        D("other").next(&_thing1)
    }

    ;
}

#[derive(Debug)]
struct D<T: std::fmt::Debug>(T);

impl<T: std::fmt::Debug>  Drop for D<T> {
    fn drop(&mut self) {
        println!("dropping {:?})", self);
    }
}

impl<T: std::fmt::Debug> D<T> {
    fn next<U: std::fmt::Debug>(&self, _other: U) -> D<U> { D(_other) }
    fn end(&self) { }
}

Under AST-borrowck, this emits the diagnostics:

Standard Error

   Compiling playground v0.0.1 (/playground)
error[E0597]: `_thing1` does not live long enough
 --> src/main.rs:6:26
  |
6 |         D("other").next(&_thing1)
  |                          ^^^^^^^ borrowed value does not live long enough
7 |     }
  |     - `_thing1` dropped here while still borrowed
8 | 
9 |     ;
  |     - borrowed value needs to live until here

under NLL, it only emits this:

error[E0597]: `_thing1` does not live long enough
 --> src/main.rs:6:25
  |
6 |         D("other").next(&_thing1)
  |                         ^^^^^^^^ borrowed value does not live long enough
7 |     }
  |     - `_thing1` dropped here while still borrowed

In particular, the note 8 "borrowed value needs to live until here" that highlights the semi-colon after the end of the block is no longer emitted.

My current plan for addressing #54556 is to improve diagnostics. The first step in that path, IMO, would be to figure out how to bring back the note about how long the borrowed value needs to live.

Activity

  1. added
    A-NLLArea: Non-lexical lifetimes (NLL)
    NLL-diagnosticsWorking towards the "diagnostic parity" goal
    on Sep 20, 2018
  2. added this to the Rust 2018 Release milestone on Sep 25, 2018
  3. nikomatsakis commented on Oct 30, 2018

    @nikomatsakis
    Contributor

    Current output:

    error[E0597]: `_thing1` does not live long enough
     --> src/main.rs:6:25
      |
    4 |        {
      |   _____-
      |  |_____|
      | ||
    5 | ||         let mut _thing1 = D(Box::new("thing1"));
    6 | ||         D("other").next(&_thing1)
      | ||                         ^^^^^^^^ borrowed value does not live long enough
    7 | ||     }
      | ||     -
      | ||     |
      | ||_____`_thing1` dropped here while still borrowed
      | |______a temporary with access to the borrow is created here ...
      |        ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `D`
    

    the labels look good, but the spans associated with them -- woah...

  4. pnkfelix commented on Oct 31, 2018

    @pnkfelix
    ContributorAuthor

    Hmm I wonder why the temp is being given the span of the whole block rather than the blocks tail expr

  5. pnkfelix commented on Oct 31, 2018

    @pnkfelix
    ContributorAuthor

    And likewise, why the drop of the temporary is no longer associated with the span of the semicolon on the whole statement ...

  6. pnkfelix commented on Nov 6, 2018

    @pnkfelix
    ContributorAuthor

    Triage: While I do want to address this, I don't think we would feel all that inclined to backport a fix to the beta channel.

    So, tagging as P-high, but removing from the Release milestone.


    Also, I'm going to assign this to myself since I've taken a special interest in issues involving tail expressions of blocks.

  7. removed this from the Rust 2018 Release milestone on Nov 6, 2018
  8. self-assigned this
    on Nov 6, 2018
  9. added 2 commits that reference this issue on Nov 12, 2018
    746ef18
    6ca7bc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)NLL-diagnosticsWorking towards the "diagnostic parity" goalP-highHigh priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions