Skip to content

Commit d6bb74b

Browse files
committed
-Zlink-pub-async-impls flag added to monomorphize pub async fn implementation coroutine (func::{closure#0}), when func itself is monomorphized
1 parent 55d4364 commit d6bb74b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

‎compiler/rustc_monomorphize/src/collector.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,20 @@ impl<'v> RootCollector<'_, 'v> {
15101510
fn process_nested_body(&mut self, def_id: LocalDefId) {
15111511
match self.tcx.def_kind(def_id) {
15121512
DefKind::Closure => {
1513-
if self.strategy == MonoItemCollectionStrategy::Eager
1513+
// for 'pub async fn foo(..)' also trying to monomorphize foo::{closure}
1514+
let is_pub_fn_coroutine = self.tcx.sess.opts.unstable_opts.link_pub_async_impls
1515+
&& match *self.tcx.type_of(def_id).instantiate_identity().kind() {
1516+
ty::Coroutine(cor_id, _args) => {
1517+
let tcx = self.tcx;
1518+
let parent_id = tcx.parent(cor_id);
1519+
tcx.def_kind(parent_id) == DefKind::Fn
1520+
&& tcx.asyncness(parent_id).is_async()
1521+
&& tcx.visibility(parent_id).is_public()
1522+
}
1523+
ty::Closure(..) | ty::CoroutineClosure(..) => false,
1524+
_ => unreachable!(),
1525+
};
1526+
if (self.strategy == MonoItemCollectionStrategy::Eager || is_pub_fn_coroutine)
15141527
&& !self
15151528
.tcx
15161529
.generics_of(self.tcx.typeck_root_def_id(def_id.to_def_id()))

‎compiler/rustc_session/src/options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,10 @@ options! {
22812281
"link native libraries in the linker invocation (default: yes)"),
22822282
link_only: bool = (false, parse_bool, [TRACKED],
22832283
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
2284+
link_pub_async_impls: bool = (false, parse_bool, [TRACKED],
2285+
"monomorphize pub async function implementation coroutine, func::{closure}, \
2286+
when func itself is monomorphized (default: no)
2287+
Should optimize compilation time for projects with async fn lib dependencies"),
22842288
linker_features: LinkerFeaturesCli = (LinkerFeaturesCli::default(), parse_linker_features, [UNTRACKED],
22852289
"a comma-separated list of linker features to enable (+) or disable (-): `lld`"),
22862290
lint_llvm_ir: bool = (false, parse_bool, [TRACKED],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ edition: 2024
2+
// When pub async fn is monomorphized, its implementation coroutine is also monomorphized
3+
// (with -Zlink-pub-async-impls)
4+
//@ compile-flags: -Zlink-pub-async-impls --crate-type=lib
5+
6+
//~ MONO_ITEM fn async_fn @@
7+
//~ MONO_ITEM fn async_fn::{closure#0} @@
8+
#[unsafe(no_mangle)]
9+
pub async fn async_fn(x: u64) -> bool {
10+
true
11+
}

0 commit comments

Comments
 (0)