1 //! Check whether a type has (potentially) non-trivial drop glue.
3 use rustc::ty::subst::Subst;
4 use rustc::ty::util::{needs_drop_components, AlwaysRequiresDrop};
5 use rustc::ty::{self, Ty, TyCtxt};
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_hir::def_id::DefId;
8 use rustc_span::DUMMY_SP;
10 type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>;
12 fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
14 move |adt_def: &ty::AdtDef| tcx.adt_drop_tys(adt_def.did).map(|tys| tys.iter().copied());
15 // If we don't know a type doesn't need drop, for example if it's a type
16 // parameter without a `Copy` bound, then we conservatively return that it
18 let res = NeedsDropTypes::new(tcx, query.param_env, query.value, adt_fields).next().is_some();
19 debug!("needs_drop_raw({:?}) = {:?}", query, res);
23 struct NeedsDropTypes<'tcx, F> {
25 param_env: ty::ParamEnv<'tcx>,
27 seen_tys: FxHashSet<Ty<'tcx>>,
28 /// A stack of types left to process, and the recursion depth when we
29 /// pushed that type. Each round, we pop something from the stack and check
30 /// if it needs drop. If the result depends on whether some other types
31 /// need drop we push them onto the stack.
32 unchecked_tys: Vec<(Ty<'tcx>, usize)>,
33 recursion_limit: usize,
37 impl<'tcx, F> NeedsDropTypes<'tcx, F> {
40 param_env: ty::ParamEnv<'tcx>,
44 let mut seen_tys = FxHashSet::default();
46 let recursion_limit = *tcx.sess.recursion_limit.get();
52 unchecked_tys: vec![(ty, 0)],
59 impl<'tcx, F, I> Iterator for NeedsDropTypes<'tcx, F>
61 F: Fn(&ty::AdtDef) -> NeedsDropResult<I>,
62 I: Iterator<Item = Ty<'tcx>>,
64 type Item = NeedsDropResult<Ty<'tcx>>;
66 fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
69 while let Some((ty, level)) = self.unchecked_tys.pop() {
70 if level > self.recursion_limit {
71 // Not having a `Span` isn't great. But there's hopefully some other
72 // recursion limit error as well.
75 &format!("overflow while checking whether `{}` requires drop", self.query_ty),
77 return Some(Err(AlwaysRequiresDrop));
80 let components = match needs_drop_components(ty, &tcx.data_layout) {
81 Err(e) => return Some(Err(e)),
82 Ok(components) => components,
84 debug!("needs_drop_components({:?}) = {:?}", ty, components);
86 let queue_type = move |this: &mut Self, component: Ty<'tcx>| {
87 if this.seen_tys.insert(component) {
88 this.unchecked_tys.push((component, level + 1));
92 for component in components {
93 match component.kind {
94 _ if component.is_copy_modulo_regions(tcx, self.param_env, DUMMY_SP) => (),
96 ty::Closure(def_id, substs) => {
97 for upvar_ty in substs.as_closure().upvar_tys(def_id, tcx) {
98 queue_type(self, upvar_ty);
102 // Check for a `Drop` impl and whether this is a union or
103 // `ManuallyDrop`. If it's a struct or enum without a `Drop`
104 // impl then check whether the field types need `Drop`.
105 ty::Adt(adt_def, substs) => {
106 let tys = match (self.adt_components)(adt_def) {
107 Err(e) => return Some(Err(e)),
110 for required_ty in tys {
111 let subst_ty = tcx.normalize_erasing_regions(
113 required_ty.subst(tcx, substs),
115 queue_type(self, subst_ty);
118 ty::Array(..) | ty::Opaque(..) | ty::Projection(..) | ty::Param(_) => {
120 // Return the type to the caller: they may be able
121 // to normalize further than we can.
122 return Some(Ok(component));
124 // Store the type for later. We can't return here
125 // because we would then lose any other components
127 queue_type(self, component);
130 _ => return Some(Err(AlwaysRequiresDrop)),
139 fn adt_drop_tys(tcx: TyCtxt<'_>, def_id: DefId) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
140 let adt_components = move |adt_def: &ty::AdtDef| {
141 if adt_def.is_manually_drop() {
142 debug!("adt_drop_tys: `{:?}` is manually drop", adt_def);
143 return Ok(Vec::new().into_iter());
144 } else if adt_def.destructor(tcx).is_some() {
145 debug!("adt_drop_tys: `{:?}` implements `Drop`", adt_def);
146 return Err(AlwaysRequiresDrop);
147 } else if adt_def.is_union() {
148 debug!("adt_drop_tys: `{:?}` is a union", adt_def);
149 return Ok(Vec::new().into_iter());
151 Ok(adt_def.all_fields().map(|field| tcx.type_of(field.did)).collect::<Vec<_>>().into_iter())
154 let adt_ty = tcx.type_of(def_id);
155 let param_env = tcx.param_env(def_id);
156 let res: Result<Vec<_>, _> =
157 NeedsDropTypes::new(tcx, param_env, adt_ty, adt_components).collect();
159 debug!("adt_drop_tys(`{}`) = `{:?}`", tcx.def_path_str(def_id), res);
160 res.map(|components| tcx.intern_type_list(&components))
163 pub(crate) fn provide(providers: &mut ty::query::Providers<'_>) {
164 *providers = ty::query::Providers { needs_drop_raw, adt_drop_tys, ..*providers };