Repository navigation
Wrong hint for multiple mutable borrows #164169
Copy link
Copy link
Open
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Code
use std::path::PathBuf;
fn main() {}
type Test = Option<(PathBuf, Vec<u8>)>;
fn test1(backend: &mut Vec<Test>) -> Result<(), String> {
let mut backend2 = Vec::new();
for b in backend {
backend2.push(match b {
None => None,
Some((file, bytes)) => Some((file.as_path(), bytes)),
});
}
loop {
test2(&mut backend2)?;
}
}
fn test2<'a, P, C>(disks: &'a mut [Option<(P, &'a mut Vec<u8>)>]) -> Result<C, String> {
todo!()
}
fn test3<'a, P, C>(disks: &'a mut [Option<(P, &'a mut Vec<u8>)>]) -> Result<C, String> {
loop {
test2(disks)?;
}
}Current output
error[E0499]: cannot borrow `backend2` as mutable more than once at a time
--> atari-sio-link-cli/src/main.rs:17:15
|
10 | backend2.push(match b {
| -------- first borrow used here, in later iteration of loop
...
17 | test2(&mut backend2)?;
| ^^^^^^^^^^^^^ `backend2` was mutably borrowed here in the previous iteration of the loop
error[E0499]: cannot borrow `*disks` as mutable more than once at a time
--> atari-sio-link-cli/src/main.rs:27:15
|
25 | fn test3<'a, P, C>(disks: &'a mut [Option<(P, &'a mut Vec<u8>)>]) -> Result<C, String> {
| -- lifetime `'a` defined here
26 | loop {
27 | test2(disks)?;
| ------^^^^^-
| | |
| | `*disks` was mutably borrowed here in the previous iteration of the loop
| argument requires that `*disks` is borrowed for `'a`Desired output
Rationale and extra context
I guess it's the same problem in both cases though the first reports the problem outside the loop.
Other cases
Rust Version
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: aarch64-apple-darwin
release: 1.98.1
LLVM version: 22.1.8Anything else?
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
maxdexh commentedon Oct 11, 2026
I don't understand what you're saying the problem is. What do you expect to happen?
alexkazik commentedon Oct 11, 2026
If both are the same errors (which I guess but are not totally sure) then I'd expect that both messages tell me that the function in the loop is where the problem is, but the error in test1 refers to a use outside of the loop.
maxdexh commentedon Oct 11, 2026
Hm yeah, this is strange. The usage of
backend2in theforloop should not have any relevance to the error later on.The same thing doesn't happen when unwrapping the
forloop (e.g. by just matching&mut backend[0]without a loop).estebank commentedon Oct 12, 2026
Just FYI, signatures like
disks: &'a mut [Option<(P, &'a mut Vec<u8>)>]are almost never what you want, the inner borrow and the outer borrow should have different lifetimes.alexkazik commentedon Oct 12, 2026
Thanks, luckily I already figured that out (before posting).
xizheyin commentedon Oct 16, 2026
Indeed strange, the first borrow
backend2.pushalready released when loop, but the error points to it.Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Rollup merge of rust-lang#164424 - xizheyin:issue-164169, r=estebank
Unrolled build for #164424 in rollup 164479