Skip to content

Borrow checker does not release a mutable borrow when the condition is false on async function #136245

Open
@ultimaweapon

Description

@ultimaweapon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.fixed-by-poloniusCompiling with `-Zpolonius` fixes this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions