Skip to content

Lifetime error doesn't point to causing line #138900

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

Closed
VojtaStanek opened this issue Mar 24, 2025 · 2 comments
Closed

Lifetime error doesn't point to causing line #138900

VojtaStanek opened this issue Mar 24, 2025 · 2 comments
Labels
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.

Comments

@VojtaStanek
Copy link

VojtaStanek commented Mar 24, 2025

Code

struct A;

struct B(&'static A);

async fn some_function(a: &mut A) {
    let _b = B(a);
}

Current output

Compiling playground v0.0.1 (/playground)
error: lifetime may not live long enough
 --> src/main.rs:5:24
  |
5 | async fn some_function(a: &mut A) {
  |                        ^  - let's call the lifetime of this reference `'1`
  |                        |
  |                        assignment requires that `'1` must outlive `'static`

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

Desired output

It should point to struct B since it is causing the lifetime of a ('1) to outlive 'static.

Rationale and extra context

Currently the compiler doesn't guide me to the line of causing the 'static requirement.

Other cases

Rust Version

> rustc --version --verbose
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

Maybe related to #69224

@VojtaStanek VojtaStanek 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 24, 2025
@compiler-errors
Copy link
Member

On nightly, it's a bit more descriptive:

error: lifetime may not live long enough
 --> src/lib.rs:6:16
  |
5 | async fn some_function(a: &mut A) {
  |                           - let's call the lifetime of this reference `'1`
6 |     let _b = B(a);
  |                ^ this usage requires that `'1` must outlive `'static`

@VojtaStanek
Copy link
Author

@compiler-errors This is a significant improvement and help me identify the problem in my actual codebase instantly. 👍

This enhancement is also present in version 1.86-beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

2 participants