]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_ty_utils/src/needs_drop.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_ty_utils / src / needs_drop.rs
1 //! Check whether a type has (potentially) non-trivial drop glue.
2
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_hir::def_id::DefId;
5 use rustc_middle::ty::subst::Subst;
6 use rustc_middle::ty::util::{needs_drop_components, AlwaysRequiresDrop};
7 use rustc_middle::ty::{self, Ty, TyCtxt};
8 use rustc_session::Limit;
9 use rustc_span::{sym, DUMMY_SP};
10
11 type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>;
12
13 fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
14 let adt_fields =
15 move |adt_def: &ty::AdtDef| tcx.adt_drop_tys(adt_def.did).map(|tys| tys.iter());
16 // If we don't know a type doesn't need drop, for example if it's a type
17 // parameter without a `Copy` bound, then we conservatively return that it
18 // needs drop.
19 let res = NeedsDropTypes::new(tcx, query.param_env, query.value, adt_fields).next().is_some();
20 debug!("needs_drop_raw({:?}) = {:?}", query, res);
21 res
22 }
23
24 fn has_significant_drop_raw<'tcx>(
25 tcx: TyCtxt<'tcx>,
26 query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
27 ) -> bool {
28 let significant_drop_fields =
29 move |adt_def: &ty::AdtDef| tcx.adt_significant_drop_tys(adt_def.did).map(|tys| tys.iter());
30 let res = NeedsDropTypes::new(tcx, query.param_env, query.value, significant_drop_fields)
31 .next()
32 .is_some();
33 debug!("has_significant_drop_raw({:?}) = {:?}", query, res);
34 res
35 }
36
37 struct NeedsDropTypes<'tcx, F> {
38 tcx: TyCtxt<'tcx>,
39 param_env: ty::ParamEnv<'tcx>,
40 query_ty: Ty<'tcx>,
41 seen_tys: FxHashSet<Ty<'tcx>>,
42 /// A stack of types left to process, and the recursion depth when we
43 /// pushed that type. Each round, we pop something from the stack and check
44 /// if it needs drop. If the result depends on whether some other types
45 /// need drop we push them onto the stack.
46 unchecked_tys: Vec<(Ty<'tcx>, usize)>,
47 recursion_limit: Limit,
48 adt_components: F,
49 }
50
51 impl<'tcx, F> NeedsDropTypes<'tcx, F> {
52 fn new(
53 tcx: TyCtxt<'tcx>,
54 param_env: ty::ParamEnv<'tcx>,
55 ty: Ty<'tcx>,
56 adt_components: F,
57 ) -> Self {
58 let mut seen_tys = FxHashSet::default();
59 seen_tys.insert(ty);
60 Self {
61 tcx,
62 param_env,
63 seen_tys,
64 query_ty: ty,
65 unchecked_tys: vec![(ty, 0)],
66 recursion_limit: tcx.recursion_limit(),
67 adt_components,
68 }
69 }
70 }
71
72 impl<'tcx, F, I> Iterator for NeedsDropTypes<'tcx, F>
73 where
74 F: Fn(&ty::AdtDef) -> NeedsDropResult<I>,
75 I: Iterator<Item = Ty<'tcx>>,
76 {
77 type Item = NeedsDropResult<Ty<'tcx>>;
78
79 fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
80 let tcx = self.tcx;
81
82 while let Some((ty, level)) = self.unchecked_tys.pop() {
83 if !self.recursion_limit.value_within_limit(level) {
84 // Not having a `Span` isn't great. But there's hopefully some other
85 // recursion limit error as well.
86 tcx.sess.span_err(
87 DUMMY_SP,
88 &format!("overflow while checking whether `{}` requires drop", self.query_ty),
89 );
90 return Some(Err(AlwaysRequiresDrop));
91 }
92
93 let components = match needs_drop_components(ty, &tcx.data_layout) {
94 Err(e) => return Some(Err(e)),
95 Ok(components) => components,
96 };
97 debug!("needs_drop_components({:?}) = {:?}", ty, components);
98
99 let queue_type = move |this: &mut Self, component: Ty<'tcx>| {
100 if this.seen_tys.insert(component) {
101 this.unchecked_tys.push((component, level + 1));
102 }
103 };
104
105 for component in components {
106 match *component.kind() {
107 _ if component.is_copy_modulo_regions(tcx.at(DUMMY_SP), self.param_env) => (),
108
109 ty::Closure(_, substs) => {
110 queue_type(self, substs.as_closure().tupled_upvars_ty());
111 }
112
113 ty::Generator(def_id, substs, _) => {
114 let substs = substs.as_generator();
115 queue_type(self, substs.tupled_upvars_ty());
116
117 let witness = substs.witness();
118 let interior_tys = match witness.kind() {
119 &ty::GeneratorWitness(tys) => tcx.erase_late_bound_regions(tys),
120 _ => {
121 tcx.sess.delay_span_bug(
122 tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP),
123 &format!("unexpected generator witness type {:?}", witness),
124 );
125 return Some(Err(AlwaysRequiresDrop));
126 }
127 };
128
129 for interior_ty in interior_tys {
130 queue_type(self, interior_ty);
131 }
132 }
133
134 // Check for a `Drop` impl and whether this is a union or
135 // `ManuallyDrop`. If it's a struct or enum without a `Drop`
136 // impl then check whether the field types need `Drop`.
137 ty::Adt(adt_def, substs) => {
138 let tys = match (self.adt_components)(adt_def) {
139 Err(e) => return Some(Err(e)),
140 Ok(tys) => tys,
141 };
142 for required_ty in tys {
143 let subst_ty = tcx.normalize_erasing_regions(
144 self.param_env,
145 required_ty.subst(tcx, substs),
146 );
147 queue_type(self, subst_ty);
148 }
149 }
150 ty::Array(..) | ty::Opaque(..) | ty::Projection(..) | ty::Param(_) => {
151 if ty == component {
152 // Return the type to the caller: they may be able
153 // to normalize further than we can.
154 return Some(Ok(component));
155 } else {
156 // Store the type for later. We can't return here
157 // because we would then lose any other components
158 // of the type.
159 queue_type(self, component);
160 }
161 }
162 _ => return Some(Err(AlwaysRequiresDrop)),
163 }
164 }
165 }
166
167 None
168 }
169 }
170
171 // This is a helper function for `adt_drop_tys` and `adt_significant_drop_tys`.
172 // Depending on the implentation of `adt_has_dtor`, it is used to check if the
173 // ADT has a destructor or if the ADT only has a significant destructor. For
174 // understanding significant destructor look at `adt_significant_drop_tys`.
175 fn adt_drop_tys_helper(
176 tcx: TyCtxt<'_>,
177 def_id: DefId,
178 adt_has_dtor: impl Fn(&ty::AdtDef) -> bool,
179 ) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
180 let adt_components = move |adt_def: &ty::AdtDef| {
181 if adt_def.is_manually_drop() {
182 debug!("adt_drop_tys: `{:?}` is manually drop", adt_def);
183 return Ok(Vec::new().into_iter());
184 } else if adt_has_dtor(adt_def) {
185 debug!("adt_drop_tys: `{:?}` implements `Drop`", adt_def);
186 return Err(AlwaysRequiresDrop);
187 } else if adt_def.is_union() {
188 debug!("adt_drop_tys: `{:?}` is a union", adt_def);
189 return Ok(Vec::new().into_iter());
190 }
191 Ok(adt_def.all_fields().map(|field| tcx.type_of(field.did)).collect::<Vec<_>>().into_iter())
192 };
193
194 let adt_ty = tcx.type_of(def_id);
195 let param_env = tcx.param_env(def_id);
196 let res: Result<Vec<_>, _> =
197 NeedsDropTypes::new(tcx, param_env, adt_ty, adt_components).collect();
198
199 debug!("adt_drop_tys(`{}`) = `{:?}`", tcx.def_path_str(def_id), res);
200 res.map(|components| tcx.intern_type_list(&components))
201 }
202
203 fn adt_drop_tys(tcx: TyCtxt<'_>, def_id: DefId) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
204 let adt_has_dtor = |adt_def: &ty::AdtDef| adt_def.destructor(tcx).is_some();
205 adt_drop_tys_helper(tcx, def_id, adt_has_dtor)
206 }
207
208 fn adt_significant_drop_tys(
209 tcx: TyCtxt<'_>,
210 def_id: DefId,
211 ) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
212 let adt_has_dtor = |adt_def: &ty::AdtDef| {
213 adt_def
214 .destructor(tcx)
215 .map(|dtor| !tcx.has_attr(dtor.did, sym::rustc_insignificant_dtor))
216 .unwrap_or(false)
217 };
218 adt_drop_tys_helper(tcx, def_id, adt_has_dtor)
219 }
220
221 pub(crate) fn provide(providers: &mut ty::query::Providers) {
222 *providers = ty::query::Providers {
223 needs_drop_raw,
224 has_significant_drop_raw,
225 adt_drop_tys,
226 adt_significant_drop_tys,
227 ..*providers
228 };
229 }