Skip to content

Commit 55a2c2d

Browse files
committed
Start implementing needs_async_drop and related
1 parent f5efc3c commit 55a2c2d

File tree

10 files changed

+196
-110
lines changed

10 files changed

+196
-110
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub fn provide(providers: &mut Providers) {
111111
wfcheck::provide(providers);
112112
*providers = Providers {
113113
adt_destructor,
114+
adt_async_destructor,
114115
region_scope_tree,
115116
collect_return_position_impl_trait_in_trait_tys,
116117
compare_impl_const: compare_impl_item::compare_impl_const_raw,
@@ -123,6 +124,10 @@ fn adt_destructor(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Destructor>
123124
tcx.calculate_dtor(def_id.to_def_id(), dropck::check_drop_impl)
124125
}
125126

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ provide! { tcx, def_id, other, cdata,
286286
let _ = cdata;
287287
tcx.calculate_dtor(def_id, |_,_| Ok(()))
288288
}
289+
adt_async_destructor => {
290+
let _ = cdata;
291+
tcx.calculate_async_dtor(def_id, |_,_| Ok(()))
292+
}
289293
associated_item_def_ids => {
290294
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
291295
}

compiler/rustc_middle/src/query/erase.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ trivial! {
236236
Option<rustc_hir::CoroutineKind>,
237237
Option<rustc_hir::HirId>,
238238
Option<rustc_middle::middle::stability::DeprecationEntry>,
239+
Option<rustc_middle::ty::AsyncDestructor>,
239240
Option<rustc_middle::ty::Destructor>,
240241
Option<rustc_middle::ty::ImplTraitInTraitData>,
241242
Option<rustc_middle::ty::ScalarInt>,
@@ -293,6 +294,7 @@ trivial! {
293294
rustc_middle::ty::AssocItem,
294295
rustc_middle::ty::AssocItemContainer,
295296
rustc_middle::ty::Asyncness,
297+
rustc_middle::ty::AsyncDestructor,
296298
rustc_middle::ty::BoundVariableKind,
297299
rustc_middle::ty::DeducedParamAttrs,
298300
rustc_middle::ty::Destructor,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 9 additions & 8 deletions
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<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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use std::hash::{Hash, Hasher};
2323
use std::ops::Range;
2424
use std::str;
2525

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

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

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

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,15 @@ pub struct Destructor {
12221222
pub constness: hir::Constness,
12231223
}
12241224

1225+
// FIXME: consider combining this definition with regular `Destructor`
1226+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
1227+
pub struct AsyncDestructor {
1228+
/// The `DefId` of the async destructor future constructor
1229+
pub ctor: DefId,
1230+
/// The `DefId` of the async destructor future type
1231+
pub future: DefId,
1232+
}
1233+
12251234
#[derive(Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
12261235
pub struct VariantFlags(u8);
12271236
bitflags::bitflags! {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ impl<'tcx> Ty<'tcx> {
23582358
ty::Adt(adt_def, _) => {
23592359
assert!(adt_def.is_union());
23602360

2361-
let surface_drop = self.surface_async_dropper_ty(tcx, param_env).unwrap();
2361+
let surface_drop = self.surface_async_dropper_ty(tcx).unwrap();
23622362

23632363
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
23642364
.instantiate(tcx, &[surface_drop.into()])
@@ -2408,7 +2408,7 @@ impl<'tcx> Ty<'tcx> {
24082408
})
24092409
.unwrap();
24102410

2411-
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx, param_env) {
2411+
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx) {
24122412
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)
24132413
.instantiate(tcx, &[dropper_ty.into(), variants_dtor.into()])
24142414
} else {
@@ -2419,21 +2419,13 @@ impl<'tcx> Ty<'tcx> {
24192419
.instantiate(tcx, &[dtor.into()])
24202420
}
24212421

2422-
fn surface_async_dropper_ty(
2423-
self,
2424-
tcx: TyCtxt<'tcx>,
2425-
param_env: ParamEnv<'tcx>,
2426-
) -> Option<Ty<'tcx>> {
2427-
if self.has_surface_async_drop(tcx, param_env) {
2428-
Some(LangItem::SurfaceAsyncDropInPlace)
2429-
} else if self.has_surface_drop(tcx, param_env) {
2430-
Some(LangItem::AsyncDropSurfaceDropInPlace)
2431-
} else {
2432-
None
2433-
}
2434-
.map(|dropper| {
2435-
Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()])
2436-
})
2422+
fn surface_async_dropper_ty(self, tcx: TyCtxt<'tcx>) -> Option<Ty<'tcx>> {
2423+
let adt_def = self.ty_adt_def()?;
2424+
let dropper = adt_def
2425+
.async_destructor(tcx)
2426+
.map(|_| LangItem::SurfaceAsyncDropInPlace)
2427+
.or_else(|| adt_def.destructor(tcx).map(|_| LangItem::AsyncDropSurfaceDropInPlace))?;
2428+
Some(Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()]))
24372429
}
24382430

24392431
fn async_destructor_combinator(

0 commit comments

Comments
 (0)