]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use rustc_data_structures::fx::FxHashSet; |
2 | use rustc_hir::def_id::DefId; | |
74b04a01 XL |
3 | use rustc_infer::infer::canonical::{Canonical, QueryResponse}; |
4 | use rustc_infer::infer::TyCtxtInferExt; | |
ba9703b0 XL |
5 | use rustc_infer::traits::TraitEngineExt as _; |
6 | use rustc_middle::ty::query::Providers; | |
7 | use rustc_middle::ty::subst::{InternalSubsts, Subst}; | |
04454e1e | 8 | use rustc_middle::ty::{self, EarlyBinder, ParamEnvAnd, Ty, TyCtxt}; |
dfeec247 | 9 | use rustc_span::source_map::{Span, DUMMY_SP}; |
ba9703b0 XL |
10 | use rustc_trait_selection::traits::query::dropck_outlives::trivial_dropck_outlives; |
11 | use rustc_trait_selection::traits::query::dropck_outlives::{ | |
5e7ed085 | 12 | DropckConstraint, DropckOutlivesResult, |
ba9703b0 XL |
13 | }; |
14 | use rustc_trait_selection::traits::query::normalize::AtExt; | |
15 | use rustc_trait_selection::traits::query::{CanonicalTyGoal, NoSolution}; | |
16 | use rustc_trait_selection::traits::{ | |
17 | Normalized, ObligationCause, TraitEngine, TraitEngineExt as _, | |
18 | }; | |
0531ce1d | 19 | |
923072b8 | 20 | pub(crate) fn provide(p: &mut Providers) { |
dfeec247 | 21 | *p = Providers { dropck_outlives, adt_dtorck_constraint, ..*p }; |
8faf50e0 XL |
22 | } |
23 | ||
24 | fn dropck_outlives<'tcx>( | |
dc9dc135 | 25 | tcx: TyCtxt<'tcx>, |
0bf4aa26 | 26 | canonical_goal: CanonicalTyGoal<'tcx>, |
48663c56 | 27 | ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, DropckOutlivesResult<'tcx>>>, NoSolution> { |
0bf4aa26 XL |
28 | debug!("dropck_outlives(goal={:#?})", canonical_goal); |
29 | ||
30 | tcx.infer_ctxt().enter_with_canonical( | |
31 | DUMMY_SP, | |
32 | &canonical_goal, | |
33 | |ref infcx, goal, canonical_inference_vars| { | |
34 | let tcx = infcx.tcx; | |
dfeec247 | 35 | let ParamEnvAnd { param_env, value: for_ty } = goal; |
0531ce1d | 36 | |
dfeec247 | 37 | let mut result = DropckOutlivesResult { kinds: vec![], overflows: vec![] }; |
0531ce1d | 38 | |
0bf4aa26 XL |
39 | // A stack of types left to process. Each round, we pop |
40 | // something from the stack and invoke | |
41 | // `dtorck_constraint_for_ty`. This may produce new types that | |
42 | // have to be pushed on the stack. This continues until we have explored | |
43 | // all the reachable types from the type `for_ty`. | |
44 | // | |
45 | // Example: Imagine that we have the following code: | |
46 | // | |
47 | // ```rust | |
48 | // struct A { | |
49 | // value: B, | |
50 | // children: Vec<A>, | |
51 | // } | |
52 | // | |
53 | // struct B { | |
54 | // value: u32 | |
55 | // } | |
56 | // | |
57 | // fn f() { | |
58 | // let a: A = ...; | |
59 | // .. | |
60 | // } // here, `a` is dropped | |
61 | // ``` | |
62 | // | |
63 | // at the point where `a` is dropped, we need to figure out | |
64 | // which types inside of `a` contain region data that may be | |
65 | // accessed by any destructors in `a`. We begin by pushing `A` | |
66 | // onto the stack, as that is the type of `a`. We will then | |
67 | // invoke `dtorck_constraint_for_ty` which will expand `A` | |
68 | // into the types of its fields `(B, Vec<A>)`. These will get | |
69 | // pushed onto the stack. Eventually, expanding `Vec<A>` will | |
70 | // lead to us trying to push `A` a second time -- to prevent | |
71 | // infinite recursion, we notice that `A` was already pushed | |
72 | // once and stop. | |
73 | let mut ty_stack = vec![(for_ty, 0)]; | |
74 | ||
75 | // Set used to detect infinite recursion. | |
76 | let mut ty_set = FxHashSet::default(); | |
77 | ||
6a06907d | 78 | let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx); |
0bf4aa26 XL |
79 | |
80 | let cause = ObligationCause::dummy(); | |
5e7ed085 | 81 | let mut constraints = DropckConstraint::empty(); |
0bf4aa26 | 82 | while let Some((ty, depth)) = ty_stack.pop() { |
6a06907d | 83 | debug!( |
dfeec247 XL |
84 | "{} kinds, {} overflows, {} ty_stack", |
85 | result.kinds.len(), | |
86 | result.overflows.len(), | |
87 | ty_stack.len() | |
88 | ); | |
e74abb32 | 89 | dtorck_constraint_for_ty(tcx, DUMMY_SP, for_ty, depth, ty, &mut constraints)?; |
0bf4aa26 XL |
90 | |
91 | // "outlives" represent types/regions that may be touched | |
92 | // by a destructor. | |
94222f64 XL |
93 | result.kinds.append(&mut constraints.outlives); |
94 | result.overflows.append(&mut constraints.overflows); | |
e74abb32 XL |
95 | |
96 | // If we have even one overflow, we should stop trying to evaluate further -- | |
97 | // chances are, the subsequent overflows for this evaluation won't provide useful | |
98 | // information and will just decrease the speed at which we can emit these errors | |
99 | // (since we'll be printing for just that much longer for the often enormous types | |
100 | // that result here). | |
74b04a01 | 101 | if !result.overflows.is_empty() { |
e74abb32 XL |
102 | break; |
103 | } | |
0bf4aa26 XL |
104 | |
105 | // dtorck types are "types that will get dropped but which | |
106 | // do not themselves define a destructor", more or less. We have | |
107 | // to push them onto the stack to be expanded. | |
e74abb32 | 108 | for ty in constraints.dtorck_types.drain(..) { |
fc512014 | 109 | match infcx.at(&cause, param_env).normalize(ty) { |
dfeec247 | 110 | Ok(Normalized { value: ty, obligations }) => { |
0bf4aa26 XL |
111 | fulfill_cx.register_predicate_obligations(infcx, obligations); |
112 | ||
113 | debug!("dropck_outlives: ty from dtorck_types = {:?}", ty); | |
114 | ||
1b1a35ee | 115 | match ty.kind() { |
0bf4aa26 XL |
116 | // All parameters live for the duration of the |
117 | // function. | |
118 | ty::Param(..) => {} | |
119 | ||
120 | // A projection that we couldn't resolve - it | |
121 | // might have a destructor. | |
122 | ty::Projection(..) | ty::Opaque(..) => { | |
123 | result.kinds.push(ty.into()); | |
124 | } | |
0531ce1d | 125 | |
0bf4aa26 XL |
126 | _ => { |
127 | if ty_set.insert(ty) { | |
128 | ty_stack.push((ty, depth + 1)); | |
129 | } | |
0531ce1d XL |
130 | } |
131 | } | |
132 | } | |
0531ce1d | 133 | |
0bf4aa26 XL |
134 | // We don't actually expect to fail to normalize. |
135 | // That implies a WF error somewhere else. | |
136 | Err(NoSolution) => { | |
137 | return Err(NoSolution); | |
138 | } | |
0531ce1d XL |
139 | } |
140 | } | |
141 | } | |
0531ce1d | 142 | |
0bf4aa26 | 143 | debug!("dropck_outlives: result = {:#?}", result); |
0531ce1d | 144 | |
0731742a XL |
145 | infcx.make_canonicalized_query_response( |
146 | canonical_inference_vars, | |
147 | result, | |
dfeec247 | 148 | &mut *fulfill_cx, |
0731742a | 149 | ) |
0bf4aa26 XL |
150 | }, |
151 | ) | |
0531ce1d XL |
152 | } |
153 | ||
9fa01778 | 154 | /// Returns a set of constraints that needs to be satisfied in |
0531ce1d | 155 | /// order for `ty` to be valid for destruction. |
dc9dc135 XL |
156 | fn dtorck_constraint_for_ty<'tcx>( |
157 | tcx: TyCtxt<'tcx>, | |
0531ce1d XL |
158 | span: Span, |
159 | for_ty: Ty<'tcx>, | |
160 | depth: usize, | |
161 | ty: Ty<'tcx>, | |
5e7ed085 | 162 | constraints: &mut DropckConstraint<'tcx>, |
e74abb32 | 163 | ) -> Result<(), NoSolution> { |
dfeec247 | 164 | debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})", span, for_ty, depth, ty); |
0531ce1d | 165 | |
136023e0 | 166 | if !tcx.recursion_limit().value_within_limit(depth) { |
e74abb32 XL |
167 | constraints.overflows.push(ty); |
168 | return Ok(()); | |
0531ce1d XL |
169 | } |
170 | ||
60c5eb7d | 171 | if trivial_dropck_outlives(tcx, ty) { |
e74abb32 XL |
172 | return Ok(()); |
173 | } | |
174 | ||
1b1a35ee | 175 | match ty.kind() { |
b7449926 XL |
176 | ty::Bool |
177 | | ty::Char | |
178 | | ty::Int(_) | |
179 | | ty::Uint(_) | |
180 | | ty::Float(_) | |
181 | | ty::Str | |
182 | | ty::Never | |
183 | | ty::Foreign(..) | |
184 | | ty::RawPtr(..) | |
185 | | ty::Ref(..) | |
186 | | ty::FnDef(..) | |
187 | | ty::FnPtr(_) | |
188 | | ty::GeneratorWitness(..) => { | |
0531ce1d | 189 | // these types never have a destructor |
0531ce1d XL |
190 | } |
191 | ||
b7449926 | 192 | ty::Array(ety, _) | ty::Slice(ety) => { |
0531ce1d | 193 | // single-element containers, behave like their element |
f9f354fc | 194 | rustc_data_structures::stack::ensure_sufficient_stack(|| { |
5099ac24 | 195 | dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, *ety, constraints) |
f9f354fc | 196 | })?; |
0531ce1d XL |
197 | } |
198 | ||
f9f354fc | 199 | ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| { |
dfeec247 | 200 | for ty in tys.iter() { |
5e7ed085 | 201 | dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?; |
dfeec247 | 202 | } |
f9f354fc XL |
203 | Ok::<_, NoSolution>(()) |
204 | })?, | |
0531ce1d | 205 | |
29967ef6 XL |
206 | ty::Closure(_, substs) => { |
207 | if !substs.as_closure().is_valid() { | |
208 | // By the time this code runs, all type variables ought to | |
209 | // be fully resolved. | |
210 | ||
211 | tcx.sess.delay_span_bug( | |
212 | span, | |
213 | &format!("upvar_tys for closure not found. Expected capture information for closure {}", ty,), | |
214 | ); | |
215 | return Err(NoSolution); | |
dfeec247 | 216 | } |
29967ef6 XL |
217 | |
218 | rustc_data_structures::stack::ensure_sufficient_stack(|| { | |
219 | for ty in substs.as_closure().upvar_tys() { | |
220 | dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?; | |
221 | } | |
222 | Ok::<_, NoSolution>(()) | |
223 | })? | |
224 | } | |
0531ce1d | 225 | |
ba9703b0 | 226 | ty::Generator(_, substs, _movability) => { |
83c7162d XL |
227 | // rust-lang/rust#49918: types can be constructed, stored |
228 | // in the interior, and sit idle when generator yields | |
229 | // (and is subsequently dropped). | |
230 | // | |
231 | // It would be nice to descend into interior of a | |
232 | // generator to determine what effects dropping it might | |
233 | // have (by looking at any drop effects associated with | |
234 | // its interior). | |
235 | // | |
236 | // However, the interior's representation uses things like | |
b7449926 | 237 | // GeneratorWitness that explicitly assume they are not |
83c7162d XL |
238 | // traversed in such a manner. So instead, we will |
239 | // simplify things for now by treating all generators as | |
240 | // if they were like trait objects, where its upvars must | |
241 | // all be alive for the generator's (potential) | |
242 | // destructor. | |
243 | // | |
244 | // In particular, skipping over `_interior` is safe | |
245 | // because any side-effects from dropping `_interior` can | |
246 | // only take place through references with lifetimes | |
74b04a01 XL |
247 | // derived from lifetimes attached to the upvars and resume |
248 | // argument, and we *do* incorporate those here. | |
83c7162d | 249 | |
29967ef6 XL |
250 | if !substs.as_generator().is_valid() { |
251 | // By the time this code runs, all type variables ought to | |
252 | // be fully resolved. | |
253 | tcx.sess.delay_span_bug( | |
254 | span, | |
255 | &format!("upvar_tys for generator not found. Expected capture information for generator {}", ty,), | |
256 | ); | |
257 | return Err(NoSolution); | |
258 | } | |
259 | ||
dfeec247 XL |
260 | constraints.outlives.extend( |
261 | substs | |
262 | .as_generator() | |
ba9703b0 | 263 | .upvar_tys() |
dfeec247 XL |
264 | .map(|t| -> ty::subst::GenericArg<'tcx> { t.into() }), |
265 | ); | |
ba9703b0 | 266 | constraints.outlives.push(substs.as_generator().resume_ty().into()); |
0531ce1d XL |
267 | } |
268 | ||
b7449926 | 269 | ty::Adt(def, substs) => { |
5e7ed085 FG |
270 | let DropckConstraint { dtorck_types, outlives, overflows } = |
271 | tcx.at(span).adt_dtorck_constraint(def.did())?; | |
e74abb32 XL |
272 | // FIXME: we can try to recursively `dtorck_constraint_on_ty` |
273 | // there, but that needs some way to handle cycles. | |
04454e1e FG |
274 | constraints |
275 | .dtorck_types | |
276 | .extend(dtorck_types.iter().map(|t| EarlyBinder(*t).subst(tcx, substs))); | |
277 | constraints | |
278 | .outlives | |
279 | .extend(outlives.iter().map(|t| EarlyBinder(*t).subst(tcx, substs))); | |
280 | constraints | |
281 | .overflows | |
282 | .extend(overflows.iter().map(|t| EarlyBinder(*t).subst(tcx, substs))); | |
0531ce1d XL |
283 | } |
284 | ||
285 | // Objects must be alive in order for their destructor | |
286 | // to be called. | |
e74abb32 XL |
287 | ty::Dynamic(..) => { |
288 | constraints.outlives.push(ty.into()); | |
dfeec247 | 289 | } |
0531ce1d XL |
290 | |
291 | // Types that can't be resolved. Pass them forward. | |
e74abb32 XL |
292 | ty::Projection(..) | ty::Opaque(..) | ty::Param(..) => { |
293 | constraints.dtorck_types.push(ty); | |
dfeec247 | 294 | } |
0531ce1d | 295 | |
f035d41b | 296 | ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) | ty::Error(_) => { |
0531ce1d XL |
297 | // By the time this code runs, all type variables ought to |
298 | // be fully resolved. | |
dfeec247 | 299 | return Err(NoSolution); |
0531ce1d | 300 | } |
e74abb32 | 301 | } |
0531ce1d | 302 | |
e74abb32 | 303 | Ok(()) |
0531ce1d XL |
304 | } |
305 | ||
306 | /// Calculates the dtorck constraint for a type. | |
923072b8 | 307 | pub(crate) fn adt_dtorck_constraint( |
416331ca | 308 | tcx: TyCtxt<'_>, |
0531ce1d | 309 | def_id: DefId, |
5e7ed085 | 310 | ) -> Result<&DropckConstraint<'_>, NoSolution> { |
0531ce1d XL |
311 | let def = tcx.adt_def(def_id); |
312 | let span = tcx.def_span(def_id); | |
313 | debug!("dtorck_constraint: {:?}", def); | |
314 | ||
315 | if def.is_phantom_data() { | |
94b46f34 XL |
316 | // The first generic parameter here is guaranteed to be a type because it's |
317 | // `PhantomData`. | |
532ac7d7 | 318 | let substs = InternalSubsts::identity_for_item(tcx, def_id); |
94b46f34 | 319 | assert_eq!(substs.len(), 1); |
5e7ed085 | 320 | let result = DropckConstraint { |
0531ce1d | 321 | outlives: vec![], |
94b46f34 | 322 | dtorck_types: vec![substs.type_at(0)], |
0531ce1d XL |
323 | overflows: vec![], |
324 | }; | |
325 | debug!("dtorck_constraint: {:?} => {:?}", def, result); | |
5099ac24 | 326 | return Ok(tcx.arena.alloc(result)); |
0531ce1d XL |
327 | } |
328 | ||
5e7ed085 | 329 | let mut result = DropckConstraint::empty(); |
e74abb32 XL |
330 | for field in def.all_fields() { |
331 | let fty = tcx.type_of(field.did); | |
332 | dtorck_constraint_for_ty(tcx, span, fty, 0, fty, &mut result)?; | |
333 | } | |
0531ce1d XL |
334 | result.outlives.extend(tcx.destructor_constraints(def)); |
335 | dedup_dtorck_constraint(&mut result); | |
336 | ||
337 | debug!("dtorck_constraint: {:?} => {:?}", def, result); | |
338 | ||
5099ac24 | 339 | Ok(tcx.arena.alloc(result)) |
0531ce1d XL |
340 | } |
341 | ||
5e7ed085 | 342 | fn dedup_dtorck_constraint(c: &mut DropckConstraint<'_>) { |
0bf4aa26 XL |
343 | let mut outlives = FxHashSet::default(); |
344 | let mut dtorck_types = FxHashSet::default(); | |
0531ce1d XL |
345 | |
346 | c.outlives.retain(|&val| outlives.replace(val).is_none()); | |
dfeec247 | 347 | c.dtorck_types.retain(|&val| dtorck_types.replace(val).is_none()); |
0531ce1d | 348 | } |