Too conservative lifetime check in match arm with if condition.
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
#![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.
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 reduced example in src/main.rs and compare the two match arms, focusing on the borrow checker behavior around the guard. Trace why the mutable borrow remains live for the Some(_) pattern. Done when the second example is accepted without E0502 and a regression test covers the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100