Skip to content

When encountering for<'a> in closure declaration, try to provide structured suggestion to use a binding with a type #160431

Description

@estebank

Code

fn main() {
    //let cl: for<'a> fn(&'a str) -> (&'a str, &'a str) = |x| { x.split_at(0) };
    let cl = for<'a> |x: &'a str| -> (&'a str, &'a str) { x.split_at(0) };
    let (_x, _y) = cl("42");
}

Current output

error[E0658]: `for<...>` binders for closures are experimental
 --> f83.rs:3:14
  |
3 |     let cl = for<'a> |x: &'a str| -> (&'a str, &'a str) { x.split_at(0) };
  |              ^^^^^^^
  |
  = note: see issue #97362 <https://github.com/rust-lang/rust/issues/97362> for more information
  = help: add `#![feature(closure_lifetime_binder)]` to the crate attributes to enable
  = help: consider removing `for<...>`

Desired output

error[E0658]: `for<...>` binders for closures are experimental
 --> f83.rs:3:14
  |
3 |     let cl = for<'a> |x: &'a str| -> (&'a str, &'a str) { x.split_at(0) };
  |              ^^^^^^^
  |
  = note: see issue #97362 <https://github.com/rust-lang/rust/issues/97362> for more information
  = help: add `#![feature(closure_lifetime_binder)]` to the crate attributes to enable
  = help: consider setting the binding type instead
  |
3 -     let cl = for<'a> |x: &'a str| -> (&'a str, &'a str) { x.split_at(0) };
3 +     let cl: for<'a> fn(&'a str) -> (&'a str, &'a str) = |x| { x.split_at(0) };
  |

Rationale and extra context

Although the feature is unstable, someone trying to deal with lifetime errors might accidentally arrive to this code, but the feature gate doesn't actually provide reasonable guideline on what to do instead.

Other cases

Rust Version

1.97

Anything else?

Noticed while looking at #58525.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-closure_lifetime_binder`#![feature(closure_lifetime_binder)]`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