2020-01-30 14:28:16 -06:00
|
|
|
//! Check whether a type has (potentially) non-trivial drop glue.
|
|
|
|
|
|
|
|
use rustc::ty::subst::Subst;
|
|
|
|
use rustc::ty::util::{needs_drop_components, AlwaysRequiresDrop};
|
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_span::DUMMY_SP;
|
|
|
|
|
|
|
|
type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>;
|
|
|
|
|
|
|
|
fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
|
|
|
|
let adt_fields =
|
|
|
|
move |adt_def: &ty::AdtDef| tcx.adt_drop_tys(adt_def.did).map(|tys| tys.iter().copied());
|
2020-02-09 04:16:57 -06:00
|
|
|
// If we don't know a type doesn't need drop, for example if it's a type
|
|
|
|
// parameter without a `Copy` bound, then we conservatively return that it
|
|
|
|
// needs drop.
|
2020-01-30 14:28:16 -06:00
|
|
|
let res = NeedsDropTypes::new(tcx, query.param_env, query.value, adt_fields).next().is_some();
|
|
|
|
debug!("needs_drop_raw({:?}) = {:?}", query, res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NeedsDropTypes<'tcx, F> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
query_ty: Ty<'tcx>,
|
|
|
|
seen_tys: FxHashSet<Ty<'tcx>>,
|
2020-02-09 04:16:57 -06:00
|
|
|
/// A stack of types left to process, and the recursion depth when we
|
|
|
|
/// pushed that type. Each round, we pop something from the stack and check
|
|
|
|
/// if it needs drop. If the result depends on whether some other types
|
|
|
|
/// need drop we push them onto the stack.
|
2020-01-30 14:28:16 -06:00
|
|
|
unchecked_tys: Vec<(Ty<'tcx>, usize)>,
|
|
|
|
recursion_limit: usize,
|
|
|
|
adt_components: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, F> NeedsDropTypes<'tcx, F> {
|
|
|
|
fn new(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
adt_components: F,
|
|
|
|
) -> Self {
|
|
|
|
let mut seen_tys = FxHashSet::default();
|
|
|
|
seen_tys.insert(ty);
|
|
|
|
let recursion_limit = *tcx.sess.recursion_limit.get();
|
|
|
|
Self {
|
|
|
|
tcx,
|
|
|
|
param_env,
|
|
|
|
seen_tys,
|
|
|
|
query_ty: ty,
|
|
|
|
unchecked_tys: vec![(ty, 0)],
|
|
|
|
recursion_limit,
|
|
|
|
adt_components,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, F, I> Iterator for NeedsDropTypes<'tcx, F>
|
|
|
|
where
|
|
|
|
F: Fn(&ty::AdtDef) -> NeedsDropResult<I>,
|
|
|
|
I: Iterator<Item = Ty<'tcx>>,
|
|
|
|
{
|
|
|
|
type Item = NeedsDropResult<Ty<'tcx>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
|
|
|
while let Some((ty, level)) = self.unchecked_tys.pop() {
|
|
|
|
if level > self.recursion_limit {
|
|
|
|
// Not having a `Span` isn't great. But there's hopefully some other
|
|
|
|
// recursion limit error as well.
|
|
|
|
tcx.sess.span_err(
|
|
|
|
DUMMY_SP,
|
|
|
|
&format!("overflow while checking whether `{}` requires drop", self.query_ty),
|
|
|
|
);
|
|
|
|
return Some(Err(AlwaysRequiresDrop));
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:22:30 -06:00
|
|
|
let components = match needs_drop_components(ty, &tcx.data_layout) {
|
2020-01-30 14:28:16 -06:00
|
|
|
Err(e) => return Some(Err(e)),
|
|
|
|
Ok(components) => components,
|
|
|
|
};
|
|
|
|
debug!("needs_drop_components({:?}) = {:?}", ty, components);
|
|
|
|
|
2020-01-31 16:22:30 -06:00
|
|
|
let queue_type = move |this: &mut Self, component: Ty<'tcx>| {
|
|
|
|
if this.seen_tys.insert(component) {
|
|
|
|
this.unchecked_tys.push((component, level + 1));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-30 14:28:16 -06:00
|
|
|
for component in components {
|
|
|
|
match component.kind {
|
|
|
|
_ if component.is_copy_modulo_regions(tcx, self.param_env, DUMMY_SP) => (),
|
|
|
|
|
2020-03-12 20:23:38 -05:00
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
for upvar_ty in substs.as_closure().upvar_tys() {
|
2020-01-31 16:22:30 -06:00
|
|
|
queue_type(self, upvar_ty);
|
2020-01-30 14:28:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a `Drop` impl and whether this is a union or
|
|
|
|
// `ManuallyDrop`. If it's a struct or enum without a `Drop`
|
|
|
|
// impl then check whether the field types need `Drop`.
|
|
|
|
ty::Adt(adt_def, substs) => {
|
|
|
|
let tys = match (self.adt_components)(adt_def) {
|
|
|
|
Err(e) => return Some(Err(e)),
|
|
|
|
Ok(tys) => tys,
|
|
|
|
};
|
|
|
|
for required_ty in tys {
|
|
|
|
let subst_ty = tcx.normalize_erasing_regions(
|
|
|
|
self.param_env,
|
|
|
|
required_ty.subst(tcx, substs),
|
|
|
|
);
|
2020-01-31 16:22:30 -06:00
|
|
|
queue_type(self, subst_ty);
|
2020-01-30 14:28:16 -06:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 16:22:30 -06:00
|
|
|
ty::Array(..) | ty::Opaque(..) | ty::Projection(..) | ty::Param(_) => {
|
2020-01-30 14:28:16 -06:00
|
|
|
if ty == component {
|
2020-01-31 16:22:30 -06:00
|
|
|
// Return the type to the caller: they may be able
|
|
|
|
// to normalize further than we can.
|
2020-01-30 14:28:16 -06:00
|
|
|
return Some(Ok(component));
|
2020-01-31 16:22:30 -06:00
|
|
|
} else {
|
2020-01-30 14:28:16 -06:00
|
|
|
// Store the type for later. We can't return here
|
|
|
|
// because we would then lose any other components
|
|
|
|
// of the type.
|
2020-01-31 16:22:30 -06:00
|
|
|
queue_type(self, component);
|
2020-01-30 14:28:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Some(Err(AlwaysRequiresDrop)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 09:03:11 -05:00
|
|
|
None
|
2020-01-30 14:28:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn adt_drop_tys(tcx: TyCtxt<'_>, def_id: DefId) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
|
|
|
|
let adt_components = move |adt_def: &ty::AdtDef| {
|
|
|
|
if adt_def.is_manually_drop() {
|
|
|
|
debug!("adt_drop_tys: `{:?}` is manually drop", adt_def);
|
|
|
|
return Ok(Vec::new().into_iter());
|
|
|
|
} else if adt_def.destructor(tcx).is_some() {
|
|
|
|
debug!("adt_drop_tys: `{:?}` implements `Drop`", adt_def);
|
|
|
|
return Err(AlwaysRequiresDrop);
|
|
|
|
} else if adt_def.is_union() {
|
|
|
|
debug!("adt_drop_tys: `{:?}` is a union", adt_def);
|
|
|
|
return Ok(Vec::new().into_iter());
|
|
|
|
}
|
|
|
|
Ok(adt_def.all_fields().map(|field| tcx.type_of(field.did)).collect::<Vec<_>>().into_iter())
|
|
|
|
};
|
|
|
|
|
|
|
|
let adt_ty = tcx.type_of(def_id);
|
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
let res: Result<Vec<_>, _> =
|
|
|
|
NeedsDropTypes::new(tcx, param_env, adt_ty, adt_components).collect();
|
|
|
|
|
|
|
|
debug!("adt_drop_tys(`{}`) = `{:?}`", tcx.def_path_str(def_id), res);
|
|
|
|
res.map(|components| tcx.intern_type_list(&components))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn provide(providers: &mut ty::query::Providers<'_>) {
|
|
|
|
*providers = ty::query::Providers { needs_drop_raw, adt_drop_tys, ..*providers };
|
|
|
|
}
|