# Lifetime error should point out the reason for a lifetime

**URL:** <https://internals.rust-lang.org/t/lifetime-error-should-point-out-the-reason-for-a-lifetime/14004>\
**Category:** language design\
**Created:** [February 7, 2021, 6:41pm UTC](https://internals.rust-lang.org/t/lifetime-error-should-point-out-the-reason-for-a-lifetime/14004 "2021-02-07T18:41:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xixixao](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/xixixao/32/5443_2.png) [@xixixao](https://internals.rust-lang.org/u/xixixao)\
**Post date:** [February 7, 2021, 6:41pm UTC](https://internals.rust-lang.org/t/lifetime-error-should-point-out-the-reason-for-a-lifetime/14004/1 "2021-02-07T18:41:24Z")

</div>

This is the error message I got:

```rust
error[E0597]: `shader_file_path_string` does not live long enough
   --> shader_roy/src/main.rs:60:48
    |
60 | let shader_file_path = std::path::Path::new(&shader_file_path_string);
    | ---------------------^^^^^^^^^^^^^^^^^^^^^^^^-
    | | |
    | | borrowed value does not live long enough
    | argument requires that `shader_file_path_string` is borrowed for `'static`
...
242 | }
    | - `shader_file_path_string` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

```

The problem is, this doesn't tell me why `shader_file_path` is expected to live for the `static` lifetime (well, it doesn't even tell me that, it only talks about `shader_file_path_string`). In my code, the reason is this part:

```rust
    events_loop.run(move |_, _, _| {
        foo(shader_file_path)?;
    });

```

Where the run function has this signature:

```rust
pub fn run<F>(self, event_handler: F) -> !
    where
        F: 'static + FnMut(Event<'_, T>, &EventLoopWindowTarget<T>, &mut ControlFlow),
    {
        self.event_loop.run(event_handler)
    }

```

The error message should ideally include a pointer to the lambda, and a pointer to the declaration of `run` which specifies the `'static` constraint.

Meta: I'm tagging this with "language design" category, but it feels like there should be a "devx" category focused on errors.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [February 7, 2021, 7:05pm UTC](https://internals.rust-lang.org/t/lifetime-error-should-point-out-the-reason-for-a-lifetime/14004/2 "2021-02-07T19:05:28Z")

</div>

Here's a self-contained test case, in case it helps anyone working on this:

```rust
fn main() {
    pub fn run<F>(_: F) where F: 'static + Fn() {}

    let x = String::new();
    let y = std::path::Path::new(&x);
    run(move || { dbg!(y); });
}

```

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

It looks like this is related to [No explanation for source of static lifetime requirement in E0597 · Issue #43531 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/43531)

---

<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 8, 2021, 7:06pm UTC](https://internals.rust-lang.org/t/lifetime-error-should-point-out-the-reason-for-a-lifetime/14004/3 "2021-05-08T19:06:06Z")

</div>

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