Skip to content

Commit dfc9b26

Browse files
committed
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation
1 parent 4efe2f2 commit dfc9b26

File tree

27 files changed

+299
-52
lines changed

27 files changed

+299
-52
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn exported_symbols_provider_local(
372372
));
373373
}
374374
MonoItem::Fn(Instance {
375-
def: InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)),
375+
def: InstanceKind::AsyncDropGlueCtorShim(_, ty),
376376
args,
377377
}) => {
378378
// A little sanity-check
@@ -386,6 +386,16 @@ fn exported_symbols_provider_local(
386386
},
387387
));
388388
}
389+
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(_, ty), args: _ }) => {
390+
symbols.push((
391+
ExportedSymbol::AsyncDropGlue(ty),
392+
SymbolExportInfo {
393+
level: SymbolExportLevel::Rust,
394+
kind: SymbolExportKind::Text,
395+
used: false,
396+
},
397+
));
398+
}
389399
_ => {
390400
// Any other symbols don't qualify for sharing
391401
}
@@ -409,6 +419,7 @@ fn upstream_monomorphizations_provider(
409419

410420
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
411421
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
422+
let async_drop_in_place_poll_fn_def_id = tcx.lang_items().async_drop_in_place_poll_fn();
412423

413424
for &cnum in cnums.iter() {
414425
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
@@ -427,8 +438,13 @@ fn upstream_monomorphizations_provider(
427438
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
428439
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
429440
} else {
430-
// `drop_in_place` in place does not exist, don't try
431-
// to use it.
441+
continue;
442+
}
443+
}
444+
ExportedSymbol::AsyncDropGlue(ty) => {
445+
if let Some(poll_fn_def_id) = async_drop_in_place_poll_fn_def_id {
446+
(poll_fn_def_id, tcx.mk_args(&[ty.into()]))
447+
} else {
432448
continue;
433449
}
434450
}
@@ -580,6 +596,13 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
580596
instantiating_crate,
581597
)
582598
}
599+
ExportedSymbol::AsyncDropGlue(ty) => {
600+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
601+
tcx,
602+
Instance::resolve_async_drop_in_place_poll(tcx, ty),
603+
instantiating_crate,
604+
)
605+
}
583606
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
584607
}
585608
}
@@ -631,6 +654,7 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
631654
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
632655
// target's default symbol decoration scheme.
633656
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
657+
ExportedSymbol::AsyncDropGlue(..) => None,
634658
// NoDefId always follow the target's default symbol decoration scheme.
635659
ExportedSymbol::NoDefId(..) => None,
636660
// 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
@@ -569,6 +569,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
569569
| ty::InstanceKind::FnPtrAddrShim(..)
570570
| ty::InstanceKind::ThreadLocalShim(..)
571571
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
572+
| ty::InstanceKind::AsyncDropGlue(..)
573+
| ty::InstanceKind::FutureDropPollShim(..)
572574
| ty::InstanceKind::Item(_) => {
573575
// We need MIR for this fn.
574576
// 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
@@ -475,6 +475,8 @@ impl<'tcx> CodegenUnit<'tcx> {
475475
| InstanceKind::CloneShim(..)
476476
| InstanceKind::ThreadLocalShim(..)
477477
| InstanceKind::FnPtrAddrShim(..)
478+
| InstanceKind::AsyncDropGlue(..)
479+
| InstanceKind::FutureDropPollShim(..)
478480
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
479481
}
480482
}

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
@@ -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
@@ -1633,6 +1633,10 @@ impl<'tcx> TyCtxt<'tcx> {
16331633
self.coroutine_kind(def_id).is_some()
16341634
}
16351635

1636+
pub fn is_templated_coroutine(self, def_id: DefId) -> bool {
1637+
Some(def_id) == self.lang_items().async_drop_in_place_poll_fn()
1638+
}
1639+
16361640
/// Returns the movability of the coroutine of `def_id`, or panics
16371641
/// if given a `def_id` that is not a coroutine.
16381642
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {

0 commit comments

Comments
 (0)