# Rust is shrinking lifetimes too much when passed to closures

**URL:** <https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026>\
**Category:** Uncategorized\
**Created:** [June 13, 2024, 12:19pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026 "2024-06-13T12:19:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [June 13, 2024, 12:19pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026/1 "2024-06-13T12:19:34Z")

</div>

This is _very_ inconvenient to have that code not compiling: [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=%23%21%5Ballow%28dead_code%29%5D%0A%23%21%5Ballow%28clippy%3A%3Aall%29%5D%0A%23%21%5Ballow%28unused_must_use%29%5D%0A%0Afn+main%28%29+%7B%7D%0A%0Astruct+Opaque%28%26%27static+mut+%28%29%29%3B%0Afn+use_static%28_%3A+%26%27static+mut+%28%29%29+%7B+%7D%0Afn+use_static2%28op%3A+Opaque%29+%7B+use_static%28op.0%29+%7D%0A%0Afn+compiles%28foo%3A+%26%27static+mut+%28%29%2C+bar%3A+%26%27static+mut+%28%29%29+%7B%0A++++let+foo+%3D+Opaque%28foo%29%3B%0A++++let+bar+%3D+Opaque%28bar%29%3B%0A++++%7C%7C+use_static2%28foo%29+%3B%0A++++move+%7C%7C+use_static2%28bar%29%3B%0A%7D%0A%0A%2F%2F+%23%5Bcfg%28skip%29%5D+%2F%2F+remove+that+line+and+it+will+not+compile%0Afn+error%28foo%3A+%26%27static+mut+%28%29%2C+bar%3A+%26%27static+mut+%28%29%29+%7B%0A++++%7C%7C+use_static%28foo%29%3B%0A++++move+%7C%7C+use_static%28bar%29%3B%0A%7D%0A%0A)

It makes a _lot_ of problems during embedded development, requiring to do stuff like `Opaque`, that makes code very unpleasant to read and write.

```rust
#![allow(dead_code)]
#![allow(clippy::all)]
#![allow(unused_must_use)]

fn main() {}

struct Opaque(&'static mut ());
fn use_static(_: &'static mut ()) { }
fn use_static2(op: Opaque) { use_static(op.0) }

fn compiles(foo: &'static mut (), bar: &'static mut ()) {
    let foo = Opaque(foo);
    let bar = Opaque(bar);
    || use_static2(foo) ;
    move || use_static2(bar);
}

// #[cfg(skip)] // remove that line and it will not compile
fn error(foo: &'static mut (), bar: &'static mut ()) {
    || use_static(foo);
    move || use_static(bar);
}

```

```rust
   Compiling playground v0.0.1 (/playground)
error: lifetime may not live long enough
  --> src/main.rs:20:8
   |
20 | || use_static(foo);
   | -- ^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'static`
   | |
   | lifetime `'1` represents this closure's body
   |
   = note: closure implements `FnMut`, so references to captured variables can't escape the closure

error: lifetime may not live long enough
  --> src/main.rs:21:13
   |
21 | move || use_static(bar);
   | ------- ^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'static`
   | |
   | lifetime `'1` represents this closure's body
   |
   = note: closure implements `FnMut`, so references to captured variables can't escape the closure

error: could not compile `playground` (bin "playground") due to 2 previous errors

```

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [June 13, 2024, 12:33pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026/2 "2024-06-13T12:33:38Z")

</div>

This compiles too:

```rust
fn use_static3(_: (&'static mut (),)) { }

fn tuples(foo: &'static mut (), bar: &'static mut ()) {
    let foo = (foo,);
    let bar = (bar,);
    || use_static3(foo);
    move || use_static3(bar);
}
fn also_compiles2(foo: &'static mut (), bar: &'static mut ()) {
    let (foo, bar) = ((foo,),(bar,));
    fn transform(foo: (&'static mut (),)) -> &'static mut () {
        foo.0
    }
    || use_static(transform(foo));
    move || use_static(transform(bar));
}

```

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [June 13, 2024, 2:44pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026/3 "2024-06-13T14:44:43Z")

</div>

I'm guessing the issue is that if the reference is only used in ways that could use reborrowing, ~~the closure won't capture it by value~~. Regardless, this compiles:

```rust
fn compiles(foo: &'static mut (), bar: &'static mut ()) {
    || { 
        let foo = foo;
        use_static(foo);
    };
    move || { 
        let bar = bar;
        use_static(bar);
    };
}

```

edit: Specifically, this is best explained by this section in the [Reference](https://doc.rust-lang.org/reference/types/closure.html#call-traits-and-coercions)[https://doc.rust-lang.org/reference/types/closure.html#call-traits-and-coercions](https://doc.rust-lang.org/reference/types/closure.html#call-traits-and-coercions)

> - A closure which does not move out of any captured variables implements [`FnMut`](https://doc.rust-lang.org/std/ops/trait.FnMut.html), indicating that it can be called by mutable reference.

> > Note: `move` closures may still implement [`Fn`](https://doc.rust-lang.org/std/ops/trait.Fn.html) or [`FnMut`](https://doc.rust-lang.org/std/ops/trait.FnMut.html), even though they capture variables by move. This is because the traits implemented by a closure type are determined by what the closure does with captured values, not how it captures them.

As was evident (though not obvious that it was critical) in the error message, the error is due to the closure "looking like" it should implement `FnMut`, when that implementation is not actually possible due to lifetimes.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [June 13, 2024, 4:16pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026/4 "2024-06-13T16:16:57Z")

</div>

Yes, we need some better way to override closure inferences generally.

Another workaround for the playground is to funnel the closure through an identity function expecting `FnOnce`, as that will influence the compiler to not implement `FnMut`.

```rust
fn funnel<F: FnOnce()>(f: F) -> F { f }

fn error(foo: &'static mut (), bar: &'static mut ()) {
    funnel(|| use_static(foo));
    funnel(move || use_static(bar));
}

```

---

<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:** [September 11, 2024, 4:17pm UTC](https://internals.rust-lang.org/t/rust-is-shrinking-lifetimes-too-much-when-passed-to-closures/21026/5 "2024-09-11T16:17:20Z")

</div>

Closed on September 11, 2024, 4:17pm UTC

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