Skip to content

Commit 9a0fb73

Browse files
committed
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation
1 parent b1f3a6e commit 9a0fb73

File tree

27 files changed

+298
-51
lines changed

27 files changed

+298
-51
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fn exported_symbols_provider_local(
374374
));
375375
}
376376
MonoItem::Fn(Instance {
377-
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),
377+
def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
378378
args,
379379
}) => {
380380
// A little sanity-check
@@ -388,6 +388,16 @@ fn exported_symbols_provider_local(
388388
},
389389
));
390390
}
391+
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(_, ty), args: _ }) => {
392+
symbols.push((
393+
ExportedSymbol::AsyncDropGlue(ty),
394+
SymbolExportInfo {
395+
level: SymbolExportLevel::Rust,
396+
kind: SymbolExportKind::Text,
397+
used: false,
398+
},
399+
));
400+
}
391401
_ => {
392402
// Any other symbols don't qualify for sharing
393403
}
@@ -411,6 +421,7 @@ fn upstream_monomorphizations_provider(
411421

412422
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
413423
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
424+
let async_drop_in_place_poll_fn_def_id = tcx.lang_items().async_drop_in_place_poll_fn();
414425

415426
for &cnum in cnums.iter() {
416427
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
@@ -429,8 +440,13 @@ fn upstream_monomorphizations_provider(
429440
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
430441
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
431442
} else {
432-
// `drop_in_place` in place does not exist, don't try
433-
// to use it.
443+
continue;
444+
}
445+
}
446+
ExportedSymbol::AsyncDropGlue(ty) => {
447+
if let Some(poll_fn_def_id) = async_drop_in_place_poll_fn_def_id {
448+
(poll_fn_def_id, tcx.mk_args(&[ty.into()]))
449+
} else {
434450
continue;
435451
}
436452
}
@@ -582,6 +598,13 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
582598
instantiating_crate,
583599
)
584600
}
601+
ExportedSymbol::AsyncDropGlue(ty) => {
602+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
603+
tcx,
604+
Instance::resolve_async_drop_in_place_poll(tcx, ty),
605+
instantiating_crate,
606+
)
607+
}
585608
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
586609
}
587610
}
@@ -604,6 +627,7 @@ fn calling_convention_for_symbol<'tcx>(
604627
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
605628
// target's default symbol decoration scheme.
606629
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
630+
ExportedSymbol::AsyncDropGlue(..) => None,
607631
// NoDefId always follow the target's default symbol decoration scheme.
608632
ExportedSymbol::NoDefId(..) => None,
609633
// 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
@@ -570,6 +570,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
570570
| ty::InstanceKind::FnPtrAddrShim(..)
571571
| ty::InstanceKind::ThreadLocalShim(..)
572572
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
573+
| ty::InstanceKind::AsyncDropGlue(..)
574+
| ty::InstanceKind::FutureDropPollShim(..)
573575
| ty::InstanceKind::Item(_) => {
574576
// We need MIR for this fn.
575577
// 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
@@ -191,6 +191,7 @@ language_item_table! {
191191
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
192192
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
193193
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
194+
FutureDropPoll, sym::future_drop_poll, future_drop_poll_fn, Target::Fn, GenericRequirement::Exact(1);
194195

195196
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
196197
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
@@ -200,7 +200,13 @@ pub struct CoroutineInfo<'tcx> {
200200
/// Coroutine drop glue. This field is populated after the state transform pass.
201201
pub coroutine_drop: Option<Body<'tcx>>,
202202

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

206212
/// If this is a coroutine then record the type of source expression that caused this coroutine
@@ -220,6 +226,8 @@ impl<'tcx> CoroutineInfo<'tcx> {
220226
yield_ty: Some(yield_ty),
221227
resume_ty: Some(resume_ty),
222228
coroutine_drop: None,
229+
coroutine_drop_async: None,
230+
coroutine_drop_proxy_async: None,
223231
coroutine_layout: None,
224232
}
225233
}
@@ -587,6 +595,26 @@ impl<'tcx> Body<'tcx> {
587595
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop.as_ref())
588596
}
589597

598+
#[inline]
599+
pub fn coroutine_drop_async(&self) -> Option<&Body<'tcx>> {
600+
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop_async.as_ref())
601+
}
602+
603+
#[inline]
604+
pub fn coroutine_requires_async_drop(&self) -> bool {
605+
self.coroutine_drop_async().is_some()
606+
}
607+
608+
#[inline]
609+
pub fn future_drop_poll(&self) -> Option<&Body<'tcx>> {
610+
self.coroutine.as_ref().and_then(|coroutine| {
611+
coroutine
612+
.coroutine_drop_async
613+
.as_ref()
614+
.or(coroutine.coroutine_drop_proxy_async.as_ref())
615+
})
616+
}
617+
590618
#[inline]
591619
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
592620
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
@@ -530,6 +530,8 @@ impl<'tcx> CodegenUnit<'tcx> {
530530
| InstanceKind::CloneShim(..)
531531
| InstanceKind::ThreadLocalShim(..)
532532
| InstanceKind::FnPtrAddrShim(..)
533+
| InstanceKind::AsyncDropGlue(..)
534+
| InstanceKind::FutureDropPollShim(..)
533535
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
534536
}
535537
}

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('.');
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
@@ -353,17 +353,21 @@ macro_rules! make_mir_visitor {
353353
coroutine_closure_def_id: _def_id,
354354
receiver_by_ref: _,
355355
}
356-
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None)
357356
| ty::InstanceKind::DropGlue(_def_id, None) => {}
358357

359358
ty::InstanceKind::FnPtrShim(_def_id, ty)
360359
| ty::InstanceKind::DropGlue(_def_id, Some(ty))
361360
| ty::InstanceKind::CloneShim(_def_id, ty)
362361
| ty::InstanceKind::FnPtrAddrShim(_def_id, ty)
363-
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, Some(ty)) => {
362+
| ty::InstanceKind::AsyncDropGlue(_def_id, ty)
363+
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, ty) => {
364364
// FIXME(eddyb) use a better `TyContext` here.
365365
self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
366366
}
367+
ty::InstanceKind::FutureDropPollShim(_def_id, proxy_ty, impl_ty) => {
368+
self.visit_ty($(& $mutability)? *proxy_ty, TyContext::Location(location));
369+
self.visit_ty($(& $mutability)? *impl_ty, TyContext::Location(location));
370+
}
367371
}
368372
self.visit_args(callee_args, location);
369373
}

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,10 @@ impl<'tcx> TyCtxt<'tcx> {
16911691
self.coroutine_kind(def_id).is_some()
16921692
}
16931693

1694+
pub fn is_templated_coroutine(self, def_id: DefId) -> bool {
1695+
Some(def_id) == self.lang_items().async_drop_in_place_poll_fn()
1696+
}
1697+
16941698
/// Returns the movability of the coroutine of `def_id`, or panics
16951699
/// if given a `def_id` that is not a coroutine.
16961700
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {

0 commit comments

Comments
 (0)