From c210fec3cba5776f62c8df0f3ecdac2d6bfd1c9d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 10 Feb 2024 22:58:23 +0000 Subject: [PATCH] Encode coroutine_for_closure for foreign crates --- .../src/rmeta/decoder/cstore_impl.rs | 1 + compiler/rustc_metadata/src/rmeta/encoder.rs | 7 +++++++ compiler/rustc_metadata/src/rmeta/mod.rs | 1 + .../async-closures/auxiliary/foreign.rs | 7 +++++++ .../ui/async-await/async-closures/foreign.rs | 19 +++++++++++++++++++ 5 files changed, 35 insertions(+) create mode 100644 tests/ui/async-await/async-closures/auxiliary/foreign.rs create mode 100644 tests/ui/async-await/async-closures/foreign.rs diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 7cd2f58779f..988388edfd5 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -250,6 +250,7 @@ provide! { tcx, def_id, other, cdata, asyncness => { table_direct } fn_arg_names => { table } coroutine_kind => { table_direct } + coroutine_for_closure => { table } trait_def => { table } deduced_param_attrs => { table } is_type_alias_impl_trait => { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 79aa9a547f7..6ca1973396f 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1447,6 +1447,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { { self.tables.coroutine_kind.set(def_id.index, Some(coroutine_kind)) } + if def_kind == DefKind::Closure + && tcx.type_of(def_id).skip_binder().is_coroutine_closure() + { + self.tables + .coroutine_for_closure + .set_some(def_id.index, self.tcx.coroutine_for_closure(def_id).into()); + } if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind { self.encode_info_for_adt(local_id); } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 2f775882693..8205e995c19 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -443,6 +443,7 @@ define_tables! { asyncness: Table, fn_arg_names: Table>, coroutine_kind: Table, + coroutine_for_closure: Table, trait_def: Table>, trait_item_def_id: Table, expn_that_defined: Table>, diff --git a/tests/ui/async-await/async-closures/auxiliary/foreign.rs b/tests/ui/async-await/async-closures/auxiliary/foreign.rs new file mode 100644 index 00000000000..e11dfc22213 --- /dev/null +++ b/tests/ui/async-await/async-closures/auxiliary/foreign.rs @@ -0,0 +1,7 @@ +// edition:2021 + +#![feature(async_closure)] + +pub fn closure() -> impl async Fn() { + async || { /* Don't really need to do anything here. */ } +} diff --git a/tests/ui/async-await/async-closures/foreign.rs b/tests/ui/async-await/async-closures/foreign.rs new file mode 100644 index 00000000000..60fea909801 --- /dev/null +++ b/tests/ui/async-await/async-closures/foreign.rs @@ -0,0 +1,19 @@ +// aux-build:block-on.rs +// aux-build:foreign.rs +// edition:2021 +// build-pass + +#![feature(async_closure)] + +use std::future::Future; + +extern crate block_on; +extern crate foreign; + +struct NoCopy; + +fn main() { + block_on::block_on(async { + foreign::closure()().await; + }); +}