Open
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.