Skip to content

MIR-borrowck: emit "foo does not live long enough" instead of borrow errors #45360

Closed
@arielb1

Description

@arielb1

e.g. in the test issue-36082.rs:

// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::cell::RefCell;
fn main() {
let mut r = 0;
let s = 0;
let x = RefCell::new((&mut r,s));
let val: &_ = x.borrow().0;
//~^ ERROR borrowed value does not live long enough
//~| temporary value dropped here while still borrowed
//~| temporary value created here
//~| consider using a `let` binding to increase its lifetime
println!("{}", val);
}
//~^ temporary value needs to live until here

MIR borrowck currently emits the following errors:

$ ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc ../src/test/compile-fail/issue-36082.rs -Z borrowck-mir
error[E0597]: borrowed value does not live long enough (Ast)
  --> ../src/test/compile-fail/issue-36082.rs:18:31
   |
18 |     let val: &_ = x.borrow().0;
   |                   ----------  ^ temporary value dropped here while still borrowed
   |                   |
   |                   temporary value created here
...
24 | }
   | - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

error[E0505]: cannot move out of `_` because it is borrowed (Mir)
  --> ../src/test/compile-fail/issue-36082.rs:18:32
   |
18 |     let val: &_ = x.borrow().0;
   |                   ----------   ^ move out of `_` occurs here
   |                   |
   |                   borrow of `_` occurs here

error[E0505]: cannot move out of `_` because it is borrowed (Mir)
  --> ../src/test/compile-fail/issue-36082.rs:18:32
   |
18 |     let val: &_ = x.borrow().0;
   |                   ----------   ^ move out of `_` occurs here
   |                   |
   |                   borrow of `_` occurs here

error[E0506]: cannot assign to `_` because it is borrowed (Mir)
  --> ../src/test/compile-fail/issue-36082.rs:18:32
   |
18 |     let val: &_ = x.borrow().0;
   |                   ----------   ^ assignment to borrowed `_` occurs here
   |                   |
   |                   borrow of `_` occurs here

The MIR borrowck errors are correctly detected when the MIR storagedead and drop conflict with the pre-existing borrow.

That is the exact definition of a "does not live long enough" error, so we need to detect this case and instead of showing multiple borrow errors, show a single "does not live long enough" error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions