Skip to content

Commit 2d5cad6

Browse files
committed
Prereq4 for async drop - needs_async_drop query fixes and some cleanup from previous async drop glue implementation
1 parent d8c1a84 commit 2d5cad6

File tree

29 files changed

+102
-1762
lines changed

29 files changed

+102
-1762
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
417417
Err(instance) => Some(instance),
418418
}
419419
}
420-
InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) => {
420+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
421+
// it is `func returning noop future`
422+
InstanceKind::DropGlue(_, None) => {
421423
// empty drop glue - a nop.
422424
let dest = target.expect("Non terminating drop_in_place_real???");
423425
let ret_block = fx.get_block(dest);
@@ -675,9 +677,8 @@ pub(crate) fn codegen_drop<'tcx>(
675677
let ty = drop_place.layout().ty;
676678
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
677679

678-
if let ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) =
679-
drop_instance.def
680-
{
680+
// AsyncDropGlueCtorShim can't be here
681+
if let ty::InstanceKind::DropGlue(_, None) = drop_instance.def {
681682
// we don't actually need to drop anything
682683
} else {
683684
match ty.kind() {

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
852852

853853
let def = instance.map(|i| i.def);
854854

855-
if let Some(
856-
ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None),
857-
) = def
858-
{
855+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
856+
// it is `func returning noop future`
857+
if let Some(ty::InstanceKind::DropGlue(_, None)) = def {
859858
// Empty drop glue; a no-op.
860859
let target = target.unwrap();
861860
return helper.funclet_br(self, bx, target, mergeable_succ);

compiler/rustc_hir/src/lang_items.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,9 @@ language_item_table! {
179179

180180
Drop, sym::drop, drop_trait, Target::Trait, GenericRequirement::None;
181181
Destruct, sym::destruct, destruct_trait, Target::Trait, GenericRequirement::None;
182-
183-
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::Exact(0);
184-
AsyncDestruct, sym::async_destruct, async_destruct_trait, Target::Trait, GenericRequirement::Exact(0);
182+
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
185183
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
186184
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
187-
SurfaceAsyncDropInPlace, sym::surface_async_drop_in_place, surface_async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
188-
AsyncDropSurfaceDropInPlace, sym::async_drop_surface_drop_in_place, async_drop_surface_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
189-
AsyncDropSlice, sym::async_drop_slice, async_drop_slice_fn, Target::Fn, GenericRequirement::Exact(1);
190-
AsyncDropChain, sym::async_drop_chain, async_drop_chain_fn, Target::Fn, GenericRequirement::Exact(2);
191-
AsyncDropNoop, sym::async_drop_noop, async_drop_noop_fn, Target::Fn, GenericRequirement::Exact(0);
192-
AsyncDropDeferredDropInPlace, sym::async_drop_deferred_drop_in_place, async_drop_deferred_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
193-
AsyncDropFuse, sym::async_drop_fuse, async_drop_fuse_fn, Target::Fn, GenericRequirement::Exact(1);
194-
AsyncDropDefer, sym::async_drop_defer, async_drop_defer_fn, Target::Fn, GenericRequirement::Exact(1);
195-
AsyncDropEither, sym::async_drop_either, async_drop_either_fn, Target::Fn, GenericRequirement::Exact(3);
196185

197186
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
198187
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
@@ -324,7 +313,6 @@ language_item_table! {
324313

325314
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
326315
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
327-
FallbackSurfaceDrop, sym::fallback_surface_drop, fallback_surface_drop_fn, Target::Fn, GenericRequirement::None;
328316
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
329317

330318
Start, sym::start, start_fn, Target::Fn, GenericRequirement::Exact(1);

compiler/rustc_hir_typeck/src/callee.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ pub(crate) fn check_legal_trait_for_method_call(
3636
receiver: Option<Span>,
3737
expr_span: Span,
3838
trait_id: DefId,
39-
body_id: DefId,
39+
_body_id: DefId,
4040
) -> Result<(), ErrorGuaranteed> {
41-
if tcx.is_lang_item(trait_id, LangItem::Drop)
42-
&& tcx.lang_items().fallback_surface_drop_fn() != Some(body_id)
43-
{
41+
if tcx.is_lang_item(trait_id, LangItem::Drop) {
4442
let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) {
4543
errors::ExplicitDestructorCallSugg::Snippet {
4644
lo: expr_span.shrink_to_lo(),

compiler/rustc_middle/src/query/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,10 @@ rustc_queries! {
13801380
query is_unpin_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
13811381
desc { "computing whether `{}` is `Unpin`", env.value }
13821382
}
1383+
/// Query backing `Ty::is_async_drop`.
1384+
query is_async_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
1385+
desc { "computing whether `{}` is `AsyncDrop`", env.value }
1386+
}
13831387
/// Query backing `Ty::needs_drop`.
13841388
query needs_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
13851389
desc { "computing whether `{}` needs drop", env.value }
@@ -1412,6 +1416,14 @@ rustc_queries! {
14121416
cache_on_disk_if { true }
14131417
}
14141418

1419+
/// A list of types where the ADT requires async drop if and only if any of
1420+
/// those types require async drop. If the ADT is known to always need async drop
1421+
/// then `Err(AlwaysRequiresDrop)` is returned.
1422+
query adt_async_drop_tys(def_id: DefId) -> Result<&'tcx ty::List<Ty<'tcx>>, AlwaysRequiresDrop> {
1423+
desc { |tcx| "computing when `{}` needs async drop", tcx.def_path_str(def_id) }
1424+
cache_on_disk_if { true }
1425+
}
1426+
14151427
/// A list of types where the ADT requires drop if and only if any of those types
14161428
/// has significant drop. A type marked with the attribute `rustc_insignificant_dtor`
14171429
/// is considered to not be significant. A drop is significant if it is implemented

compiler/rustc_middle/src/ty/context.rs

-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ macro_rules! bidirectional_lang_item_map {
600600

601601
bidirectional_lang_item_map! {
602602
// tidy-alphabetical-start
603-
AsyncDestruct,
604603
AsyncFn,
605604
AsyncFnKindHelper,
606605
AsyncFnKindUpvars,

compiler/rustc_middle/src/ty/sty.rs

+1-125
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use std::assert_matches::debug_assert_matches;
66
use std::borrow::Cow;
7-
use std::iter;
87
use std::ops::{ControlFlow, Range};
98

109
use hir::def::{CtorKind, DefKind};
@@ -21,7 +20,7 @@ use rustc_target::spec::abi;
2120
use rustc_type_ir::visit::TypeVisitableExt;
2221
use rustc_type_ir::TyKind::*;
2322
use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, DynKind};
24-
use ty::util::{AsyncDropGlueMorphology, IntTypeExt};
23+
use ty::util::IntTypeExt;
2524

2625
use super::GenericParamDefKind;
2726
use crate::infer::canonical::Canonical;
@@ -962,10 +961,6 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
962961
fn discriminant_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
963962
self.discriminant_ty(interner)
964963
}
965-
966-
fn async_destructor_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
967-
self.async_destructor_ty(interner)
968-
}
969964
}
970965

971966
/// Type utilities
@@ -1477,125 +1472,6 @@ impl<'tcx> Ty<'tcx> {
14771472
}
14781473
}
14791474

1480-
/// Returns the type of the async destructor of this type.
1481-
pub fn async_destructor_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1482-
match self.async_drop_glue_morphology(tcx) {
1483-
AsyncDropGlueMorphology::Noop => {
1484-
return Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop)
1485-
.instantiate_identity();
1486-
}
1487-
AsyncDropGlueMorphology::DeferredDropInPlace => {
1488-
let drop_in_place =
1489-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropDeferredDropInPlace)
1490-
.instantiate(tcx, &[self.into()]);
1491-
return Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1492-
.instantiate(tcx, &[drop_in_place.into()]);
1493-
}
1494-
AsyncDropGlueMorphology::Custom => (),
1495-
}
1496-
1497-
match *self.kind() {
1498-
ty::Param(_) | ty::Alias(..) | ty::Infer(ty::TyVar(_)) => {
1499-
let assoc_items = tcx
1500-
.associated_item_def_ids(tcx.require_lang_item(LangItem::AsyncDestruct, None));
1501-
Ty::new_projection(tcx, assoc_items[0], [self])
1502-
}
1503-
1504-
ty::Array(elem_ty, _) | ty::Slice(elem_ty) => {
1505-
let dtor = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropSlice)
1506-
.instantiate(tcx, &[elem_ty.into()]);
1507-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1508-
.instantiate(tcx, &[dtor.into()])
1509-
}
1510-
1511-
ty::Adt(adt_def, args) if adt_def.is_enum() || adt_def.is_struct() => self
1512-
.adt_async_destructor_ty(
1513-
tcx,
1514-
adt_def.variants().iter().map(|v| v.fields.iter().map(|f| f.ty(tcx, args))),
1515-
),
1516-
ty::Tuple(tys) => self.adt_async_destructor_ty(tcx, iter::once(tys)),
1517-
ty::Closure(_, args) => {
1518-
self.adt_async_destructor_ty(tcx, iter::once(args.as_closure().upvar_tys()))
1519-
}
1520-
ty::CoroutineClosure(_, args) => self
1521-
.adt_async_destructor_ty(tcx, iter::once(args.as_coroutine_closure().upvar_tys())),
1522-
1523-
ty::Adt(adt_def, _) => {
1524-
assert!(adt_def.is_union());
1525-
1526-
let surface_drop = self.surface_async_dropper_ty(tcx).unwrap();
1527-
1528-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1529-
.instantiate(tcx, &[surface_drop.into()])
1530-
}
1531-
1532-
ty::Bound(..)
1533-
| ty::Foreign(_)
1534-
| ty::Placeholder(_)
1535-
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1536-
bug!("`async_destructor_ty` applied to unexpected type: {self:?}")
1537-
}
1538-
1539-
_ => bug!("`async_destructor_ty` is not yet implemented for type: {self:?}"),
1540-
}
1541-
}
1542-
1543-
fn adt_async_destructor_ty<I>(self, tcx: TyCtxt<'tcx>, variants: I) -> Ty<'tcx>
1544-
where
1545-
I: Iterator + ExactSizeIterator,
1546-
I::Item: IntoIterator<Item = Ty<'tcx>>,
1547-
{
1548-
debug_assert_eq!(self.async_drop_glue_morphology(tcx), AsyncDropGlueMorphology::Custom);
1549-
1550-
let defer = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropDefer);
1551-
let chain = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain);
1552-
1553-
let noop =
1554-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop).instantiate_identity();
1555-
let either = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropEither);
1556-
1557-
let variants_dtor = variants
1558-
.into_iter()
1559-
.map(|variant| {
1560-
variant
1561-
.into_iter()
1562-
.map(|ty| defer.instantiate(tcx, &[ty.into()]))
1563-
.reduce(|acc, next| chain.instantiate(tcx, &[acc.into(), next.into()]))
1564-
.unwrap_or(noop)
1565-
})
1566-
.reduce(|other, matched| {
1567-
either.instantiate(tcx, &[other.into(), matched.into(), self.into()])
1568-
})
1569-
.unwrap();
1570-
1571-
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx) {
1572-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)
1573-
.instantiate(tcx, &[dropper_ty.into(), variants_dtor.into()])
1574-
} else {
1575-
variants_dtor
1576-
};
1577-
1578-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1579-
.instantiate(tcx, &[dtor.into()])
1580-
}
1581-
1582-
fn surface_async_dropper_ty(self, tcx: TyCtxt<'tcx>) -> Option<Ty<'tcx>> {
1583-
let adt_def = self.ty_adt_def()?;
1584-
let dropper = adt_def
1585-
.async_destructor(tcx)
1586-
.map(|_| LangItem::SurfaceAsyncDropInPlace)
1587-
.or_else(|| adt_def.destructor(tcx).map(|_| LangItem::AsyncDropSurfaceDropInPlace))?;
1588-
Some(Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()]))
1589-
}
1590-
1591-
fn async_destructor_combinator(
1592-
tcx: TyCtxt<'tcx>,
1593-
lang_item: LangItem,
1594-
) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
1595-
tcx.fn_sig(tcx.require_lang_item(lang_item, None))
1596-
.map_bound(|fn_sig| fn_sig.output().no_bound_vars().unwrap())
1597-
}
1598-
15991475
/// Returns the type of metadata for (potentially fat) pointers to this type,
16001476
/// or the struct tail if the metadata type cannot be determined.
16011477
pub fn ptr_metadata_ty_or_tail(

0 commit comments

Comments
 (0)