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

Detect cases of lifetime errors caused by closures using a lifetime of its enclosing item and suggest anon lifetime #100572

Open
estebank opened this issue Aug 15, 2022 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

fn test<'a>()
where
    'a: 'a, // <- makes sure `'a` is early-bound; late-bound lifetimes are already rejected
{
    let f = |_: &'a str| {};
    f(&String::new());
}

compiles on 1.63 but will stop compiling in 1.64 with the following error because of #98835 (comment) (#100544):

error[E0716]: temporary value dropped while borrowed
 --> src/lib.rs:6:8
  |
1 | fn test<'a>()
  |         -- lifetime `'a` defined here
...
6 |     f(&String::new());
  |     ---^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
  |     |  |
  |     |  creates a temporary which is freed while still in use
  |     argument requires that borrow lasts for `'a`

We should detect cases of lifetime errors caused by closures using a lifetime of its enclosing item and suggest using an anonymous lifetime when appropriate to make the code compile.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Aug 15, 2022
@estebank
Copy link
Contributor Author

CC #100002

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-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant