Skip to content

Commit 175820b

Browse files
committed
Prereq5 for async drop - AsyncDropGlue & FutureDropPoll instances preparation
1 parent 2d5cad6 commit 175820b

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
@@ -552,6 +552,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
552552
| ty::InstanceKind::FnPtrAddrShim(..)
553553
| ty::InstanceKind::ThreadLocalShim(..)
554554
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
555+
| ty::InstanceKind::AsyncDropGlue(..)
556+
| ty::InstanceKind::FutureDropPollShim(..)
555557
| ty::InstanceKind::Item(_) => {
556558
// We need MIR for this fn
557559
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
@@ -205,7 +205,13 @@ pub struct CoroutineInfo<'tcx> {
205205
/// Coroutine drop glue. This field is populated after the state transform pass.
206206
pub coroutine_drop: Option<Body<'tcx>>,
207207

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

211217
/// If this is a coroutine then record the type of source expression that caused this coroutine
@@ -225,6 +231,8 @@ impl<'tcx> CoroutineInfo<'tcx> {
225231
yield_ty: Some(yield_ty),
226232
resume_ty: Some(resume_ty),
227233
coroutine_drop: None,
234+
coroutine_drop_async: None,
235+
coroutine_drop_proxy_async: None,
228236
coroutine_layout: None,
229237
}
230238
}
@@ -590,6 +598,26 @@ impl<'tcx> Body<'tcx> {
590598
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop.as_ref())
591599
}
592600

601+
#[inline]
602+
pub fn coroutine_drop_async(&self) -> Option<&Body<'tcx>> {
603+
self.coroutine.as_ref().and_then(|coroutine| coroutine.coroutine_drop_async.as_ref())
604+
}
605+
606+
#[inline]
607+
pub fn coroutine_requires_async_drop(&self) -> bool {
608+
self.coroutine_drop_async().is_some()
609+
}
610+
611+
#[inline]
612+
pub fn future_drop_poll(&self) -> Option<&Body<'tcx>> {
613+
self.coroutine.as_ref().and_then(|coroutine| {
614+
coroutine
615+
.coroutine_drop_async
616+
.as_ref()
617+
.or(coroutine.coroutine_drop_proxy_async.as_ref())
618+
})
619+
}
620+
593621
#[inline]
594622
pub fn coroutine_kind(&self) -> Option<CoroutineKind> {
595623
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()
@@ -419,6 +421,8 @@ impl<'tcx> CodegenUnit<'tcx> {
419421
| InstanceKind::CloneShim(..)
420422
| InstanceKind::ThreadLocalShim(..)
421423
| InstanceKind::FnPtrAddrShim(..)
424+
| InstanceKind::AsyncDropGlue(..)
425+
| InstanceKind::FutureDropPollShim(..)
422426
| InstanceKind::AsyncDropGlueCtorShim(..) => None,
423427
}
424428
}

compiler/rustc_middle/src/mir/pretty.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,20 @@ fn dump_path<'tcx>(
232232
}));
233233
s
234234
}
235-
ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) => {
236-
// Unfortunately, pretty-printed typed are not very filename-friendly.
237-
// We dome some filtering.
235+
ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
236+
let mut s = ".".to_owned();
237+
s.extend(ty.to_string().chars().filter_map(|c| match c {
238+
' ' => None,
239+
':' | '<' | '>' => Some('_'),
240+
c => Some(c),
241+
}));
242+
s
243+
}
244+
ty::InstanceKind::AsyncDropGlue(_, ty) => {
245+
let ty::Coroutine(_, args) = ty.kind() else {
246+
bug!();
247+
};
248+
let ty = args.first().unwrap().expect_ty();
238249
let mut s = ".".to_owned();
239250
s.extend(ty.to_string().chars().filter_map(|c| match c {
240251
' ' => None,
@@ -243,6 +254,21 @@ fn dump_path<'tcx>(
243254
}));
244255
s
245256
}
257+
ty::InstanceKind::FutureDropPollShim(_, proxy_cor, impl_cor) => {
258+
let mut s = ".".to_owned();
259+
s.extend(proxy_cor.to_string().chars().filter_map(|c| match c {
260+
' ' => None,
261+
':' | '<' | '>' => Some('_'),
262+
c => Some(c),
263+
}));
264+
s.push_str(".");
265+
s.extend(impl_cor.to_string().chars().filter_map(|c| match c {
266+
' ' => None,
267+
':' | '<' | '>' => Some('_'),
268+
c => Some(c),
269+
}));
270+
s
271+
}
246272
_ => String::new(),
247273
};
248274

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
@@ -1562,6 +1562,10 @@ impl<'tcx> TyCtxt<'tcx> {
15621562
self.coroutine_kind(def_id).is_some()
15631563
}
15641564

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

0 commit comments

Comments
 (0)