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

Unheplful Borrow Error Related to Use of Borrows in Loop #124181

Open
asquared31415 opened this issue Apr 19, 2024 · 0 comments
Open

Unheplful Borrow Error Related to Use of Borrows in Loop #124181

asquared31415 opened this issue Apr 19, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@asquared31415
Copy link
Contributor

asquared31415 commented Apr 19, 2024

Code

struct Cat {
    nya: String,
}

impl Cat {
    fn meow(&mut self) {}
}

fn use_cat(cat: &mut Cat) {
    let nya = &mut cat.nya;

    loop {
        if nya == "" {}

        cat.meow();
    }
}

Current output

error[E0499]: cannot borrow `*cat` as mutable more than once at a time
  --> src/lib.rs:15:9
   |
10 |     let nya = &mut cat.nya;
   |               ------------ first mutable borrow occurs here
...
13 |         if nya == "" {}
   |            --- first borrow later used here
14 |
15 |         cat.meow();
   |         ^^^ second mutable borrow occurs here

Desired output

error[E0499]: cannot borrow `*cat` as mutable more than once at a time
  --> src/lib.rs:15:9
   |
10 |     let nya = &mut cat.nya;
   |               ------------ first mutable borrow occurs here
...
13 |         if nya == "" {}
   |            --- first borrow later used here (in a future iteration of this loop)
14 |
15 |         cat.meow();
   |         ^^^ second mutable borrow occurs here (in a loop)

Rationale and extra context

I encountered this issue in a real project where the binding equivalent to nya was very far away from the use, and i was using cat immutably above the == (but didn't recognize it as immutable), which led me to believe that the use of cat was not the issue. Really I think this just needs a note about the fact that the uses are in a loop, so they are both used before and after each other.

Rust Version

rustc 1.79.0-nightly (e3181b091 2024-04-18)
binary: rustc
commit-hash: e3181b091e88321f5ea149afed6db0edf0a4f37b
commit-date: 2024-04-18
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4
@asquared31415 asquared31415 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 19, 2024
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 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

1 participant