Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrowing error refers to ***borrow when borrow should be &mut i32 #33071

Closed
birkenfeld opened this issue Apr 18, 2016 · 6 comments
Closed

Borrowing error refers to ***borrow when borrow should be &mut i32 #33071

birkenfeld opened this issue Apr 18, 2016 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@birkenfeld
Copy link
Contributor

Found on rustc 1.10.0-nightly (2174bd9 2016-04-14). Minimal example:

fn main() {
    let mut var = 0i32;
    let borrow = &mut var;
    || {
        let borrow_borrow = &borrow;
        *borrow = 1;
    };
}

The error is

src/main.rs:6:9: 6:20 error: cannot assign to `***borrow` because it is borrowed [E0506]
src/main.rs:6         *borrow = 1;
                      ^~~~~~~~~~~
src/main.rs:5:30: 5:36 note: borrow of `***borrow` occurs here
src/main.rs:5         let borrow_borrow = &borrow;
                                           ^~~~~~

Actually trying to assign to ***borrow or **borrow fails with the expected "i32 cannot be dereferenced".

@KalitaAlexey
Copy link
Contributor

KalitaAlexey commented Apr 18, 2016

@birkenfeld In closure you have borrow_borrow, but use borrow. Is it ok?
Type of borrow_borrow is & &mut i32. You borrowed mutable borrow so you cannot use mutable borrow until borrow of mutable borrow exists.

@birkenfeld
Copy link
Contributor Author

@KalitaAlexey I know. The point of the issue isn't that it should compile, but that the error message is strange: it refers to ***borrow which is probably invalid, but at least confusing.

@Thiez
Copy link
Contributor

Thiez commented Apr 18, 2016

Not sure if this helps, but using a move closure as shown below changes the name in the error **borrow instead of ***borrow.

    let mut var = 0i32;
    let borrow = &mut var;
    move || {
        let borrow_borrow: &&mut i32 = &borrow;
        *borrow = 1;
    };
6:9: 6:20 error: cannot assign to `**borrow` because it is borrowed [E0506]
6         *borrow = 1;
          ^~~~~~~~~~~
5:41: 5:47 note: borrow of `**borrow` occurs here
5         let borrow_borrow: &&mut i32 = &borrow;
                                          ^~~~~~

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 25, 2016
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
@steveklabnik
Copy link
Member

Triage: non-lexical lifetimes makes this code work. But without it, this error still happens.

Are there other code samples that trigger this error? Because eventually, in practice, nobody will see it, even if it's bad.

@jonas-schievink
Copy link
Contributor

jonas-schievink commented Mar 16, 2019

fn main() {
    let mut var = 0i32;
    let borrow = &mut var;
    || {
        let borrow_borrow = &borrow;
        *borrow = 1;
        let _use = borrow_borrow;
    };
}

gives (with NLL)

error[E0506]: cannot assign to `*borrow` because it is borrowed
 --> src/main.rs:6:9
  |
5 |         let borrow_borrow = &borrow;
  |                             ------- borrow of `*borrow` occurs here
6 |         *borrow = 1;
  |         ^^^^^^^^^^^ assignment to borrowed `*borrow` occurs here
7 |         let _use = borrow_borrow;
  |                    ------------- borrow later used here

So the diagnostic is fixed when NLL is enabled.

@estebank estebank added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label May 22, 2019
@pnkfelix
Copy link
Member

pnkfelix commented Jul 9, 2019

Now that NLL is stably deployed for all editions, closing as fixed.

@pnkfelix pnkfelix closed this as completed Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants