Skip to content

Commit b1f3a6e

Browse files
committed
Prereq4 for async drop - needs_async_drop query fixes and some cleanup from previous async drop glue implementation
1 parent 900a391 commit b1f3a6e

File tree

32 files changed

+151
-1777
lines changed

32 files changed

+151
-1777
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
441441
Err(instance) => Some(instance),
442442
}
443443
}
444-
InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) => {
444+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
445+
// it is `func returning noop future`
446+
InstanceKind::DropGlue(_, None) => {
445447
// empty drop glue - a nop.
446448
let dest = target.expect("Non terminating drop_in_place_real???");
447449
let ret_block = fx.get_block(dest);
@@ -707,9 +709,8 @@ pub(crate) fn codegen_drop<'tcx>(
707709
let ty = drop_place.layout().ty;
708710
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty);
709711

710-
if let ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) =
711-
drop_instance.def
712-
{
712+
// AsyncDropGlueCtorShim can't be here
713+
if let ty::InstanceKind::DropGlue(_, None) = drop_instance.def {
713714
// we don't actually need to drop anything
714715
} else {
715716
match ty.kind() {

compiler/rustc_codegen_ssa/src/mir/block.rs

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

927927
let def = instance.map(|i| i.def);
928928

929-
if let Some(
930-
ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None),
931-
) = def
932-
{
929+
// We don't need AsyncDropGlueCtorShim here because it is not `noop func`,
930+
// it is `func returning noop future`
931+
if let Some(ty::InstanceKind::DropGlue(_, None)) = def {
933932
// Empty drop glue; a no-op.
934933
let target = target.unwrap();
935934
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
@@ -188,20 +188,9 @@ language_item_table! {
188188

189189
Drop, sym::drop, drop_trait, Target::Trait, GenericRequirement::None;
190190
Destruct, sym::destruct, destruct_trait, Target::Trait, GenericRequirement::None;
191-
192-
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::Exact(0);
193-
AsyncDestruct, sym::async_destruct, async_destruct_trait, Target::Trait, GenericRequirement::Exact(0);
191+
AsyncDrop, sym::async_drop, async_drop_trait, Target::Trait, GenericRequirement::None;
194192
AsyncDropInPlace, sym::async_drop_in_place, async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
195193
AsyncDropInPlacePoll, sym::async_drop_in_place_poll, async_drop_in_place_poll_fn, Target::Closure, GenericRequirement::Exact(1);
196-
SurfaceAsyncDropInPlace, sym::surface_async_drop_in_place, surface_async_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
197-
AsyncDropSurfaceDropInPlace, sym::async_drop_surface_drop_in_place, async_drop_surface_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
198-
AsyncDropSlice, sym::async_drop_slice, async_drop_slice_fn, Target::Fn, GenericRequirement::Exact(1);
199-
AsyncDropChain, sym::async_drop_chain, async_drop_chain_fn, Target::Fn, GenericRequirement::Exact(2);
200-
AsyncDropNoop, sym::async_drop_noop, async_drop_noop_fn, Target::Fn, GenericRequirement::Exact(0);
201-
AsyncDropDeferredDropInPlace, sym::async_drop_deferred_drop_in_place, async_drop_deferred_drop_in_place_fn, Target::Fn, GenericRequirement::Exact(1);
202-
AsyncDropFuse, sym::async_drop_fuse, async_drop_fuse_fn, Target::Fn, GenericRequirement::Exact(1);
203-
AsyncDropDefer, sym::async_drop_defer, async_drop_defer_fn, Target::Fn, GenericRequirement::Exact(1);
204-
AsyncDropEither, sym::async_drop_either, async_drop_either_fn, Target::Fn, GenericRequirement::Exact(3);
205194

206195
CoerceUnsized, sym::coerce_unsized, coerce_unsized_trait, Target::Trait, GenericRequirement::Minimum(1);
207196
DispatchFromDyn, sym::dispatch_from_dyn, dispatch_from_dyn_trait, Target::Trait, GenericRequirement::Minimum(1);
@@ -335,7 +324,6 @@ language_item_table! {
335324

336325
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
337326
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
338-
FallbackSurfaceDrop, sym::fallback_surface_drop, fallback_surface_drop_fn, Target::Fn, GenericRequirement::None;
339327
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
340328

341329
/// For all binary crates without `#![no_main]`, Rust will generate a "main" function.

compiler/rustc_hir_typeck/src/callee.rs

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

compiler/rustc_middle/src/query/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,10 @@ rustc_queries! {
15751575
query is_unpin_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool {
15761576
desc { "computing whether `{}` is `Unpin`", env.value }
15771577
}
1578+
/// Query backing `Ty::is_async_drop`.
1579+
query is_async_drop_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool {
1580+
desc { "computing whether `{}` is `AsyncDrop`", env.value }
1581+
}
15781582
/// Query backing `Ty::needs_drop`.
15791583
query needs_drop_raw(env: ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) -> bool {
15801584
desc { "computing whether `{}` needs drop", env.value }
@@ -1607,6 +1611,14 @@ rustc_queries! {
16071611
cache_on_disk_if { true }
16081612
}
16091613

1614+
/// A list of types where the ADT requires async drop if and only if any of
1615+
/// those types require async drop. If the ADT is known to always need async drop
1616+
/// then `Err(AlwaysRequiresDrop)` is returned.
1617+
query adt_async_drop_tys(def_id: DefId) -> Result<&'tcx ty::List<Ty<'tcx>>, AlwaysRequiresDrop> {
1618+
desc { |tcx| "computing when `{}` needs async drop", tcx.def_path_str(def_id) }
1619+
cache_on_disk_if { true }
1620+
}
1621+
16101622
/// A list of types where the ADT requires drop if and only if any of those types
16111623
/// has significant drop. A type marked with the attribute `rustc_insignificant_dtor`
16121624
/// 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
@@ -694,7 +694,6 @@ macro_rules! bidirectional_lang_item_map {
694694

695695
bidirectional_lang_item_map! {
696696
// tidy-alphabetical-start
697-
AsyncDestruct,
698697
AsyncFn,
699698
AsyncFnKindHelper,
700699
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};
@@ -18,7 +17,7 @@ use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1817
use rustc_type_ir::TyKind::*;
1918
use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, DynKind, TypeVisitableExt, elaborate};
2019
use tracing::instrument;
21-
use ty::util::{AsyncDropGlueMorphology, IntTypeExt};
20+
use ty::util::IntTypeExt;
2221

2322
use super::GenericParamDefKind;
2423
use crate::infer::canonical::Canonical;
@@ -1045,10 +1044,6 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
10451044
self.discriminant_ty(interner)
10461045
}
10471046

1048-
fn async_destructor_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
1049-
self.async_destructor_ty(interner)
1050-
}
1051-
10521047
fn has_unsafe_fields(self) -> bool {
10531048
Ty::has_unsafe_fields(self)
10541049
}
@@ -1559,125 +1554,6 @@ impl<'tcx> Ty<'tcx> {
15591554
}
15601555
}
15611556

1562-
/// Returns the type of the async destructor of this type.
1563-
pub fn async_destructor_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1564-
match self.async_drop_glue_morphology(tcx) {
1565-
AsyncDropGlueMorphology::Noop => {
1566-
return Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop)
1567-
.instantiate_identity();
1568-
}
1569-
AsyncDropGlueMorphology::DeferredDropInPlace => {
1570-
let drop_in_place =
1571-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropDeferredDropInPlace)
1572-
.instantiate(tcx, &[self.into()]);
1573-
return Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1574-
.instantiate(tcx, &[drop_in_place.into()]);
1575-
}
1576-
AsyncDropGlueMorphology::Custom => (),
1577-
}
1578-
1579-
match *self.kind() {
1580-
ty::Param(_) | ty::Alias(..) | ty::Infer(ty::TyVar(_)) => {
1581-
let assoc_items = tcx
1582-
.associated_item_def_ids(tcx.require_lang_item(LangItem::AsyncDestruct, None));
1583-
Ty::new_projection(tcx, assoc_items[0], [self])
1584-
}
1585-
1586-
ty::Array(elem_ty, _) | ty::Slice(elem_ty) => {
1587-
let dtor = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropSlice)
1588-
.instantiate(tcx, &[elem_ty.into()]);
1589-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1590-
.instantiate(tcx, &[dtor.into()])
1591-
}
1592-
1593-
ty::Adt(adt_def, args) if adt_def.is_enum() || adt_def.is_struct() => self
1594-
.adt_async_destructor_ty(
1595-
tcx,
1596-
adt_def.variants().iter().map(|v| v.fields.iter().map(|f| f.ty(tcx, args))),
1597-
),
1598-
ty::Tuple(tys) => self.adt_async_destructor_ty(tcx, iter::once(tys)),
1599-
ty::Closure(_, args) => {
1600-
self.adt_async_destructor_ty(tcx, iter::once(args.as_closure().upvar_tys()))
1601-
}
1602-
ty::CoroutineClosure(_, args) => self
1603-
.adt_async_destructor_ty(tcx, iter::once(args.as_coroutine_closure().upvar_tys())),
1604-
1605-
ty::Adt(adt_def, _) => {
1606-
assert!(adt_def.is_union());
1607-
1608-
let surface_drop = self.surface_async_dropper_ty(tcx).unwrap();
1609-
1610-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1611-
.instantiate(tcx, &[surface_drop.into()])
1612-
}
1613-
1614-
ty::Bound(..)
1615-
| ty::Foreign(_)
1616-
| ty::Placeholder(_)
1617-
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1618-
bug!("`async_destructor_ty` applied to unexpected type: {self:?}")
1619-
}
1620-
1621-
_ => bug!("`async_destructor_ty` is not yet implemented for type: {self:?}"),
1622-
}
1623-
}
1624-
1625-
fn adt_async_destructor_ty<I>(self, tcx: TyCtxt<'tcx>, variants: I) -> Ty<'tcx>
1626-
where
1627-
I: Iterator + ExactSizeIterator,
1628-
I::Item: IntoIterator<Item = Ty<'tcx>>,
1629-
{
1630-
debug_assert_eq!(self.async_drop_glue_morphology(tcx), AsyncDropGlueMorphology::Custom);
1631-
1632-
let defer = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropDefer);
1633-
let chain = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain);
1634-
1635-
let noop =
1636-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop).instantiate_identity();
1637-
let either = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropEither);
1638-
1639-
let variants_dtor = variants
1640-
.into_iter()
1641-
.map(|variant| {
1642-
variant
1643-
.into_iter()
1644-
.map(|ty| defer.instantiate(tcx, &[ty.into()]))
1645-
.reduce(|acc, next| chain.instantiate(tcx, &[acc.into(), next.into()]))
1646-
.unwrap_or(noop)
1647-
})
1648-
.reduce(|other, matched| {
1649-
either.instantiate(tcx, &[other.into(), matched.into(), self.into()])
1650-
})
1651-
.unwrap();
1652-
1653-
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx) {
1654-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)
1655-
.instantiate(tcx, &[dropper_ty.into(), variants_dtor.into()])
1656-
} else {
1657-
variants_dtor
1658-
};
1659-
1660-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
1661-
.instantiate(tcx, &[dtor.into()])
1662-
}
1663-
1664-
fn surface_async_dropper_ty(self, tcx: TyCtxt<'tcx>) -> Option<Ty<'tcx>> {
1665-
let adt_def = self.ty_adt_def()?;
1666-
let dropper = adt_def
1667-
.async_destructor(tcx)
1668-
.map(|_| LangItem::SurfaceAsyncDropInPlace)
1669-
.or_else(|| adt_def.destructor(tcx).map(|_| LangItem::AsyncDropSurfaceDropInPlace))?;
1670-
Some(Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()]))
1671-
}
1672-
1673-
fn async_destructor_combinator(
1674-
tcx: TyCtxt<'tcx>,
1675-
lang_item: LangItem,
1676-
) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
1677-
tcx.fn_sig(tcx.require_lang_item(lang_item, None))
1678-
.map_bound(|fn_sig| fn_sig.output().no_bound_vars().unwrap())
1679-
}
1680-
16811557
/// Returns the type of metadata for (potentially wide) pointers to this type,
16821558
/// or the struct tail if the metadata type cannot be determined.
16831559
pub fn ptr_metadata_ty_or_tail(

0 commit comments

Comments
 (0)