Skip to content

Commit 48af6f8

Browse files
committed
Start implementing needs_async_drop and related
1 parent 7689408 commit 48af6f8

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
@@ -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

+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<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
@@ -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

+9
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,15 @@ pub struct Destructor {
11621162
pub constness: hir::Constness,
11631163
}
11641164

1165+
// FIXME: consider combining this definition with regular `Destructor`
1166+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
1167+
pub struct AsyncDestructor {
1168+
/// The `DefId` of the async destructor future constructor
1169+
pub ctor: DefId,
1170+
/// The `DefId` of the async destructor future type
1171+
pub future: DefId,
1172+
}
1173+
11651174
#[derive(Clone, Copy, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
11661175
pub struct VariantFlags(u8);
11671176
bitflags::bitflags! {

compiler/rustc_middle/src/ty/sty.rs

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

2165-
let surface_drop = self.surface_async_dropper_ty(tcx, param_env).unwrap();
2165+
let surface_drop = self.surface_async_dropper_ty(tcx).unwrap();
21662166

21672167
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
21682168
.instantiate(tcx, &[surface_drop.into()])
@@ -2212,7 +2212,7 @@ impl<'tcx> Ty<'tcx> {
22122212
})
22132213
.unwrap();
22142214

2215-
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx, param_env) {
2215+
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx) {
22162216
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)
22172217
.instantiate(tcx, &[dropper_ty.into(), variants_dtor.into()])
22182218
} else {
@@ -2223,21 +2223,13 @@ impl<'tcx> Ty<'tcx> {
22232223
.instantiate(tcx, &[dtor.into()])
22242224
}
22252225

2226-
fn surface_async_dropper_ty(
2227-
self,
2228-
tcx: TyCtxt<'tcx>,
2229-
param_env: ParamEnv<'tcx>,
2230-
) -> Option<Ty<'tcx>> {
2231-
if self.has_surface_async_drop(tcx, param_env) {
2232-
Some(LangItem::SurfaceAsyncDropInPlace)
2233-
} else if self.has_surface_drop(tcx, param_env) {
2234-
Some(LangItem::AsyncDropSurfaceDropInPlace)
2235-
} else {
2236-
None
2237-
}
2238-
.map(|dropper| {
2239-
Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()])
2240-
})
2226+
fn surface_async_dropper_ty(self, tcx: TyCtxt<'tcx>) -> Option<Ty<'tcx>> {
2227+
let adt_def = self.ty_adt_def()?;
2228+
let dropper = adt_def
2229+
.async_destructor(tcx)
2230+
.map(|_| LangItem::SurfaceAsyncDropInPlace)
2231+
.or_else(|| adt_def.destructor(tcx).map(|_| LangItem::AsyncDropSurfaceDropInPlace))?;
2232+
Some(Ty::async_destructor_combinator(tcx, dropper).instantiate(tcx, &[self.into()]))
22412233
}
22422234

22432235
fn async_destructor_combinator(

0 commit comments

Comments
 (0)