Skip to content

Commit e439b5d

Browse files
committed
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation
1 parent 2b693c0 commit e439b5d

File tree

27 files changed

+296
-52
lines changed

27 files changed

+296
-52
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ fn exported_symbols_provider_local(
342342
}));
343343
}
344344
MonoItem::Fn(Instance {
345-
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),
345+
def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
346346
args,
347347
}) => {
348348
// A little sanity-check
@@ -353,6 +353,13 @@ fn exported_symbols_provider_local(
353353
used: false,
354354
}));
355355
}
356+
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(_, ty), args: _ }) => {
357+
symbols.push((ExportedSymbol::AsyncDropGlue(ty), SymbolExportInfo {
358+
level: SymbolExportLevel::Rust,
359+
kind: SymbolExportKind::Text,
360+
used: false,
361+
}));
362+
}
356363
_ => {
357364
// Any other symbols don't qualify for sharing
358365
}
@@ -376,6 +383,7 @@ fn upstream_monomorphizations_provider(
376383

377384
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
378385
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
386+
let async_drop_in_place_poll_fn_def_id = tcx.lang_items().async_drop_in_place_poll_fn();
379387

380388
for &cnum in cnums.iter() {
381389
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
@@ -394,8 +402,13 @@ fn upstream_monomorphizations_provider(
394402
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
395403
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
396404
} else {
397-
// `drop_in_place` in place does not exist, don't try
398-
// to use it.
405+
continue;
406+
}
407+
}
408+
ExportedSymbol::AsyncDropGlue(ty) => {
409+
if let Some(poll_fn_def_id) = async_drop_in_place_poll_fn_def_id {
410+
(poll_fn_def_id, tcx.mk_args(&[ty.into()]))
411+
} else {
399412
continue;
400413
}
401414
}
@@ -547,6 +560,13 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
547560
instantiating_crate,
548561
)
549562
}
563+
ExportedSymbol::AsyncDropGlue(ty) => {
564+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
565+
tcx,
566+
Instance::resolve_async_drop_in_place_poll(tcx, ty),
567+
instantiating_crate,
568+
)
569+
}
550570
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
551571
}
552572
}
@@ -598,6 +618,7 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
598618
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
599619
// target's default symbol decoration scheme.
600620
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
621+
ExportedSymbol::AsyncDropGlue(..) => None,
601622
// NoDefId always follow the target's default symbol decoration scheme.
602623
ExportedSymbol::NoDefId(..) => None,
603624
// ThreadLocalShim always follow the target's default symbol decoration scheme.

compiler/rustc_const_eval/src/interpret/call.rs

+2
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
566566
| ty::InstanceKind::FnPtrAddrShim(..)
567567
| ty::InstanceKind::ThreadLocalShim(..)
568568
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
569+
| ty::InstanceKind::AsyncDropGlue(..)
570+
| ty::InstanceKind::FutureDropPollShim(..)
569571
| ty::InstanceKind::Item(_) => {
570572
// We need MIR for this fn.
571573
// Note that this can be an intrinsic, if we are executing its fallback body.

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ language_item_table! {
190190
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
191191
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
192192
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
193+
FutureDropPoll, sym::future_drop_poll, future_drop_poll_fn, Target::Fn, GenericRequirement::Exact(1);
193194

194195
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
195196
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);

compiler/rustc_middle/src/middle/exported_symbols.rs

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub enum ExportedSymbol<'tcx> {
4444
Generic(DefId, GenericArgsRef<'tcx>),
4545
DropGlue(Ty<'tcx>),
4646
AsyncDropGlueCtorShim(Ty<'tcx>),
47+
AsyncDropGlue(Ty<'tcx>),
4748
ThreadLocalShim(DefId),
4849
NoDefId(ty::SymbolName<'tcx>),
4950
}
@@ -63,6 +64,9 @@ impl<'tcx> ExportedSymbol<'tcx> {
6364
ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
6465
tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty))
6566
}
67+
ExportedSymbol::AsyncDropGlue(ty) => {
68+
tcx.symbol_name(ty::Instance::resolve_async_drop_in_place_poll(tcx, ty))
69+
}
6670
ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
6771
def: ty::InstanceKind::ThreadLocalShim(def_id),
6872
args: ty::GenericArgs::empty(),

compiler/rustc_middle/src/mir/mod.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ pub struct CoroutineInfo<'tcx> {
202202
/// Coroutine drop glue. This field is populated after the state transform pass.
203203
pub coroutine_drop: Option<Body<'tcx>>,
204204

205-
/// The layout of a coroutine. This field is populated after the state transform pass.
205+
/// Coroutine async drop glue.
206+
pub coroutine_drop_async: Option<Body<'tcx>>,
207+
208+
/// When coroutine has sync drop, this is async proxy calling `coroutine_drop` sync impl.
209+
pub coroutine_drop_proxy_async: Option<Body<'tcx>>,
210+
211+
/// The layout of a coroutine. Produced by the state transformation.
206212
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,
207213

208214
/// If this is a coroutine then record the type of source expression that caused this coroutine
@@ -222,6 +228,8 @@ impl<'tcx> CoroutineInfo<'tcx> {
222228
yield_ty: Some(yield_ty),
223229
resume_ty: Some(resume_ty),
224230
coroutine_drop: None,
231+
coroutine_drop_async: None,
232+
coroutine_drop_proxy_async: None,
225233
coroutine_layout: None,
226234
}
227235
}
@@ -602,6 +610,26 @@ impl<'tcx> Body<'tcx> {
602610
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop.as_ref())
603611
}
604612

613+
#[inline]
614+
pub fn coroutine_drop_async(&self) -> Option<&Body<'tcx>> {
615+
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop_async.as_ref())
616+
}
617+
618+
#[inline]
619+
pub fn coroutine_requires_async_drop(&self) -> bool {
620+
self.coroutine_drop_async().is_some()
621+
}
622+
623+
#[inline]
624+
pub fn future_drop_poll(&self) -> Option<&Body<'tcx>> {
625+
self.coroutine.as_ref().and_then(|coroutine| {
626+
coroutine
627+
.coroutine_drop_async
628+
.as_ref()
629+
.or(coroutine.coroutine_drop_proxy_async.as_ref())
630+
})
631+
}
632+
605633
#[inline]
606634
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
607635
self.coroutine.as_ref().map(|coroutine| coroutine.coroutine_kind)

compiler/rustc_middle/src/mir/mono.rs

+2
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ impl<'tcx> CodegenUnit<'tcx> {
477477
| InstanceKind::CloneShim(..)
478478
| InstanceKind::ThreadLocalShim(..)
479479
| InstanceKind::FnPtrAddrShim(..)
480+
| InstanceKind::AsyncDropGlue(..)
481+
| InstanceKind::FutureDropPollShim(..)
480482
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
481483
}
482484
}

compiler/rustc_middle/src/mir/pretty.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,20 @@ fn dump_path<'tcx>(
253253
}));
254254
s
255255
}
256-
ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) => {
257-
// Unfortunately, pretty-printed typed are not very filename-friendly.
258-
// We dome some filtering.
256+
ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
257+
let mut s = ".".to_owned();
258+
s.extend(ty.to_string().chars().filter_map(|c| match c {
259+
' ' => None,
260+
':' | '<' | '>' => Some('_'),
261+
c => Some(c),
262+
}));
263+
s
264+
}
265+
ty::InstanceKind::AsyncDropGlue(_, ty) => {
266+
let ty::Coroutine(_, args) = ty.kind() else {
267+
bug!();
268+
};
269+
let ty = args.first().unwrap().expect_ty();
259270
let mut s = ".".to_owned();
260271
s.extend(ty.to_string().chars().filter_map(|c| match c {
261272
' ' => None,
@@ -264,6 +275,21 @@ fn dump_path<'tcx>(
264275
}));
265276
s
266277
}
278+
ty::InstanceKind::FutureDropPollShim(_, proxy_cor, impl_cor) => {
279+
let mut s = ".".to_owned();
280+
s.extend(proxy_cor.to_string().chars().filter_map(|c| match c {
281+
' ' => None,
282+
':' | '<' | '>' => Some('_'),
283+
c => Some(c),
284+
}));
285+
s.push_str(".");
286+
s.extend(impl_cor.to_string().chars().filter_map(|c| match c {
287+
' ' => None,
288+
':' | '<' | '>' => Some('_'),
289+
c => Some(c),
290+
}));
291+
s
292+
}
267293
_ => String::new(),
268294
};
269295

compiler/rustc_middle/src/mir/visit.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,21 @@ macro_rules! make_mir_visitor {
349349
coroutine_closure_def_id: _def_id,
350350
receiver_by_ref: _,
351351
} |
352-
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None) |
353352
ty::InstanceKind::DropGlue(_def_id, None) => {}
354353

355354
ty::InstanceKind::FnPtrShim(_def_id, ty) |
356355
ty::InstanceKind::DropGlue(_def_id, Some(ty)) |
357356
ty::InstanceKind::CloneShim(_def_id, ty) |
358357
ty::InstanceKind::FnPtrAddrShim(_def_id, ty) |
359-
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, Some(ty)) => {
358+
ty::InstanceKind::AsyncDropGlue(_def_id, ty) |
359+
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, ty) => {
360360
// FIXME(eddyb) use a better `TyContext` here.
361361
self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
362362
}
363+
ty::InstanceKind::FutureDropPollShim(_def_id, proxy_ty, impl_ty) => {
364+
self.visit_ty($(& $mutability)? *proxy_ty, TyContext::Location(location));
365+
self.visit_ty($(& $mutability)? *impl_ty, TyContext::Location(location));
366+
}
363367
}
364368
self.visit_args(callee_args, location);
365369
}

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,10 @@ impl<'tcx> TyCtxt<'tcx> {
16301630
self.coroutine_kind(def_id).is_some()
16311631
}
16321632

1633+
pub fn is_templated_coroutine(self, def_id: DefId) -> bool {
1634+
Some(def_id) == self.lang_items().async_drop_in_place_poll_fn()
1635+
}
1636+
16331637
/// Returns the movability of the coroutine of `def_id`, or panics
16341638
/// if given a `def_id` that is not a coroutine.
16351639
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {

0 commit comments

Comments
 (0)