Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime must be 'static, but why? #138408

Open
timokoesters opened this issue Mar 12, 2025 · 0 comments
Open

Lifetime must be 'static, but why? #138408

timokoesters opened this issue Mar 12, 2025 · 0 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@timokoesters
Copy link

timokoesters commented Mar 12, 2025

Code

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f9243c5a93b40649aaa99f20dc78d5b0

fn input(_: &'static str) {}

fn main() {
    let x = String::new();
    input(&x);
}

Current output

error[E0597]: `x` does not live long enough
 --> src/main.rs:5:11
  |
4 |     let x = String::new();
  |         - binding `x` declared here
5 |     input(&x);
  |     ------^^-
  |     |     |
  |     |     borrowed value does not live long enough
  |     argument requires that `x` is borrowed for `'static`
6 | }
  | - `x` dropped here while still borrowed

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

Desired output

Maybe something like

hint: lifetime must be 'static to satisfy function
fn input(_: &'static str) {}

Rationale and extra context

In the example above it is also easy to figure it out yourself, but I ran into a case where the actual function is hidden:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4c1003f809e67aec7ded5ea736ab0430
pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder
impl From<&'static str> for Body

In this case the message should directly point to the from implementation.

Rust Version

I tried this on the Rust playground on Rust 1.85.0

@timokoesters timokoesters added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 12, 2025
@fmease fmease added A-lifetimes Area: Lifetimes / regions D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-borrow-checker Area: The borrow checker labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants