Skip to content

Commit 6f6d305

Browse files
committed
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation
1 parent b667246 commit 6f6d305

File tree

27 files changed

+305
-53
lines changed

27 files changed

+305
-53
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn exported_symbols_provider_local(
364364
));
365365
}
366366
MonoItem::Fn(Instance {
367-
def: InstanceKind::AsyncDropGlueCtorShim(def_id, Some(ty)),
367+
def: InstanceKind::AsyncDropGlueCtorShim(def_id, ty),
368368
args,
369369
}) => {
370370
// A little sanity-check
@@ -381,6 +381,16 @@ fn exported_symbols_provider_local(
381381
},
382382
));
383383
}
384+
MonoItem::Fn(Instance { def: InstanceKind::AsyncDropGlue(_, ty), args: _ }) => {
385+
symbols.push((
386+
ExportedSymbol::AsyncDropGlue(ty),
387+
SymbolExportInfo {
388+
level: SymbolExportLevel::Rust,
389+
kind: SymbolExportKind::Text,
390+
used: false,
391+
},
392+
));
393+
}
384394
_ => {
385395
// Any other symbols don't qualify for sharing
386396
}
@@ -404,6 +414,7 @@ fn upstream_monomorphizations_provider(
404414

405415
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
406416
let async_drop_in_place_fn_def_id = tcx.lang_items().async_drop_in_place_fn();
417+
let async_drop_in_place_poll_fn_def_id = tcx.lang_items().async_drop_in_place_poll_fn();
407418

408419
for &cnum in cnums.iter() {
409420
for (exported_symbol, _) in tcx.exported_symbols(cnum).iter() {
@@ -422,8 +433,13 @@ fn upstream_monomorphizations_provider(
422433
if let Some(async_drop_in_place_fn_def_id) = async_drop_in_place_fn_def_id {
423434
(async_drop_in_place_fn_def_id, tcx.mk_args(&[ty.into()]))
424435
} else {
425-
// `drop_in_place` in place does not exist, don't try
426-
// to use it.
436+
continue;
437+
}
438+
}
439+
ExportedSymbol::AsyncDropGlue(ty) => {
440+
if let Some(poll_fn_def_id) = async_drop_in_place_poll_fn_def_id {
441+
(poll_fn_def_id, tcx.mk_args(&[ty.into()]))
442+
} else {
427443
continue;
428444
}
429445
}
@@ -575,6 +591,13 @@ pub fn symbol_name_for_instance_in_crate<'tcx>(
575591
instantiating_crate,
576592
)
577593
}
594+
ExportedSymbol::AsyncDropGlue(ty) => {
595+
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
596+
tcx,
597+
Instance::resolve_async_drop_in_place_poll(tcx, ty),
598+
instantiating_crate,
599+
)
600+
}
578601
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
579602
}
580603
}
@@ -626,6 +649,7 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
626649
// AsyncDropGlueCtorShim always use the Rust calling convention and thus follow the
627650
// target's default symbol decoration scheme.
628651
ExportedSymbol::AsyncDropGlueCtorShim(..) => None,
652+
ExportedSymbol::AsyncDropGlue(..) => None,
629653
// NoDefId always follow the target's default symbol decoration scheme.
630654
ExportedSymbol::NoDefId(..) => None,
631655
// 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
@@ -583,6 +583,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
583583
| ty::InstanceKind::FnPtrAddrShim(..)
584584
| ty::InstanceKind::ThreadLocalShim(..)
585585
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
586+
| ty::InstanceKind::AsyncDropGlue(..)
587+
| ty::InstanceKind::FutureDropPollShim(..)
586588
| ty::InstanceKind::Item(_) => {
587589
// We need MIR for this fn
588590
let Some((body, instance)) = M::find_mir_or_eval_fn(

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ language_item_table! {
182182
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
183183
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
184184
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
185+
FutureDropPoll, sym::future_drop_poll, future_drop_poll_fn, Target::Fn, GenericRequirement::Exact(1);
185186

186187
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
187188
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
@@ -267,6 +267,12 @@ pub struct CoroutineInfo<'tcx> {
267267
/// Coroutine drop glue. This field is populated after the state transform pass.
268268
pub coroutine_drop: Option<Body<'tcx>>,
269269

270+
/// Coroutine async drop glue.
271+
pub coroutine_drop_async: Option<Body<'tcx>>,
272+
273+
/// When coroutine has sync drop, this is async proxy calling `coroutine_drop` sync impl.
274+
pub coroutine_drop_proxy_async: Option<Body<'tcx>>,
275+
270276
/// The body of the coroutine, modified to take its upvars by move rather than by ref.
271277
///
272278
/// This is used by coroutine-closures, which must return a different flavor of coroutine
@@ -279,7 +285,7 @@ pub struct CoroutineInfo<'tcx> {
279285
/// using `run_passes`.
280286
pub by_move_body: Option<Body<'tcx>>,
281287

282-
/// The layout of a coroutine. This field is populated after the state transform pass.
288+
/// The layout of a coroutine. Produced by the state transformation.
283289
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,
284290

285291
/// If this is a coroutine then record the type of source expression that caused this coroutine
@@ -300,6 +306,8 @@ impl<'tcx> CoroutineInfo<'tcx> {
300306
resume_ty: Some(resume_ty),
301307
by_move_body: None,
302308
coroutine_drop: None,
309+
coroutine_drop_async: None,
310+
coroutine_drop_proxy_async: None,
303311
coroutine_layout: None,
304312
}
305313
}
@@ -669,6 +677,26 @@ impl<'tcx> Body<'tcx> {
669677
self.coroutine.as_ref()?.by_move_body.as_ref()
670678
}
671679

680+
#[inline]
681+
pub fn coroutine_drop_async(&self) -> Option<&Body<'tcx>> {
682+
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop_async.as_ref())
683+
}
684+
685+
#[inline]
686+
pub fn coroutine_requires_async_drop(&self) -> bool {
687+
self.coroutine_drop_async().is_some()
688+
}
689+
690+
#[inline]
691+
pub fn future_drop_poll(&self) -> Option<&Body<'tcx>> {
692+
self.coroutine.as_ref().and_then(|coroutine| {
693+
coroutine
694+
.coroutine_drop_async
695+
.as_ref()
696+
.or(coroutine.coroutine_drop_proxy_async.as_ref())
697+
})
698+
}
699+
672700
#[inline]
673701
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
674702
self.coroutine.as_ref().map(|coroutine| coroutine.coroutine_kind)

compiler/rustc_middle/src/mir/mono.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ impl<'tcx> MonoItem<'tcx> {
7171
// statements, plus one for the terminator.
7272
InstanceKind::Item(..)
7373
| InstanceKind::DropGlue(..)
74+
| InstanceKind::FutureDropPollShim(..)
75+
| InstanceKind::AsyncDropGlue(..)
7476
| InstanceKind::AsyncDropGlueCtorShim(..) => {
7577
let mir = tcx.instance_mir(instance.def);
7678
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
@@ -420,6 +422,8 @@ impl<'tcx> CodegenUnit<'tcx> {
420422
| InstanceKind::CloneShim(..)
421423
| InstanceKind::ThreadLocalShim(..)
422424
| InstanceKind::FnPtrAddrShim(..)
425+
| InstanceKind::AsyncDropGlue(..)
426+
| InstanceKind::FutureDropPollShim(..)
423427
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
424428
}
425429
}

compiler/rustc_middle/src/mir/pretty.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,20 @@ fn dump_path<'tcx>(
188188
}));
189189
s
190190
}
191-
ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) => {
192-
// Unfortunately, pretty-printed typed are not very filename-friendly.
193-
// We dome some filtering.
191+
ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
192+
let mut s = ".".to_owned();
193+
s.extend(ty.to_string().chars().filter_map(|c| match c {
194+
' ' => None,
195+
':' | '<' | '>' => Some('_'),
196+
c => Some(c),
197+
}));
198+
s
199+
}
200+
ty::InstanceKind::AsyncDropGlue(_, ty) => {
201+
let ty::Coroutine(_, args) = ty.kind() else {
202+
bug!();
203+
};
204+
let ty = args.first().unwrap().expect_ty();
194205
let mut s = ".".to_owned();
195206
s.extend(ty.to_string().chars().filter_map(|c| match c {
196207
' ' => None,
@@ -199,6 +210,21 @@ fn dump_path<'tcx>(
199210
}));
200211
s
201212
}
213+
ty::InstanceKind::FutureDropPollShim(_, proxy_cor, impl_cor) => {
214+
let mut s = ".".to_owned();
215+
s.extend(proxy_cor.to_string().chars().filter_map(|c| match c {
216+
' ' => None,
217+
':' | '<' | '>' => Some('_'),
218+
c => Some(c),
219+
}));
220+
s.push_str(".");
221+
s.extend(impl_cor.to_string().chars().filter_map(|c| match c {
222+
' ' => None,
223+
':' | '<' | '>' => Some('_'),
224+
c => Some(c),
225+
}));
226+
s
227+
}
202228
_ => String::new(),
203229
};
204230

compiler/rustc_middle/src/mir/visit.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,21 @@ macro_rules! make_mir_visitor {
350350
receiver_by_ref: _,
351351
} |
352352
ty::InstanceKind::CoroutineKindShim { coroutine_def_id: _def_id } |
353-
ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None) |
354353
ty::InstanceKind::DropGlue(_def_id, None) => {}
355354

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

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,10 @@ impl<'tcx> TyCtxt<'tcx> {
15641564
self.coroutine_kind(def_id).is_some()
15651565
}
15661566

1567+
pub fn is_templated_coroutine(self, def_id: DefId) -> bool {
1568+
Some(def_id) == self.lang_items().async_drop_in_place_poll_fn()
1569+
}
1570+
15671571
/// Returns the movability of the coroutine of `def_id`, or panics
15681572
/// if given a `def_id` that is not a coroutine.
15691573
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {

0 commit comments

Comments
 (0)