Unheplful Borrow Error Related to Use of Borrows in Loop
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reproducer in src/lib.rs with the reported rustc version and compare the current E0499 diagnostic with the desired output. Trace the borrow-checker diagnostic entry point that formats the first-borrow and second-borrow notes, then add coverage for the loop wording and confirm the diagnostic matches the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100