Skip to content

Commit a47173c

Browse files
committed
Start implementing needs_async_drop and related
1 parent e0904cd commit a47173c

File tree

10 files changed

+196
-110
lines changed

10 files changed

+196
-110
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub fn provide(providers: &mut Providers) {
112112
wfcheck::provide(providers);
113113
*providers = Providers {
114114
adt_destructor,
115+
adt_async_destructor,
115116
region_scope_tree,
116117
collect_return_position_impl_trait_in_trait_tys,
117118
compare_impl_const: compare_impl_item::compare_impl_const_raw,
@@ -124,6 +125,10 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Destructor>
124125
tcx.calculate_dtor(def_id.to_def_id(), dropck::check_drop_impl)
125126
}
126127

128+
fn adt_async_destructor(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::AsyncDestructor> {
129+
tcx.calculate_async_dtor(def_id.to_def_id(), dropck::check_drop_impl)
130+
}
131+
127132
/// Given a `DefId` for an opaque type in return position, find its parent item's return
128133
/// expressions.
129134
fn get_owner_return_paths(

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+4
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ provide! { tcx, def_id, other, cdata,
287287
let _ = cdata;
288288
tcx.calculate_dtor(def_id, |_,_| Ok(()))
289289
}
290+
adt_async_destructor => {
291+
let _ = cdata;
292+
tcx.calculate_async_dtor(def_id, |_,_| Ok(()))
293+
}
290294
associated_item_def_ids => {
291295
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
292296
}

compiler/rustc_middle/src/query/erase.rs

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ trivial! {
238238
Option<rustc_hir::CoroutineKind>,
239239
Option<rustc_hir::HirId>,
240240
Option<rustc_middle::middle::stability::DeprecationEntry>,
241+
Option<rustc_middle::ty::AsyncDestructor>,
241242
Option<rustc_middle::ty::Destructor>,
242243
Option<rustc_middle::ty::ImplTraitInTraitData>,
243244
Option<rustc_middle::ty::ScalarInt>,
@@ -295,6 +296,7 @@ trivial! {
295296
rustc_middle::ty::AssocItem,
296297
rustc_middle::ty::AssocItemContainer,
297298
rustc_middle::ty::Asyncness,
299+
rustc_middle::ty::AsyncDestructor,
298300
rustc_middle::ty::BoundVariableKind,
299301
rustc_middle::ty::DeducedParamAttrs,
300302
rustc_middle::ty::Destructor,

compiler/rustc_middle/src/query/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ rustc_queries! {
703703
cache_on_disk_if { key.is_local() }
704704
separate_provide_extern
705705
}
706+
query adt_async_destructor(key: DefId) -> Option<ty::AsyncDestructor> {
707+
desc { |tcx| "computing `AsyncDrop` impl for `{}`", tcx.def_path_str(key) }
708+
cache_on_disk_if { key.is_local() }
709+
separate_provide_extern
710+
}
706711

707712
query adt_sized_constraint(key: DefId) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
708713
desc { |tcx| "computing the `Sized` constraint for `{}`", tcx.def_path_str(key) }
@@ -1343,18 +1348,14 @@ rustc_queries! {
13431348
query is_unpin_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
13441349
desc { "computing whether `{}` is `Unpin`", env.value }
13451350
}
1346-
/// Query backing `Ty::has_surface_async_drop`.
1347-
query has_surface_async_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
1348-
desc { "computing whether `{}` has `AsyncDrop` implementation", env.value }
1349-
}
1350-
/// Query backing `Ty::has_surface_drop`.
1351-
query has_surface_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
1352-
desc { "computing whether `{}` has `Drop` implementation", env.value }
1353-
}
13541351
/// Query backing `Ty::needs_drop`.
13551352
query needs_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
13561353
desc { "computing whether `{}` needs drop", env.value }
13571354
}
1355+
/// Query backing `Ty::needs_async_drop`.
1356+
query needs_async_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
1357+
desc { "computing whether `{}` needs async drop", env.value }
1358+
}
13581359
/// Query backing `Ty::has_significant_drop_raw`.
13591360
query has_significant_drop_raw(env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
13601361
desc { "computing whether `{}` has a significant drop", env.value }

compiler/rustc_middle/src/ty/adt.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use std::hash::{Hash, Hasher};
2424
use std::ops::Range;
2525
use std::str;
2626

27-
use super::{Destructor, FieldDef, GenericPredicates, Ty, TyCtxt, VariantDef, VariantDiscr};
27+
use super::{
28+
AsyncDestructor, Destructor, FieldDef, GenericPredicates, Ty, TyCtxt, VariantDef, VariantDiscr,
29+
};
2830

2931
#[derive(Clone, Copy, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
3032
pub struct AdtFlags(u16);
@@ -577,6 +579,12 @@ impl<'tcx> AdtDef<'tcx> {
577579
tcx.adt_destructor(self.did())
578580
}
579581

582+
// FIXME(zetanumbers): consider supporting this method in same places where
583+
// `destructor` is referenced
584+
pub fn async_destructor(self, tcx: TyCtxt<'tcx>) -> Option<AsyncDestructor> {
585+
tcx.adt_async_destructor(self.did())
586+
}
587+
580588
/// Returns a type such that `Self: Sized` if and only if that type is `Sized`,
581589
/// or `None` if the type is always sized.
582590
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {

compiler/rustc_middle/src/ty/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,15 @@ pub struct Destructor {
11721172
pub constness: hir::Constness,
11731173
}
11741174

1175+
// FIXME: consider combining this definition with regular `Destructor`
1176+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
1177+
pub struct AsyncDestructor {
1178+
/// The `DefId` of the async destructor future constructor
1179+
pub ctor: DefId,
1180+
/// The `DefId` of the async destructor future type
1181+
pub future: DefId,
1182+
}
1183+
11751184
#[derive(Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
11761185
pub struct VariantFlags(u8);
11771186
bitflags::bitflags! {

compiler/rustc_middle/src/ty/sty.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@ impl<'tcx> Ty<'tcx> {
19911991
ty::Adt(adt_def, _) => {
19921992
assert!(adt_def.is_union());
19931993

1994-
let surface_drop = self.surface_async_dropper_ty(tcx, param_env).unwrap();
1994+
let surface_drop = self.surface_async_dropper_ty(tcx).unwrap();
19951995

19961996
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
19971997
.instantiate(tcx, &[surface_drop.into()])
@@ -2041,7 +2041,7 @@ impl<'tcx> Ty<'tcx> {
20412041
})
20422042
.unwrap();
20432043

2044-
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx, param_env) {
2044+
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx) {
20452045
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)
20462046
.instantiate(tcx, &[dropper_ty.into(), variants_dtor.into()])
20472047
} else {
@@ -2052,21 +2052,13 @@ impl<'tcx> Ty<'tcx> {
20522052
.instantiate(tcx, &[dtor.into()])
20532053
}
20542054

2055-
fn surface_async_dropper_ty(
2056-
self,
2057-
tcx: TyCtxt<'tcx>,
2058-
param_env: ParamEnv<'tcx>,
2059-
) -> Option<Ty<'tcx>> {
2060-
if self.has_surface_async_drop(tcx, param_env) {
2061-
Some(LangItem::SurfaceAsyncDropInPlace)
2062-
} else if self.has_surface_drop(tcx, param_env) {
2063-
Some(LangItem::AsyncDropSurfaceDropInPlace)
2064-
} else {
2065-
None
2066-
}
2067-
.map(|dropper| {
2068-
Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()])
2069-
})
2055+
fn surface_async_dropper_ty(self, tcx: TyCtxt<'tcx>) -> Option<Ty<'tcx>> {
2056+
let adt_def = self.ty_adt_def()?;
2057+
let dropper = adt_def
2058+
.async_destructor(tcx)
2059+
.map(|_| LangItem::SurfaceAsyncDropInPlace)
2060+
.or_else(|| adt_def.destructor(tcx).map(|_| LangItem::AsyncDropSurfaceDropInPlace))?;
2061+
Some(Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()]))
20702062
}
20712063

20722064
fn async_destructor_combinator(

0 commit comments

Comments
 (0)