Skip to content

E0507: AsyncFn vs AsyncFnOnce not given as cause of error, unlike Fn vs FnOnce #150174

@mqudsi

Description

@mqudsi

Contrived example (that might be possible to distill further, but it's short enough and gets the job done):

pub async fn in_transaction<F>(callback: F) -> Result<(), ()> where F: AsyncFn() -> Result<(), ()>, { let result = callback().await?; Ok(result) } struct Foo; async fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> { in_transaction(async || -> Result<(), ()> { for foo in foos { drop(foo) } Ok(()) }).await?; Ok(()) }

This generates the following error on 1.92 stable (playground link):

error[E0507]: cannot move out of `foos` which is behind a shared reference --> src/lib.rs:12:20 | 11 | async fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> { | ---- move occurs because `foos` has type `&mut [&mut Foo]`, which does not implement the `Copy` trait 12 | in_transaction(async || -> Result<(), ()> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `foos` is moved here 13 | for foo in foos { | ---- variable moved due to use in coroutine For more information about this error, try `rustc --explain E0507`. error: could not compile `playground` (lib) due to 1 previous error 

Nothing you do at the call site will address this (short of making Foo: Copy). The fundamental underlying issue is the usage of AsyncFn instead of AsyncFnOnce, but this isn't even remotely hinted at by the errors.

Contrast with the same code but not async, using Fn instead of FnOnce (playground link):

pub fn in_transaction<F>(callback: F) -> Result<(), ()> where F: Fn() -> Result<(), ()>, { let result = callback()?; Ok(result) } struct Foo; fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> { in_transaction(|| { for foo in foos { drop(foo); } Ok(()) })?; Ok(()) }

which generates a much better and actually useful error message:

error[E0507]: cannot move out of `foos`, a captured variable in an `Fn` closure --> src/lib.rs:13:20 | 11 | fn foo(foos: &mut [&mut Foo]) -> Result<(), ()> { | ---- --------------- move occurs because `foos` has type `&mut [&mut Foo]`, which does not implement the `Copy` trait | | | captured outer variable 12 | in_transaction(|| { | -- captured by this `Fn` closure 13 | for foo in foos { | ^^^^ | | | `foos` moved due to this implicit call to `.into_iter()` | `foos` is moved here | help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but `FnOnce` closures may consume them only once --> src/lib.rs:3:8 | 3 | F: Fn() -> Result<(), ()>, | ^^^^^^^^^^^^^^^^^^^^^^ note: `into_iter` takes ownership of the receiver `self`, which moves `foos` --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/collect.rs:310:18 | 310 | fn into_iter(self) -> Self::IntoIter; | ^^^^ help: consider creating a fresh reborrow of `foos` here | 13 | for foo in &mut *foos { | ++++++ 

Naturally, simply changing the AsyncFn to AsyncFnOnce (which, in this case, is possible), eliminates the error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=9a95c91fc4f1df30c06a34f42877ba5a

@rustbot label +A-diagnostics +T-compiler +D-lack-of-suggestion +A-async-closures +C-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-closures`async || {}`A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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