Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mosel00 opened this issue May 29, 2023 · 2 comments
Closed

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

mosel00 opened this issue May 29, 2023 · 2 comments
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mosel00
Copy link

mosel00 commented May 29, 2023

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

@mosel00 mosel00 added the C-bug Category: This is a bug. label May 29, 2023
@ilyvion
Copy link

ilyvion commented May 30, 2023

This is a version of #54663

@fmease fmease added A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. and removed needs-triage-legacy labels Jan 25, 2024
@fmease
Copy link
Member

fmease commented Jan 26, 2024

Closing as duplicate of #54663.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. fixed-by-polonius Compiling with `-Zpolonius` fixes this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants