Perform lifetime shortening on literal patterns with mutable destructuring

fn do_stuff_fails(foo @ Foo { a, b }: &mut Foo) {
    *a = 42;
    *b = 69;
    accept(foo)
}

fn do_stuff_works(foo: &mut Foo) {
    let Foo { a, b } = foo;
    *a = 42;
    *b = 69;
    accept(foo)
}

struct Foo {
    a: u32,
    b: u32,
}

fn accept(_: &mut Foo) {}

It would be pretty cool if the lifetimes of foo and {a, b} in do_stuff_fails could be inferred as spanning disjoints blocks of code as in the do_stuff_works example.

5 Likes

I'd call it implied reborrowing or such (the lifetimes in the types of a and b are subsets of that in foo, and also not be disjoint of each other), but it is a cool idea.

1 Like