Skip to content

Commit b667246

Browse files
committed
Prereq4 for async drop - needs_async_drop query fixes and some cleanup from previous async drop glue implementation
1 parent 7a0329a commit b667246

File tree

25 files changed

+102
-1312
lines changed

25 files changed

+102
-1312
lines changed

Diff for: 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() {

Diff for: 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);

Diff for: 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);

Diff for: compiler/rustc_hir_typeck/src/callee.rs

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

Diff for: 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

Diff for: compiler/rustc_middle/src/ty/context.rs

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

603603
bidirectional_lang_item_map! {
604604
// tidy-alphabetical-start
605-
AsyncDestruct,
606605
AsyncFn,
607606
AsyncFnKindHelper,
608607
AsyncFnKindUpvars,

Diff for: 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
@@ -1469,125 +1464,6 @@ impl<'tcx> Ty<'tcx> {
14691464
}
14701465
}
14711466

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

0 commit comments

Comments
 (0)