Repository: rust
Issue: Lifetime error is missing the cause
Labels: A-diagnostics,A-lifetimes,A-borrow-checker,T-compiler,D-terse

The code below is somewhat synthetic, but hopefully demonstrates the issue well enough. 

The current error is technically correct, but is not nearly helpful enough. I would expect some indication of which function or type adds the  `'static` requirement. In this case, it is only required because of the `?` and `anyhow::Error` requiring `'static` lifetime for its errors.

It was very hard to track down where this 'static requirement was coming from in a real codebase -- the current error sends you down the function you call, even though the requirement comes from elsewhere. I would find it very helpful if this error showed the first function/type that requires a longer lifetime (in this case that would be the `?`, or `anyhow::Result`).


Given the following code ([Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7b243ee9f5bb026fddeef6505a7d5c94)):

```rust
use std::error::Error;
use std::fmt::{Display, Formatter};

fn main() -> anyhow::Result<()> {
    let string: String = "abcdef".to_string();

    let alphabet = is_alphabet(&string)?;

    println!("{alphabet} is an alphabet");
    Ok(())
}

fn is_alphabet(i: &str) -> Result<&str, AlphabetError> {
    if i == "abcdef" {
        Ok(i)
    } else {
        Err(AlphabetError::NotTheAlphabet(i))
    }
}

#[derive(Debug)]
enum AlphabetError<'a> {
    NotTheAlphabet(&'a str)
}

impl<'a> Error for AlphabetError<'a> {}

impl<'a> Display for AlphabetError<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            AlphabetError::NotTheAlphabet(string) => write!(f, "{string} is not an alphabet"),
        }
    }
}
```

The current output is:

```
error[[E0597]](https://doc.rust-lang.org/stable/error-index.html#E0597): `string` does not live long enough
  --> src/main.rs:7:32
   |
7  |     let alphabet = is_alphabet(&string)?;
   |                    ------------^^^^^^^-
   |                    |           |
   |                    |           borrowed value does not live long enough
   |                    argument requires that `string` is borrowed for `'static`
...
11 | }
   | - `string` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
```

Ideally the output should look something like this:

```
error[[E0597]](https://doc.rust-lang.org/stable/error-index.html#E0597): `string` does not live long enough
  --> src/main.rs:7:32
4  |     fn main() -> anyhow::Result<()> {
   |                          ^^^^^^
   |                          |
   |                          hint: anyhow::Result<()> requires borrowing for `'static`
   |
7  |     let alphabet = is_alphabet(&string)?;
   |                    ------------^^^^^^^-
   |                    |           |
   |                    |           borrowed value does not live long enough
   |                    argument requires that `string` is borrowed for `'static`
...
11 | }
   | - `string` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

```