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

Too conservative lifetime check in match arm with if condition. #137878

Open
sswybd opened this issue Mar 2, 2025 · 0 comments
Open

Too conservative lifetime check in match arm with if condition. #137878

sswybd opened this issue Mar 2, 2025 · 0 comments
Labels
A-lifetimes Area: Lifetimes / regions needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sswybd
Copy link

sswybd commented Mar 2, 2025

#![allow(unused)]

enum Enum {
    A,
}

fn main() {
    let mut a = Some(Enum::A);
    match &mut a {
        _ if foo(&a) => (),
        _ => (),
    }  // example1: worked
    match &mut a {
        Some(_) if foo(&a) => (),
        _ => (),
    }  // example2: not worked
}
fn foo<T>(a: &T) -> bool {
    true
}

The above two examples are similar, but compilation can succeed only for the first, while the second one has the following error:

/*
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
  --> src/main.rs:14:24
   |
13 |     match &mut a {
   |           ------ mutable borrow occurs here
14 |         Some(_) if foo(&a) => (),
   |                        ^^- mutable borrow later used here
   |                        |
   |                        immutable borrow occurs here
*/

But the exclusive reference in Some(_) is indeed never used and this specific case at least should be acceptable. I think this is something related to non-lexical lifetime, yet it's a quite simple case to implement or correct in the lifetime checker.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 2, 2025
@lolbinarycat lolbinarycat added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. 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

3 participants