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