]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
1 | // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | use check::regionck::{self, Rcx}; | |
12 | ||
54a0048b | 13 | use hir::def_id::DefId; |
e9174d1e | 14 | use middle::free_region::FreeRegionMap; |
54a0048b | 15 | use rustc::infer; |
85aaf69f | 16 | use middle::region; |
54a0048b SL |
17 | use rustc::ty::subst::{self, Subst}; |
18 | use rustc::ty::{self, Ty, TyCtxt}; | |
19 | use rustc::traits::{self, ProjectionMode}; | |
c1a9b12d | 20 | use util::nodemap::FnvHashSet; |
c34b1796 AL |
21 | |
22 | use syntax::ast; | |
23 | use syntax::codemap::{self, Span}; | |
24 | ||
25 | /// check_drop_impl confirms that the Drop implementation identfied by | |
26 | /// `drop_impl_did` is not any more specialized than the type it is | |
27 | /// attached to (Issue #8142). | |
28 | /// | |
29 | /// This means: | |
30 | /// | |
31 | /// 1. The self type must be nominal (this is already checked during | |
32 | /// coherence), | |
33 | /// | |
34 | /// 2. The generic region/type parameters of the impl's self-type must | |
35 | /// all be parameters of the Drop impl itself (i.e. no | |
36 | /// specialization like `impl Drop for Foo<i32>`), and, | |
37 | /// | |
38 | /// 3. Any bounds on the generic parameters must be reflected in the | |
39 | /// struct/enum definition for the nominal type itself (i.e. | |
40 | /// cannot do `struct S<T>; impl<T:Clone> Drop for S<T> { ... }`). | |
41 | /// | |
54a0048b | 42 | pub fn check_drop_impl(tcx: &TyCtxt, drop_impl_did: DefId) -> Result<(), ()> { |
c34b1796 | 43 | let ty::TypeScheme { generics: ref dtor_generics, |
c1a9b12d SL |
44 | ty: dtor_self_type } = tcx.lookup_item_type(drop_impl_did); |
45 | let dtor_predicates = tcx.lookup_predicates(drop_impl_did); | |
c34b1796 | 46 | match dtor_self_type.sty { |
e9174d1e SL |
47 | ty::TyEnum(adt_def, self_to_impl_substs) | |
48 | ty::TyStruct(adt_def, self_to_impl_substs) => { | |
54a0048b SL |
49 | ensure_drop_params_and_item_params_correspond(tcx, |
50 | drop_impl_did, | |
51 | dtor_generics, | |
52 | &dtor_self_type, | |
53 | adt_def.did)?; | |
c34b1796 AL |
54 | |
55 | ensure_drop_predicates_are_implied_by_item_defn(tcx, | |
56 | drop_impl_did, | |
57 | &dtor_predicates, | |
e9174d1e | 58 | adt_def.did, |
c34b1796 AL |
59 | self_to_impl_substs) |
60 | } | |
61 | _ => { | |
62 | // Destructors only work on nominal types. This was | |
63 | // already checked by coherence, so we can panic here. | |
64 | let span = tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP); | |
54a0048b SL |
65 | span_bug!(span, |
66 | "should have been rejected by coherence check: {}", | |
67 | dtor_self_type); | |
c34b1796 AL |
68 | } |
69 | } | |
70 | } | |
71 | ||
72 | fn ensure_drop_params_and_item_params_correspond<'tcx>( | |
54a0048b | 73 | tcx: &TyCtxt<'tcx>, |
e9174d1e | 74 | drop_impl_did: DefId, |
c34b1796 AL |
75 | drop_impl_generics: &ty::Generics<'tcx>, |
76 | drop_impl_ty: &ty::Ty<'tcx>, | |
e9174d1e | 77 | self_type_did: DefId) -> Result<(), ()> |
c34b1796 | 78 | { |
b039eaaf SL |
79 | let drop_impl_node_id = tcx.map.as_local_node_id(drop_impl_did).unwrap(); |
80 | let self_type_node_id = tcx.map.as_local_node_id(self_type_did).unwrap(); | |
e9174d1e SL |
81 | |
82 | // check that the impl type can be made to match the trait type. | |
83 | ||
b039eaaf | 84 | let impl_param_env = ty::ParameterEnvironment::for_item(tcx, self_type_node_id); |
54a0048b SL |
85 | let infcx = infer::new_infer_ctxt(tcx, |
86 | &tcx.tables, | |
87 | Some(impl_param_env), | |
88 | ProjectionMode::AnyFinal); | |
7453a54e | 89 | let mut fulfillment_cx = traits::FulfillmentContext::new(); |
e9174d1e SL |
90 | |
91 | let named_type = tcx.lookup_item_type(self_type_did).ty; | |
92 | let named_type = named_type.subst(tcx, &infcx.parameter_environment.free_substs); | |
93 | ||
94 | let drop_impl_span = tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP); | |
95 | let fresh_impl_substs = | |
96 | infcx.fresh_substs_for_generics(drop_impl_span, drop_impl_generics); | |
97 | let fresh_impl_self_ty = drop_impl_ty.subst(tcx, &fresh_impl_substs); | |
c34b1796 | 98 | |
e9174d1e SL |
99 | if let Err(_) = infer::mk_eqty(&infcx, true, infer::TypeOrigin::Misc(drop_impl_span), |
100 | named_type, fresh_impl_self_ty) { | |
b039eaaf | 101 | let item_span = tcx.map.span(self_type_node_id); |
9cc50fc6 SL |
102 | struct_span_err!(tcx.sess, drop_impl_span, E0366, |
103 | "Implementations of Drop cannot be specialized") | |
104 | .span_note(item_span, | |
105 | "Use same sequence of generic type and region \ | |
106 | parameters that is on the struct/enum definition") | |
107 | .emit(); | |
c34b1796 | 108 | return Err(()); |
e9174d1e SL |
109 | } |
110 | ||
7453a54e | 111 | if let Err(ref errors) = fulfillment_cx.select_all_or_error(&infcx) { |
e9174d1e SL |
112 | // this could be reached when we get lazy normalization |
113 | traits::report_fulfillment_errors(&infcx, errors); | |
114 | return Err(()); | |
115 | } | |
116 | ||
117 | let free_regions = FreeRegionMap::new(); | |
b039eaaf | 118 | infcx.resolve_regions_and_report_errors(&free_regions, drop_impl_node_id); |
e9174d1e | 119 | Ok(()) |
c34b1796 AL |
120 | } |
121 | ||
122 | /// Confirms that every predicate imposed by dtor_predicates is | |
123 | /// implied by assuming the predicates attached to self_type_did. | |
124 | fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( | |
54a0048b | 125 | tcx: &TyCtxt<'tcx>, |
e9174d1e | 126 | drop_impl_did: DefId, |
c34b1796 | 127 | dtor_predicates: &ty::GenericPredicates<'tcx>, |
e9174d1e | 128 | self_type_did: DefId, |
c34b1796 AL |
129 | self_to_impl_substs: &subst::Substs<'tcx>) -> Result<(), ()> { |
130 | ||
131 | // Here is an example, analogous to that from | |
132 | // `compare_impl_method`. | |
133 | // | |
134 | // Consider a struct type: | |
135 | // | |
136 | // struct Type<'c, 'b:'c, 'a> { | |
137 | // x: &'a Contents // (contents are irrelevant; | |
138 | // y: &'c Cell<&'b Contents>, // only the bounds matter for our purposes.) | |
139 | // } | |
140 | // | |
141 | // and a Drop impl: | |
142 | // | |
143 | // impl<'z, 'y:'z, 'x:'y> Drop for P<'z, 'y, 'x> { | |
144 | // fn drop(&mut self) { self.y.set(self.x); } // (only legal if 'x: 'y) | |
145 | // } | |
146 | // | |
147 | // We start out with self_to_impl_substs, that maps the generic | |
148 | // parameters of Type to that of the Drop impl. | |
149 | // | |
150 | // self_to_impl_substs = {'c => 'z, 'b => 'y, 'a => 'x} | |
151 | // | |
152 | // Applying this to the predicates (i.e. assumptions) provided by the item | |
153 | // definition yields the instantiated assumptions: | |
154 | // | |
155 | // ['y : 'z] | |
156 | // | |
157 | // We then check all of the predicates of the Drop impl: | |
158 | // | |
159 | // ['y:'z, 'x:'y] | |
160 | // | |
161 | // and ensure each is in the list of instantiated | |
162 | // assumptions. Here, `'y:'z` is present, but `'x:'y` is | |
163 | // absent. So we report an error that the Drop impl injected a | |
164 | // predicate that is not present on the struct definition. | |
165 | ||
b039eaaf | 166 | let self_type_node_id = tcx.map.as_local_node_id(self_type_did).unwrap(); |
c34b1796 AL |
167 | |
168 | let drop_impl_span = tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP); | |
169 | ||
170 | // We can assume the predicates attached to struct/enum definition | |
171 | // hold. | |
c1a9b12d | 172 | let generic_assumptions = tcx.lookup_predicates(self_type_did); |
c34b1796 AL |
173 | |
174 | let assumptions_in_impl_context = generic_assumptions.instantiate(tcx, &self_to_impl_substs); | |
175 | assert!(assumptions_in_impl_context.predicates.is_empty_in(subst::SelfSpace)); | |
176 | assert!(assumptions_in_impl_context.predicates.is_empty_in(subst::FnSpace)); | |
177 | let assumptions_in_impl_context = | |
178 | assumptions_in_impl_context.predicates.get_slice(subst::TypeSpace); | |
179 | ||
180 | // An earlier version of this code attempted to do this checking | |
181 | // via the traits::fulfill machinery. However, it ran into trouble | |
182 | // since the fulfill machinery merely turns outlives-predicates | |
183 | // 'a:'b and T:'b into region inference constraints. It is simpler | |
184 | // just to look for all the predicates directly. | |
185 | ||
186 | assert!(dtor_predicates.predicates.is_empty_in(subst::SelfSpace)); | |
187 | assert!(dtor_predicates.predicates.is_empty_in(subst::FnSpace)); | |
188 | let predicates = dtor_predicates.predicates.get_slice(subst::TypeSpace); | |
189 | for predicate in predicates { | |
190 | // (We do not need to worry about deep analysis of type | |
191 | // expressions etc because the Drop impls are already forced | |
b039eaaf | 192 | // to take on a structure that is roughly an alpha-renaming of |
c34b1796 AL |
193 | // the generic parameters of the item definition.) |
194 | ||
195 | // This path now just checks *all* predicates via the direct | |
196 | // lookup, rather than using fulfill machinery. | |
197 | // | |
198 | // However, it may be more efficient in the future to batch | |
199 | // the analysis together via the fulfill , rather than the | |
200 | // repeated `contains` calls. | |
201 | ||
202 | if !assumptions_in_impl_context.contains(&predicate) { | |
b039eaaf | 203 | let item_span = tcx.map.span(self_type_node_id); |
9cc50fc6 SL |
204 | struct_span_err!(tcx.sess, drop_impl_span, E0367, |
205 | "The requirement `{}` is added only by the Drop impl.", predicate) | |
206 | .span_note(item_span, | |
207 | "The same requirement must be part of \ | |
208 | the struct/enum definition") | |
209 | .emit(); | |
c34b1796 AL |
210 | } |
211 | } | |
85aaf69f | 212 | |
c34b1796 AL |
213 | if tcx.sess.has_errors() { |
214 | return Err(()); | |
215 | } | |
216 | Ok(()) | |
217 | } | |
85aaf69f | 218 | |
c34b1796 AL |
219 | /// check_safety_of_destructor_if_necessary confirms that the type |
220 | /// expression `typ` conforms to the "Drop Check Rule" from the Sound | |
221 | /// Generic Drop (RFC 769). | |
222 | /// | |
223 | /// ---- | |
224 | /// | |
b039eaaf | 225 | /// The simplified (*) Drop Check Rule is the following: |
c34b1796 AL |
226 | /// |
227 | /// Let `v` be some value (either temporary or named) and 'a be some | |
228 | /// lifetime (scope). If the type of `v` owns data of type `D`, where | |
229 | /// | |
b039eaaf SL |
230 | /// * (1.) `D` has a lifetime- or type-parametric Drop implementation, |
231 | /// (where that `Drop` implementation does not opt-out of | |
232 | /// this check via the `unsafe_destructor_blind_to_params` | |
233 | /// attribute), and | |
234 | /// * (2.) the structure of `D` can reach a reference of type `&'a _`, | |
c34b1796 AL |
235 | /// |
236 | /// then 'a must strictly outlive the scope of v. | |
237 | /// | |
238 | /// ---- | |
239 | /// | |
240 | /// This function is meant to by applied to the type for every | |
241 | /// expression in the program. | |
b039eaaf SL |
242 | /// |
243 | /// ---- | |
244 | /// | |
245 | /// (*) The qualifier "simplified" is attached to the above | |
246 | /// definition of the Drop Check Rule, because it is a simplification | |
247 | /// of the original Drop Check rule, which attempted to prove that | |
248 | /// some `Drop` implementations could not possibly access data even if | |
249 | /// it was technically reachable, due to parametricity. | |
250 | /// | |
251 | /// However, (1.) parametricity on its own turned out to be a | |
252 | /// necessary but insufficient condition, and (2.) future changes to | |
253 | /// the language are expected to make it impossible to ensure that a | |
254 | /// `Drop` implementation is actually parametric with respect to any | |
255 | /// particular type parameter. (In particular, impl specialization is | |
256 | /// expected to break the needed parametricity property beyond | |
257 | /// repair.) | |
258 | /// | |
259 | /// Therefore we have scaled back Drop-Check to a more conservative | |
260 | /// rule that does not attempt to deduce whether a `Drop` | |
261 | /// implementation could not possible access data of a given lifetime; | |
262 | /// instead Drop-Check now simply assumes that if a destructor has | |
263 | /// access (direct or indirect) to a lifetime parameter, then that | |
264 | /// lifetime must be forced to outlive that destructor's dynamic | |
265 | /// extent. We then provide the `unsafe_destructor_blind_to_params` | |
266 | /// attribute as a way for destructor implementations to opt-out of | |
267 | /// this conservative assumption (and thus assume the obligation of | |
268 | /// ensuring that they do not access data nor invoke methods of | |
269 | /// values that have been previously dropped). | |
270 | /// | |
85aaf69f | 271 | pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>, |
e9174d1e SL |
272 | typ: ty::Ty<'tcx>, |
273 | span: Span, | |
274 | scope: region::CodeExtent) { | |
62682a34 SL |
275 | debug!("check_safety_of_destructor_if_necessary typ: {:?} scope: {:?}", |
276 | typ, scope); | |
85aaf69f | 277 | |
c1a9b12d | 278 | let parent_scope = rcx.tcx().region_maps.opt_encl_scope(scope).unwrap_or_else(|| { |
54a0048b | 279 | span_bug!(span, "no enclosing scope found for scope: {:?}", scope) |
c1a9b12d | 280 | }); |
85aaf69f | 281 | |
c34b1796 | 282 | let result = iterate_over_potentially_unsafe_regions_in_type( |
c1a9b12d SL |
283 | &mut DropckContext { |
284 | rcx: rcx, | |
285 | span: span, | |
286 | parent_scope: parent_scope, | |
287 | breadcrumbs: FnvHashSet() | |
288 | }, | |
c34b1796 | 289 | TypeContext::Root, |
85aaf69f | 290 | typ, |
85aaf69f | 291 | 0); |
c34b1796 AL |
292 | match result { |
293 | Ok(()) => {} | |
294 | Err(Error::Overflow(ref ctxt, ref detected_on_typ)) => { | |
295 | let tcx = rcx.tcx(); | |
9cc50fc6 SL |
296 | let mut err = struct_span_err!(tcx.sess, span, E0320, |
297 | "overflow while adding drop-check rules for {}", typ); | |
c34b1796 AL |
298 | match *ctxt { |
299 | TypeContext::Root => { | |
300 | // no need for an additional note if the overflow | |
301 | // was somehow on the root. | |
302 | } | |
54a0048b | 303 | TypeContext::ADT { def_id, variant, field } => { |
e9174d1e SL |
304 | let adt = tcx.lookup_adt_def(def_id); |
305 | let variant_name = match adt.adt_kind() { | |
306 | ty::AdtKind::Enum => format!("enum {} variant {}", | |
307 | tcx.item_path_str(def_id), | |
308 | variant), | |
309 | ty::AdtKind::Struct => format!("struct {}", | |
310 | tcx.item_path_str(def_id)) | |
311 | }; | |
c34b1796 | 312 | span_note!( |
9cc50fc6 | 313 | &mut err, |
c34b1796 | 314 | span, |
e9174d1e SL |
315 | "overflowed on {} field {} type: {}", |
316 | variant_name, | |
54a0048b | 317 | field, |
62682a34 | 318 | detected_on_typ); |
c34b1796 AL |
319 | } |
320 | } | |
9cc50fc6 | 321 | err.emit(); |
c34b1796 AL |
322 | } |
323 | } | |
324 | } | |
325 | ||
326 | enum Error<'tcx> { | |
327 | Overflow(TypeContext, ty::Ty<'tcx>), | |
85aaf69f SL |
328 | } |
329 | ||
c1a9b12d | 330 | #[derive(Copy, Clone)] |
c34b1796 AL |
331 | enum TypeContext { |
332 | Root, | |
e9174d1e SL |
333 | ADT { |
334 | def_id: DefId, | |
c34b1796 | 335 | variant: ast::Name, |
c34b1796 AL |
336 | field: ast::Name, |
337 | } | |
338 | } | |
339 | ||
c1a9b12d SL |
340 | struct DropckContext<'a, 'b: 'a, 'tcx: 'b> { |
341 | rcx: &'a mut Rcx<'b, 'tcx>, | |
342 | /// types that have already been traversed | |
343 | breadcrumbs: FnvHashSet<Ty<'tcx>>, | |
344 | /// span for error reporting | |
85aaf69f | 345 | span: Span, |
c1a9b12d SL |
346 | /// the scope reachable dtorck types must outlive |
347 | parent_scope: region::CodeExtent | |
348 | } | |
349 | ||
350 | // `context` is used for reporting overflow errors | |
351 | fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>( | |
352 | cx: &mut DropckContext<'a, 'b, 'tcx>, | |
353 | context: TypeContext, | |
354 | ty: Ty<'tcx>, | |
355 | depth: usize) -> Result<(), Error<'tcx>> | |
85aaf69f | 356 | { |
c1a9b12d | 357 | let tcx = cx.rcx.tcx(); |
c34b1796 AL |
358 | // Issue #22443: Watch out for overflow. While we are careful to |
359 | // handle regular types properly, non-regular ones cause problems. | |
c1a9b12d SL |
360 | let recursion_limit = tcx.sess.recursion_limit.get(); |
361 | if depth / 4 >= recursion_limit { | |
362 | // This can get into rather deep recursion, especially in the | |
363 | // presence of things like Vec<T> -> Unique<T> -> PhantomData<T> -> T. | |
364 | // use a higher recursion limit to avoid errors. | |
365 | return Err(Error::Overflow(context, ty)) | |
c34b1796 AL |
366 | } |
367 | ||
9cc50fc6 SL |
368 | // canoncialize the regions in `ty` before inserting - infinitely many |
369 | // region variables can refer to the same region. | |
370 | let ty = cx.rcx.infcx().resolve_type_and_region_vars_if_possible(&ty); | |
371 | ||
c1a9b12d SL |
372 | if !cx.breadcrumbs.insert(ty) { |
373 | debug!("iterate_over_potentially_unsafe_regions_in_type \ | |
374 | {}ty: {} scope: {:?} - cached", | |
375 | (0..depth).map(|_| ' ').collect::<String>(), | |
376 | ty, cx.parent_scope); | |
377 | return Ok(()); // we already visited this type | |
378 | } | |
379 | debug!("iterate_over_potentially_unsafe_regions_in_type \ | |
380 | {}ty: {} scope: {:?}", | |
381 | (0..depth).map(|_| ' ').collect::<String>(), | |
382 | ty, cx.parent_scope); | |
383 | ||
384 | // If `typ` has a destructor, then we must ensure that all | |
385 | // borrowed data reachable via `typ` must outlive the parent | |
386 | // of `scope`. This is handled below. | |
387 | // | |
b039eaaf SL |
388 | // However, there is an important special case: for any Drop |
389 | // impl that is tagged as "blind" to their parameters, | |
390 | // we assume that data borrowed via such type parameters | |
391 | // remains unreachable via that Drop impl. | |
392 | // | |
393 | // For example, consider: | |
394 | // | |
395 | // ```rust | |
396 | // #[unsafe_destructor_blind_to_params] | |
397 | // impl<T> Drop for Vec<T> { ... } | |
398 | // ``` | |
c1a9b12d | 399 | // |
c1a9b12d SL |
400 | // which does have to be able to drop instances of `T`, but |
401 | // otherwise cannot read data from `T`. | |
402 | // | |
403 | // Of course, for the type expression passed in for any such | |
404 | // unbounded type parameter `T`, we must resume the recursive | |
405 | // analysis on `T` (since it would be ignored by | |
406 | // type_must_outlive). | |
e9174d1e | 407 | if has_dtor_of_interest(tcx, ty) { |
c1a9b12d SL |
408 | debug!("iterate_over_potentially_unsafe_regions_in_type \ |
409 | {}ty: {} - is a dtorck type!", | |
410 | (0..depth).map(|_| ' ').collect::<String>(), | |
411 | ty); | |
85aaf69f | 412 | |
c1a9b12d SL |
413 | regionck::type_must_outlive(cx.rcx, |
414 | infer::SubregionOrigin::SafeDestructor(cx.span), | |
415 | ty, | |
416 | ty::ReScope(cx.parent_scope)); | |
c34b1796 | 417 | |
c1a9b12d SL |
418 | return Ok(()); |
419 | } | |
c34b1796 | 420 | |
c1a9b12d SL |
421 | debug!("iterate_over_potentially_unsafe_regions_in_type \ |
422 | {}ty: {} scope: {:?} - checking interior", | |
423 | (0..depth).map(|_| ' ').collect::<String>(), | |
424 | ty, cx.parent_scope); | |
425 | ||
426 | // We still need to ensure all referenced data is safe. | |
427 | match ty.sty { | |
428 | ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | | |
429 | ty::TyFloat(_) | ty::TyStr => { | |
430 | // primitive - definitely safe | |
431 | Ok(()) | |
432 | } | |
85aaf69f | 433 | |
c1a9b12d SL |
434 | ty::TyBox(ity) | ty::TyArray(ity, _) | ty::TySlice(ity) => { |
435 | // single-element containers, behave like their element | |
436 | iterate_over_potentially_unsafe_regions_in_type( | |
437 | cx, context, ity, depth+1) | |
438 | } | |
85aaf69f | 439 | |
e9174d1e | 440 | ty::TyStruct(def, substs) if def.is_phantom_data() => { |
c1a9b12d SL |
441 | // PhantomData<T> - behaves identically to T |
442 | let ity = *substs.types.get(subst::TypeSpace, 0); | |
443 | iterate_over_potentially_unsafe_regions_in_type( | |
444 | cx, context, ity, depth+1) | |
445 | } | |
85aaf69f | 446 | |
e9174d1e SL |
447 | ty::TyStruct(def, substs) | ty::TyEnum(def, substs) => { |
448 | let did = def.did; | |
449 | for variant in &def.variants { | |
54a0048b | 450 | for field in variant.fields.iter() { |
e9174d1e | 451 | let fty = field.ty(tcx, substs); |
c1a9b12d SL |
452 | let fty = cx.rcx.fcx.resolve_type_vars_if_possible( |
453 | cx.rcx.fcx.normalize_associated_types_in(cx.span, &fty)); | |
54a0048b | 454 | iterate_over_potentially_unsafe_regions_in_type( |
c1a9b12d | 455 | cx, |
e9174d1e | 456 | TypeContext::ADT { |
c1a9b12d | 457 | def_id: did, |
e9174d1e SL |
458 | field: field.name, |
459 | variant: variant.name, | |
c1a9b12d SL |
460 | }, |
461 | fty, | |
54a0048b | 462 | depth+1)? |
85aaf69f | 463 | } |
c1a9b12d SL |
464 | } |
465 | Ok(()) | |
466 | } | |
85aaf69f | 467 | |
c1a9b12d SL |
468 | ty::TyTuple(ref tys) | |
469 | ty::TyClosure(_, box ty::ClosureSubsts { upvar_tys: ref tys, .. }) => { | |
470 | for ty in tys { | |
54a0048b | 471 | iterate_over_potentially_unsafe_regions_in_type(cx, context, ty, depth+1)? |
c1a9b12d SL |
472 | } |
473 | Ok(()) | |
85aaf69f | 474 | } |
c34b1796 | 475 | |
c1a9b12d SL |
476 | ty::TyRawPtr(..) | ty::TyRef(..) | ty::TyParam(..) => { |
477 | // these always come with a witness of liveness (references | |
478 | // explicitly, pointers implicitly, parameters by the | |
479 | // caller). | |
480 | Ok(()) | |
481 | } | |
bd371182 | 482 | |
54a0048b | 483 | ty::TyFnDef(..) | ty::TyFnPtr(_) => { |
c1a9b12d SL |
484 | // FIXME(#26656): this type is always destruction-safe, but |
485 | // it implicitly witnesses Self: Fn, which can be false. | |
486 | Ok(()) | |
487 | } | |
bd371182 | 488 | |
c1a9b12d SL |
489 | ty::TyInfer(..) | ty::TyError => { |
490 | tcx.sess.delay_span_bug(cx.span, "unresolved type in regionck"); | |
491 | Ok(()) | |
492 | } | |
bd371182 | 493 | |
c1a9b12d | 494 | // these are always dtorck |
54a0048b | 495 | ty::TyTrait(..) | ty::TyProjection(_) => bug!(), |
c1a9b12d | 496 | } |
bd371182 AL |
497 | } |
498 | ||
54a0048b | 499 | fn has_dtor_of_interest<'tcx>(tcx: &TyCtxt<'tcx>, |
e9174d1e | 500 | ty: ty::Ty<'tcx>) -> bool { |
c1a9b12d | 501 | match ty.sty { |
e9174d1e SL |
502 | ty::TyEnum(def, _) | ty::TyStruct(def, _) => { |
503 | def.is_dtorck(tcx) | |
bd371182 | 504 | } |
c1a9b12d SL |
505 | ty::TyTrait(..) | ty::TyProjection(..) => { |
506 | debug!("ty: {:?} isn't known, and therefore is a dropck type", ty); | |
507 | true | |
508 | }, | |
509 | _ => false | |
bd371182 | 510 | } |
bd371182 | 511 | } |