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

Compiler Suggests Cloning an async Block #121547

Closed
HTGAzureX1212 opened this issue Feb 24, 2024 · 2 comments · Fixed by #122589
Closed

Compiler Suggests Cloning an async Block #121547

HTGAzureX1212 opened this issue Feb 24, 2024 · 2 comments · Fixed by #122589
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@HTGAzureX1212
Copy link
Contributor

Code

// https://github.com/HTGAzureX1212/HarTex/blob/491fe72da9042d5a5c7f61d0d5cdaf6e9b112057/discord-frontend/hartex-discord-leader/src/kafka.rs#L79-L120

Current output

error[E0382]: use of moved value: `producer`
   --> hartex-discord-leader\src\kafka.rs:78:23
    |
68  |   async fn inbound<'a, Q>(shards: Vec<&'a mut Shard<Q>>, producer: FutureProducer) -> miette::Result<()>
    |                                                          -------- move occurs because `producer` has type `FutureProducer`, which does not implement the `Copy` trait
...
74  |       loop {
    |       ---- inside of this loop
...
77  |           for shard in shards {
    |           ------------------- inside of this loop
78  |               set.spawn(async move {
    |  _______________________^
79  | |                 while let Some(result) = shard.next().await {
80  | |                     match result {
81  | |                         Ok(message) => {
...   |
95  | |                             if let Err((error, _)) = producer
    | |                                                      -------- use occurs due to use in coroutine
...   |
119 | |                 }
120 | |             });
    | |_____________^ value moved here, in previous iteration of loop
    |
help: consider cloning the value if the performance cost is acceptable
    |
120 |             }.clone());
    |              ++++++++

Desired output

error[E0382]: use of moved value: `producer`
   --> hartex-discord-leader\src\kafka.rs:78:23
    |
68  |   async fn inbound<'a, Q>(shards: Vec<&'a mut Shard<Q>>, producer: FutureProducer) -> miette::Result<()>
    |                                                          -------- move occurs because `producer` has type `FutureProducer`, which does not implement the `Copy` trait
...
74  |       loop {
    |       ---- inside of this loop
...
77  |           for shard in shards {
    |           ------------------- inside of this loop
78  |               set.spawn(async move {
    |  _______________________^
79  | |                 while let Some(result) = shard.next().await {
80  | |                     match result {
81  | |                         Ok(message) => {
...   |
95  | |                             if let Err((error, _)) = producer
    | |                                                      -------- use occurs due to use in coroutine
...   |
119 | |                 }
120 | |             });
    | |_____________^ value moved here, in previous iteration of loop
    |
help: consider cloning the value if the performance cost is acceptable
    |
95  |                               if let Err((error, _)) = producer.clone()
    |                                                                ++++++++

Rationale and extra context

I expect that the .clone() invocation to be added for the value that is moved, but for some reason it is doing it for the entire async closure.

Other cases

No response

Rust Version

rustc 1.78.0-nightly (8f359beca 2024-02-23)
binary: rustc
commit-hash: 8f359beca4e58bc3ae795a666301a8f47023044c
commit-date: 2024-02-23
host: x86_64-pc-windows-msvc
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

No response

@HTGAzureX1212 HTGAzureX1212 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2024
@HTGAzureX1212
Copy link
Contributor Author

@rustbot label +E-needs-mcve

@rustbot rustbot added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Feb 24, 2024
@compiler-errors compiler-errors changed the title Compiler Suggests Cloning an async Closure Compiler Suggests Cloning an async Block Feb 24, 2024
@jieyouxu jieyouxu added the A-borrow-checker Area: The borrow checker label Feb 24, 2024
@GrigorenkoPV
Copy link
Contributor

GrigorenkoPV commented Feb 26, 2024

MCVE

async fn clone_async_block(value: String) {
    for _ in 0..10 {
        drop(async { drop(value) })
    }
}

or

async fn clone_async_block(value: String) {
    for _ in 0..10 {
        async { drop(value) }.await
    }
}

@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Feb 28, 2024
@bors bors closed this as completed in 20770ac Mar 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 27, 2024
Rollup merge of rust-lang#122589 - wutchzone:121547, r=compiler-errors

Fix diagnostics for async block cloning

Closes rust-lang#121547

r? diagnostics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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.

4 participants