Borrow checker does not release a mutable borrow when the condition is false on async function
Open
Nobody has claimed this yet.
A-borrow-checker
C-bug
fixed-by-polonius
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
async fn connection(&mut self) -> Result<&mut Conn> {
// Check if we already have a connection.
if let Some(c) = &mut self.con {
return Ok(c);
}
// Get new connection.
let c = self.pool.get_conn().await?;
Ok(self.con.insert(c))
}
I expected to see this happen: Ok(self.con.insert(c)) line should allow mutable borrow on self.con.
Instead, this happened:
error[E0499]: cannot borrow `self.con` as mutable more than once at a time
--> src/mysql/src/pool/query.rs:25:12
|
16 | async fn connection(&mut self) -> Result<&mut Conn> {
| - let's call the lifetime of this reference `'1`
17 | // Check if we already have a connection.
18 | if let Some(c) = &mut self.con {
| ------------- first mutable borrow occurs here
19 | return Ok(c);
| ----- returning this value requires that `self.con` is borrowed for `'1`
...
25 | Ok(self.con.insert(c))
| ^^^^^^^^ second mutable borrow occurs here
Meta
rustc --version --verbose:
rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5
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 compiling the reported async function with rustc 1.84.0 and confirm the E0499 diagnostic. The example is referenced from src/mysql/src/pool/query.rs; done means the false branch no longer retains the mutable borrow and the reported code compiles while preserving the early-return behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100