# Lifetime for match variable is too pessimistic

**URL:** <https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965>\
**Category:** language design\
**Created:** [February 2, 2021, 6:53pm UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965 "2021-02-02T18:53:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yshui](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yshui/32/7725_2.png) [@yshui](https://internals.rust-lang.org/u/yshui)\
**Post date:** [February 2, 2021, 6:53pm UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965/1 "2021-02-02T18:53:05Z")

</div>

```rust
use either::Either;
struct A;
struct B;
struct AW<'a>(&'a mut A);
impl<'a> Drop for AW<'a> {
    fn drop(&mut self) {
        println!("AW drop");
    }
}
fn cap(_: &mut A) -> Either::<AW<'_>, B> {
    unimplemented!()
}
pub fn main() {
    let mut a = A;
    let b = cap(&mut a);
    match b {
        Either::Left(a) => {
            drop(a);
            // b is destructed, and drop(a) already called
        },
        Either::Right(y) => {
            a = A;
            // b is destructed
        }
    }; // No way is the compiler going to drop b at this point in either case
}

```

In this piece of code, the compiler borrows `b` for the entire duration of `match`. The explanation it gives is that it needs to run `drop` on `b` at the end of the `match`.

However, that's not the case at all. `b` is partially moved in both arms, its destructor _cannot_ be called at the end of `match`.

Yet the compiler still insists on borrowing `b` for the whole `match`, preventing me from modifying `a` in the `Right` arm, even if it's safe.

playground link: [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d8450c3f1f2575c211212aab93974a0c)

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [February 2, 2021, 7:21pm UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965/2 "2021-02-02T19:21:01Z")

</div>

Interesting, I was going to try and figure out the drop order when destruction happens but the rust compiler blocks moving out while destructing if the type implements `Drop`. So unless I am missing something (which tbh I probably am) this should be allowed.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [February 2, 2021, 7:22pm UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965/3 "2021-02-02T19:22:13Z")

</div>

I think this sort of control flow based lifetime analysis will come with the next version of the borrow checker: polonius

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [February 3, 2021, 5:56am UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965/4 "2021-02-03T05:56:29Z")

</div>

Still happens with `-Z polonius` though.

Anyway, this type of thing generally merits an issue report in the [rust-lang/rust repo](https://github.com/rust-lang/rust/issues).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 4, 2021, 5:57am UTC](https://internals.rust-lang.org/t/lifetime-for-match-variable-is-too-pessimistic/13965/5 "2021-05-04T05:57:01Z")

</div>

Closed on May 4, 2021, 5:57am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
