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

A out-of-air lifetime that that may not live long vs. the implementation is not general enough #131821

Open
xmh0511 opened this issue Oct 17, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-NLL Area: Non-lexical lifetimes (NLL) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. NLL-diagnostics Working towards the "diagnostic parity" goal T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@xmh0511
Copy link

xmh0511 commented Oct 17, 2024

Code

fn test<F>(f:F)
where F: for<'b> FnOnce(&'b i32){}


fn main(){
    test(|s:&'static i32|{});
}

Current output

  --> src/main.rs:11:11
   |
11 |     test(|s:&'static i32|{});
   |           ^ help: if this is intentional, prefix it with an underscore: `_s`

error: lifetime may not live long enough
  --> src/main.rs:11:11
   |
11 |     test(|s:&'static i32|{});
   |           ^ - let's call the lifetime of this reference `'1`
   |           |
   |           requires that `'1` must outlive `'static`

Desired output

The implementation is not general enough

Rationale and extra context

The closure parameter is explicitly specified as &'static i32, where does the lifetime 'l come from? Moreover, the lifetime 'static should outlive any lifetime, however, the diagnosis instead says

lifetime may not live long enough

Presumably, the implementation of trait FnOnce for the closure type is only for 'static, which is not general enough.

Other cases

No response

Rust Version

rustc 1.81.0 (eeb90cd 2024-09-04)
binary: rustc
commit-hash: eeb90cd
commit-date: 2024-09-04
host: x86_64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Anything else?

No response

@xmh0511 xmh0511 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 Oct 17, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Oct 17, 2024

This error message is not incorrect. The function you passed in is more restrictive than it is expected to be -- the closure is only valid when called with &'static i32, but you passed it to a function that expects to be able to call it with any &i32. edit: Sorry, I thought this was an issue about whether the error was valid.

@compiler-errors
Copy link
Member

Uh, ignoring my last comment, I do think we could probably explain this error message better.

@compiler-errors compiler-errors added A-NLL Area: Non-lexical lifetimes (NLL) NLL-diagnostics Working towards the "diagnostic parity" goal D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Oct 17, 2024
@fmease fmease added the A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) label Oct 17, 2024
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 A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-NLL Area: Non-lexical lifetimes (NLL) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. NLL-diagnostics Working towards the "diagnostic parity" goal 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

3 participants