Skip to content

closure-returning-async-block suggestion causes does not live long enough error #142145

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

Open
matthiaskrgr opened this issue Jun 7, 2025 · 0 comments
Labels
A-async-await Area: Async & Await A-async-closures `async || {}` A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Using the following flags

--force-warn closure-returning-async-block

this code:

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(|| async move { copyable })
}

caused the following diagnostics:

    Checking _snippet_3 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.e9LHZQ3h7hng/_snippet_3)
warning: closure returning async block can be made into an async closure
  --> src/main.rs:10:14
   |
10 |     needs_fn(|| async move { copyable })
   |              ^^ ---------- this async block can be removed, and the closure can be turned into an async closure
   |
   = note: requested on the command line with `--force-warn closure-returning-async-block`
help: turn this into an async closure
   |
10 -     needs_fn(|| async move { copyable })
10 +     needs_fn(async || { copyable })
   |

warning: `_snippet_3` (bin "_snippet_3") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

However after applying these diagnostics, the resulting code:

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(async || { copyable })
}

no longer compiled:

    Checking _snippet_3 v0.1.0 (/tmp/icemaker_global_tempdir.VQhomPU3g4RT/icemaker_clippyfix_tempdir.e9LHZQ3h7hng/_snippet_3)
error[E0597]: `copyable` does not live long enough
  --> src/main.rs:10:14
   |
10 |     needs_fn(async || { copyable })
   |     ---------^^^^^^^^^^^^^^^^^^^^^-
   |     |        |
   |     |        borrowed value does not live long enough
   |     opaque type requires that `copyable` is borrowed for `'static`
11 | }
   | - `copyable` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `_snippet_3` (bin "_snippet_3") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_3` (bin "_snippet_3" test) due to 1 previous error

Version:

rustc 1.89.0-nightly (44f415c1d 2025-06-06)
binary: rustc
commit-hash: 44f415c1d617ebc7b931a243b7b321ef8a6ca47c
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

@matthiaskrgr matthiaskrgr added the C-bug Category: This is a bug. label Jun 7, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 7, 2025
@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints A-async-await Area: Async & Await A-async-closures `async || {}` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jun 7, 2025
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-async-closures `async || {}` A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. 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

2 participants