diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_error_codes/src/error_codes/E0733.md | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_error_codes/src/error_codes/E0733.md')
-rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0733.md | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0733.md b/compiler/rustc_error_codes/src/error_codes/E0733.md new file mode 100644 index 000000000..051b75148 --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0733.md @@ -0,0 +1,44 @@ +An [`async`] function used recursion without boxing. + +Erroneous code example: + +```edition2018,compile_fail,E0733 +async fn foo(n: usize) { + if n > 0 { + foo(n - 1).await; + } +} +``` + +To perform async recursion, the `async fn` needs to be desugared such that the +`Future` is explicit in the return type: + +```edition2018,compile_fail,E0720 +use std::future::Future; +fn foo_desugared(n: usize) -> impl Future<Output = ()> { + async move { + if n > 0 { + foo_desugared(n - 1).await; + } + } +} +``` + +Finally, the future is wrapped in a pinned box: + +```edition2018 +use std::future::Future; +use std::pin::Pin; +fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> { + Box::pin(async move { + if n > 0 { + foo_recursive(n - 1).await; + } + }) +} +``` + +The `Box<...>` ensures that the result is of known size, and the pin is +required to keep it in the same place in memory. + +[`async`]: https://doc.rust-lang.org/std/keyword.async.html |