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

Lifetime inference fails for closures returning references to their parameters #125124

Closed
0xdeafbeef opened this issue May 14, 2024 · 2 comments
Closed
Labels
A-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@0xdeafbeef
Copy link

0xdeafbeef commented May 14, 2024

I tried this code:

#[derive(Default)]
struct Struct {
    field: String,
}

fn consume_closure<'a, F>(data: &'a Struct, f: F) -> &'a String
where
    F: Fn(&'a Struct) -> &'a String,
{
    f(data)
}

fn main() {
    let s = Struct::default();
    let naive = |f: &Struct| &f.field;
    consume_closure(&s, naive);
}

I expected to see this happen: code compiles

Instead, this happened:

   |
18 |     let naive = |f: &Struct| &f.field;
   |                     -      - ^^^^^^^^ returning this value requires that `'1` must outlive `'2`
   |                     |      |
   |                     |      return type of closure is &'2 String
   |                     let's call the lifetime of this reference `'1`

This workaround involves defining a helper function just to handle the lifetimes explicitly, which complicates the code unnecessarily. It feels like a cumbersome solution to what should be a simpler problem.

fn identity<'a, F, A, R>(f: F) -> F
where
    F: Fn(A) -> R,
    A: 'a,
    R: 'a,
{
    f
}

let f = identity(move |point: &Struct| -> &String { &point.field });
let s = Struct::default();
consume_closure(&s, f);

full example

@0xdeafbeef 0xdeafbeef added the C-bug Category: This is a bug. label May 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 14, 2024
@0xdeafbeef
Copy link
Author

@rustbot label A-closures

@rustbot rustbot added the A-closures Area: Closures (`|…| { … }`) label May 14, 2024
@fmease fmease added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 14, 2024
@fmease
Copy link
Member

fmease commented May 14, 2024

Duplicate of #58052. Nightly solution: Feature closure_lifetime_binder.
CC #116869, #111662.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. 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