Skip to content

Borrow checker thinks variable lives longer than it actually lives #112087

Closed as not planned
@mosel00

Description

@mosel00

The borrow checker thinks that in the following code the variable v lives at least as long as *opt = None; which is certainly not correct and is also fixed when swapping the two conditions on lines 2 and 3 . To test, here's a link to the rust playground.

I tried this code:

fn f<'a>(opt : &'a mut Option<i32>, b : bool) -> Result<&'a mut Option<i32>, &'a mut i32> {
      if let Some(v) = opt {
          if b {
              return Err(v);
          }
      }
    
    *opt = None;
    return Ok(opt)
}

I expected to see this happen: no compile error

Instead, this happened: compile error E0506

Meta

Bug also exists in beta and nightly

rustc --version --verbose:

binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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