Skip to content

Erroneous borrow error from 2018 edition's NLL migration mode. #52967

Closed

Description

@nrc

I have:

pub fn infer_defaults(&mut self, project_dir: &Path) -> CargoResult<()> {
    ....
    match &mut self.target_dir {
            Inferrable::Specified(Some(ref mut path)) if path.is_relative() => {
                *path = project_dir.join(&path);
            }
            _ => {},
        }
    ...
}

(from https://github.com/rust-lang-nursery/rls/blob/master/src/config.rs#L238-L277)

And I get the following warning:

warning[E0502]: cannot borrow `_` as mutable because it is also borrowed as immutable
   --> src/config.rs:259:40
    |
257 |         match &mut self.target_dir {
    |               -------------------- immutable borrow occurs here
258 |             // We require an absolute path, so adjust a relative one if it's passed.
259 |             Inferrable::Specified(Some(ref mut path)) if path.is_relative() => {
    |                                        ^^^^^^^^^^^^ mutable borrow occurs here
...
262 |             _ => {},
    |             - borrow later used here
    |
    = warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
            It represents potential unsoundness in your code.
            This warning will become a hard error in the future.

The warning is the same without the &mut.

It seems to me this code is fine and there should not be a warning.

cc @nikomatsakis @pnkfelix

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-NLLArea: Non-lexical lifetimes (NLL)
    on Aug 2, 2018
  2. pnkfelix commented on Aug 2, 2018

    @pnkfelix
    Contributor

    Smaller example (playground):

    fn main() {
        let mut stuff = ("left", "right");
        match stuff {
            (ref mut left, _) if *left == "left" => { }
            _ => {}
        }
    }

    which emits the diagnostic:

    warning[E0502]: cannot borrow `stuff.0` as mutable because it is also borrowed as immutable
     --> src/main.rs:4:10
      |
    3 |     match stuff {
      |           ----- immutable borrow occurs here
    4 |         (ref mut left, _) if *left == "left" => { }
      |          ^^^^^^^^^^^^ mutable borrow occurs here
    5 |         _ => {}
      |         - borrow later used here
      |
      = warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
              It represents potential unsoundness in your code.
              This warning will become a hard error in the future.
    
  3. pnkfelix commented on Aug 2, 2018

    @pnkfelix
    Contributor

    Here's the (very?) good news: this is not an NLL bug, per se.

    In particular, when we opt into NLL explicitly, the example works (playground):

    #![feature(nll)]
    
    fn main() {
        let mut stuff = ("left", "right");
        match stuff {
            (ref mut left, _) if *left == "left" => { }
            _ => {}
        }
    }

    There is a bug in how I linked the migration mode into the 2018 edition (PR #52681). Basically, its not toggling on enough switches, and one of the ones I missed is causing something to break with the new way we handle match.

  4. changed the title [-]Erroneous borrow error from NLL[/-] [+]Erroneous borrow error from 2018 edition's NLL migration mode.[/+] on Aug 2, 2018
  5. pnkfelix commented on Aug 2, 2018

    @pnkfelix
    Contributor

    This issue strikes me as an argument that we should turn on something analogous to compare-mode=nll in compiletest, except specialized to just the 2018 edition opt-in, rather than turning on NLL via debug switches.

  6. self-assigned this
    on Aug 2, 2018
  7. pnkfelix commented on Aug 2, 2018

    @pnkfelix
    Contributor

    In particular, you see the error with -Z borrowck=mir, but not with -Z borrowck=mir -Z two-phase-borrows. That's a pretty strong hint that I forgot to have edition=2008 imply two-phase-borrows.

  8. added a commit that references this issue on Aug 2, 2018
    09a7e64
  9. added a commit that references this issue on Aug 2, 2018
    5e92bf2
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)T-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