Skip to content

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

Open
@estebank

Description

@estebank
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions