]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | // Copyright 2012-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 | //! misc. type-system utilities too small to deserve their own file | |
12 | ||
8bb4bdeb | 13 | use hir::def_id::{DefId, LOCAL_CRATE}; |
476ff2be | 14 | use hir::map::DefPathData; |
a7813a04 | 15 | use infer::InferCtxt; |
cc61c64b | 16 | use ich::{StableHashingContext, NodeIdHashingMode}; |
5bcae85e | 17 | use traits::{self, Reveal}; |
32a655c1 | 18 | use ty::{self, Ty, TyCtxt, TypeAndMut, TypeFlags, TypeFoldable}; |
cc61c64b | 19 | use ty::ParameterEnvironment; |
5bcae85e | 20 | use ty::fold::TypeVisitor; |
54a0048b | 21 | use ty::layout::{Layout, LayoutError}; |
cc61c64b | 22 | use ty::subst::{Subst, Kind}; |
54a0048b | 23 | use ty::TypeVariants::*; |
8bb4bdeb | 24 | use util::common::ErrorReported; |
cc61c64b | 25 | use util::nodemap::{FxHashMap, FxHashSet}; |
476ff2be | 26 | use middle::lang_items; |
54a0048b SL |
27 | |
28 | use rustc_const_math::{ConstInt, ConstIsize, ConstUsize}; | |
cc61c64b XL |
29 | use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult, |
30 | HashStable}; | |
9e0c209e | 31 | use std::cell::RefCell; |
e9174d1e | 32 | use std::cmp; |
476ff2be | 33 | use std::hash::Hash; |
5bcae85e | 34 | use std::intrinsics; |
b039eaaf | 35 | use syntax::ast::{self, Name}; |
a7813a04 | 36 | use syntax::attr::{self, SignedInt, UnsignedInt}; |
8bb4bdeb | 37 | use syntax_pos::{Span, DUMMY_SP}; |
e9174d1e | 38 | |
54a0048b | 39 | use hir; |
e9174d1e | 40 | |
8bb4bdeb XL |
41 | type Disr = ConstInt; |
42 | ||
cc61c64b | 43 | pub trait IntTypeExt { |
8bb4bdeb | 44 | fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>; |
a7813a04 XL |
45 | fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>) |
46 | -> Option<Disr>; | |
54a0048b | 47 | fn assert_ty_matches(&self, val: Disr); |
a7813a04 | 48 | fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr; |
cc61c64b | 49 | } |
8bb4bdeb XL |
50 | |
51 | ||
52 | macro_rules! typed_literal { | |
53 | ($tcx:expr, $ty:expr, $lit:expr) => { | |
54 | match $ty { | |
55 | SignedInt(ast::IntTy::I8) => ConstInt::I8($lit), | |
56 | SignedInt(ast::IntTy::I16) => ConstInt::I16($lit), | |
57 | SignedInt(ast::IntTy::I32) => ConstInt::I32($lit), | |
58 | SignedInt(ast::IntTy::I64) => ConstInt::I64($lit), | |
59 | SignedInt(ast::IntTy::I128) => ConstInt::I128($lit), | |
60 | SignedInt(ast::IntTy::Is) => match $tcx.sess.target.int_type { | |
61 | ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16($lit)), | |
62 | ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32($lit)), | |
63 | ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64($lit)), | |
64 | _ => bug!(), | |
65 | }, | |
66 | UnsignedInt(ast::UintTy::U8) => ConstInt::U8($lit), | |
67 | UnsignedInt(ast::UintTy::U16) => ConstInt::U16($lit), | |
68 | UnsignedInt(ast::UintTy::U32) => ConstInt::U32($lit), | |
69 | UnsignedInt(ast::UintTy::U64) => ConstInt::U64($lit), | |
70 | UnsignedInt(ast::UintTy::U128) => ConstInt::U128($lit), | |
71 | UnsignedInt(ast::UintTy::Us) => match $tcx.sess.target.uint_type { | |
72 | ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16($lit)), | |
73 | ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32($lit)), | |
74 | ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64($lit)), | |
75 | _ => bug!(), | |
76 | }, | |
77 | } | |
78 | } | |
e9174d1e SL |
79 | } |
80 | ||
81 | impl IntTypeExt for attr::IntType { | |
8bb4bdeb | 82 | fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> { |
e9174d1e | 83 | match *self { |
a7813a04 XL |
84 | SignedInt(ast::IntTy::I8) => tcx.types.i8, |
85 | SignedInt(ast::IntTy::I16) => tcx.types.i16, | |
86 | SignedInt(ast::IntTy::I32) => tcx.types.i32, | |
87 | SignedInt(ast::IntTy::I64) => tcx.types.i64, | |
32a655c1 | 88 | SignedInt(ast::IntTy::I128) => tcx.types.i128, |
a7813a04 XL |
89 | SignedInt(ast::IntTy::Is) => tcx.types.isize, |
90 | UnsignedInt(ast::UintTy::U8) => tcx.types.u8, | |
91 | UnsignedInt(ast::UintTy::U16) => tcx.types.u16, | |
92 | UnsignedInt(ast::UintTy::U32) => tcx.types.u32, | |
93 | UnsignedInt(ast::UintTy::U64) => tcx.types.u64, | |
32a655c1 | 94 | UnsignedInt(ast::UintTy::U128) => tcx.types.u128, |
a7813a04 | 95 | UnsignedInt(ast::UintTy::Us) => tcx.types.usize, |
e9174d1e SL |
96 | } |
97 | } | |
98 | ||
a7813a04 | 99 | fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr { |
8bb4bdeb | 100 | typed_literal!(tcx, *self, 0) |
e9174d1e SL |
101 | } |
102 | ||
54a0048b SL |
103 | fn assert_ty_matches(&self, val: Disr) { |
104 | match (*self, val) { | |
105 | (SignedInt(ast::IntTy::I8), ConstInt::I8(_)) => {}, | |
106 | (SignedInt(ast::IntTy::I16), ConstInt::I16(_)) => {}, | |
107 | (SignedInt(ast::IntTy::I32), ConstInt::I32(_)) => {}, | |
108 | (SignedInt(ast::IntTy::I64), ConstInt::I64(_)) => {}, | |
32a655c1 | 109 | (SignedInt(ast::IntTy::I128), ConstInt::I128(_)) => {}, |
54a0048b SL |
110 | (SignedInt(ast::IntTy::Is), ConstInt::Isize(_)) => {}, |
111 | (UnsignedInt(ast::UintTy::U8), ConstInt::U8(_)) => {}, | |
112 | (UnsignedInt(ast::UintTy::U16), ConstInt::U16(_)) => {}, | |
113 | (UnsignedInt(ast::UintTy::U32), ConstInt::U32(_)) => {}, | |
114 | (UnsignedInt(ast::UintTy::U64), ConstInt::U64(_)) => {}, | |
32a655c1 | 115 | (UnsignedInt(ast::UintTy::U128), ConstInt::U128(_)) => {}, |
54a0048b SL |
116 | (UnsignedInt(ast::UintTy::Us), ConstInt::Usize(_)) => {}, |
117 | _ => bug!("disr type mismatch: {:?} vs {:?}", self, val), | |
e9174d1e SL |
118 | } |
119 | } | |
120 | ||
a7813a04 XL |
121 | fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>) |
122 | -> Option<Disr> { | |
123 | if let Some(val) = val { | |
124 | self.assert_ty_matches(val); | |
8bb4bdeb | 125 | (val + typed_literal!(tcx, *self, 1)).ok() |
a7813a04 XL |
126 | } else { |
127 | Some(self.initial_discriminant(tcx)) | |
128 | } | |
e9174d1e SL |
129 | } |
130 | } | |
131 | ||
132 | ||
133 | #[derive(Copy, Clone)] | |
32a655c1 SL |
134 | pub enum CopyImplementationError<'tcx> { |
135 | InfrigingField(&'tcx ty::FieldDef), | |
e9174d1e | 136 | NotAnAdt, |
cc61c64b | 137 | HasDestructor, |
e9174d1e SL |
138 | } |
139 | ||
140 | /// Describes whether a type is representable. For types that are not | |
141 | /// representable, 'SelfRecursive' and 'ContainsRecursive' are used to | |
142 | /// distinguish between types that are recursive with themselves and types that | |
143 | /// contain a different recursive type. These cases can therefore be treated | |
144 | /// differently when reporting errors. | |
145 | /// | |
146 | /// The ordering of the cases is significant. They are sorted so that cmp::max | |
147 | /// will keep the "more erroneous" of two values. | |
148 | #[derive(Copy, Clone, PartialOrd, Ord, Eq, PartialEq, Debug)] | |
149 | pub enum Representability { | |
150 | Representable, | |
151 | ContainsRecursive, | |
152 | SelfRecursive, | |
153 | } | |
154 | ||
a7813a04 XL |
155 | impl<'tcx> ParameterEnvironment<'tcx> { |
156 | pub fn can_type_implement_copy<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
157 | self_type: Ty<'tcx>, span: Span) | |
32a655c1 | 158 | -> Result<(), CopyImplementationError> { |
e9174d1e | 159 | // FIXME: (@jroesch) float this code up |
8bb4bdeb | 160 | tcx.infer_ctxt(self.clone(), Reveal::UserFacing).enter(|infcx| { |
32a655c1 SL |
161 | let (adt, substs) = match self_type.sty { |
162 | ty::TyAdt(adt, substs) => (adt, substs), | |
cc61c64b | 163 | _ => return Err(CopyImplementationError::NotAnAdt), |
a7813a04 | 164 | }; |
e9174d1e | 165 | |
32a655c1 SL |
166 | let field_implements_copy = |field: &ty::FieldDef| { |
167 | let cause = traits::ObligationCause::dummy(); | |
168 | match traits::fully_normalize(&infcx, cause, &field.ty(tcx, substs)) { | |
169 | Ok(ty) => !infcx.type_moves_by_default(ty, span), | |
cc61c64b | 170 | Err(..) => false, |
32a655c1 SL |
171 | } |
172 | }; | |
173 | ||
174 | for variant in &adt.variants { | |
175 | for field in &variant.fields { | |
176 | if !field_implements_copy(field) { | |
177 | return Err(CopyImplementationError::InfrigingField(field)); | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
8bb4bdeb | 182 | if adt.has_dtor(tcx) { |
a7813a04 XL |
183 | return Err(CopyImplementationError::HasDestructor); |
184 | } | |
e9174d1e | 185 | |
a7813a04 XL |
186 | Ok(()) |
187 | }) | |
e9174d1e SL |
188 | } |
189 | } | |
190 | ||
cc61c64b XL |
191 | impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { |
192 | /// Creates a hash of the type `Ty` which will be the same no matter what crate | |
193 | /// context it's calculated within. This is used by the `type_id` intrinsic. | |
194 | pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 { | |
195 | let mut hasher = StableHasher::new(); | |
196 | let mut hcx = StableHashingContext::new(self); | |
197 | ||
198 | hcx.while_hashing_spans(false, |hcx| { | |
199 | hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { | |
200 | ty.hash_stable(hcx, &mut hasher); | |
201 | }); | |
202 | }); | |
203 | hasher.finish() | |
204 | } | |
205 | } | |
206 | ||
a7813a04 | 207 | impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { |
5bcae85e SL |
208 | pub fn has_error_field(self, ty: Ty<'tcx>) -> bool { |
209 | match ty.sty { | |
9e0c209e | 210 | ty::TyAdt(def, substs) => { |
5bcae85e SL |
211 | for field in def.all_fields() { |
212 | let field_ty = field.ty(self, substs); | |
213 | if let TyError = field_ty.sty { | |
214 | return true; | |
215 | } | |
216 | } | |
217 | } | |
cc61c64b | 218 | _ => (), |
5bcae85e SL |
219 | } |
220 | false | |
221 | } | |
222 | ||
e9174d1e SL |
223 | /// Returns the type of element at index `i` in tuple or tuple-like type `t`. |
224 | /// For an enum `t`, `variant` is None only if `t` is a univariant enum. | |
a7813a04 | 225 | pub fn positional_element_ty(self, |
e9174d1e SL |
226 | ty: Ty<'tcx>, |
227 | i: usize, | |
228 | variant: Option<DefId>) -> Option<Ty<'tcx>> { | |
229 | match (&ty.sty, variant) { | |
9e0c209e SL |
230 | (&TyAdt(adt, substs), Some(vid)) => { |
231 | adt.variant_with_id(vid).fields.get(i).map(|f| f.ty(self, substs)) | |
e9174d1e | 232 | } |
9e0c209e SL |
233 | (&TyAdt(adt, substs), None) => { |
234 | // Don't use `struct_variant`, this may be a univariant enum. | |
235 | adt.variants[0].fields.get(i).map(|f| f.ty(self, substs)) | |
e9174d1e | 236 | } |
8bb4bdeb | 237 | (&TyTuple(ref v, _), None) => v.get(i).cloned(), |
cc61c64b | 238 | _ => None, |
e9174d1e SL |
239 | } |
240 | } | |
241 | ||
242 | /// Returns the type of element at field `n` in struct or struct-like type `t`. | |
243 | /// For an enum `t`, `variant` must be some def id. | |
a7813a04 | 244 | pub fn named_element_ty(self, |
e9174d1e SL |
245 | ty: Ty<'tcx>, |
246 | n: Name, | |
247 | variant: Option<DefId>) -> Option<Ty<'tcx>> { | |
248 | match (&ty.sty, variant) { | |
9e0c209e SL |
249 | (&TyAdt(adt, substs), Some(vid)) => { |
250 | adt.variant_with_id(vid).find_field_named(n).map(|f| f.ty(self, substs)) | |
e9174d1e | 251 | } |
9e0c209e SL |
252 | (&TyAdt(adt, substs), None) => { |
253 | adt.struct_variant().find_field_named(n).map(|f| f.ty(self, substs)) | |
e9174d1e SL |
254 | } |
255 | _ => return None | |
256 | } | |
257 | } | |
258 | ||
e9174d1e SL |
259 | /// Returns the deeply last field of nested structures, or the same type, |
260 | /// if not a structure at all. Corresponds to the only possible unsized | |
261 | /// field, and its type can be used to determine unsizing strategy. | |
a7813a04 | 262 | pub fn struct_tail(self, mut ty: Ty<'tcx>) -> Ty<'tcx> { |
9e0c209e SL |
263 | while let TyAdt(def, substs) = ty.sty { |
264 | if !def.is_struct() { | |
cc61c64b | 265 | break; |
9e0c209e | 266 | } |
e9174d1e SL |
267 | match def.struct_variant().fields.last() { |
268 | Some(f) => ty = f.ty(self, substs), | |
cc61c64b | 269 | None => break, |
e9174d1e SL |
270 | } |
271 | } | |
272 | ty | |
273 | } | |
274 | ||
275 | /// Same as applying struct_tail on `source` and `target`, but only | |
276 | /// keeps going as long as the two types are instances of the same | |
277 | /// structure definitions. | |
278 | /// For `(Foo<Foo<T>>, Foo<Trait>)`, the result will be `(Foo<T>, Trait)`, | |
279 | /// whereas struct_tail produces `T`, and `Trait`, respectively. | |
a7813a04 | 280 | pub fn struct_lockstep_tails(self, |
e9174d1e SL |
281 | source: Ty<'tcx>, |
282 | target: Ty<'tcx>) | |
283 | -> (Ty<'tcx>, Ty<'tcx>) { | |
284 | let (mut a, mut b) = (source, target); | |
9e0c209e SL |
285 | while let (&TyAdt(a_def, a_substs), &TyAdt(b_def, b_substs)) = (&a.sty, &b.sty) { |
286 | if a_def != b_def || !a_def.is_struct() { | |
cc61c64b | 287 | break; |
e9174d1e | 288 | } |
9e0c209e SL |
289 | match a_def.struct_variant().fields.last() { |
290 | Some(f) => { | |
291 | a = f.ty(self, a_substs); | |
292 | b = f.ty(self, b_substs); | |
293 | } | |
cc61c64b | 294 | _ => break, |
e9174d1e SL |
295 | } |
296 | } | |
297 | (a, b) | |
298 | } | |
299 | ||
e9174d1e SL |
300 | /// Given a set of predicates that apply to an object type, returns |
301 | /// the region bounds that the (erased) `Self` type must | |
302 | /// outlive. Precisely *because* the `Self` type is erased, the | |
303 | /// parameter `erased_self_ty` must be supplied to indicate what type | |
304 | /// has been used to represent `Self` in the predicates | |
305 | /// themselves. This should really be a unique type; `FreshTy(0)` is a | |
306 | /// popular choice. | |
307 | /// | |
308 | /// NB: in some cases, particularly around higher-ranked bounds, | |
309 | /// this function returns a kind of conservative approximation. | |
310 | /// That is, all regions returned by this function are definitely | |
311 | /// required, but there may be other region bounds that are not | |
312 | /// returned, as well as requirements like `for<'a> T: 'a`. | |
313 | /// | |
314 | /// Requires that trait definitions have been processed so that we can | |
315 | /// elaborate predicates and walk supertraits. | |
a7813a04 | 316 | pub fn required_region_bounds(self, |
e9174d1e SL |
317 | erased_self_ty: Ty<'tcx>, |
318 | predicates: Vec<ty::Predicate<'tcx>>) | |
9e0c209e | 319 | -> Vec<&'tcx ty::Region> { |
e9174d1e SL |
320 | debug!("required_region_bounds(erased_self_ty={:?}, predicates={:?})", |
321 | erased_self_ty, | |
322 | predicates); | |
323 | ||
324 | assert!(!erased_self_ty.has_escaping_regions()); | |
325 | ||
326 | traits::elaborate_predicates(self, predicates) | |
327 | .filter_map(|predicate| { | |
328 | match predicate { | |
329 | ty::Predicate::Projection(..) | | |
330 | ty::Predicate::Trait(..) | | |
331 | ty::Predicate::Equate(..) | | |
cc61c64b | 332 | ty::Predicate::Subtype(..) | |
e9174d1e SL |
333 | ty::Predicate::WellFormed(..) | |
334 | ty::Predicate::ObjectSafe(..) | | |
a7813a04 | 335 | ty::Predicate::ClosureKind(..) | |
e9174d1e SL |
336 | ty::Predicate::RegionOutlives(..) => { |
337 | None | |
338 | } | |
339 | ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(t, r))) => { | |
340 | // Search for a bound of the form `erased_self_ty | |
341 | // : 'a`, but be wary of something like `for<'a> | |
342 | // erased_self_ty : 'a` (we interpret a | |
343 | // higher-ranked bound like that as 'static, | |
344 | // though at present the code in `fulfill.rs` | |
345 | // considers such bounds to be unsatisfiable, so | |
346 | // it's kind of a moot point since you could never | |
347 | // construct such an object, but this seems | |
348 | // correct even if that code changes). | |
349 | if t == erased_self_ty && !r.has_escaping_regions() { | |
350 | Some(r) | |
351 | } else { | |
352 | None | |
353 | } | |
354 | } | |
355 | } | |
356 | }) | |
357 | .collect() | |
358 | } | |
359 | ||
8bb4bdeb XL |
360 | /// Calculate the destructor of a given type. |
361 | pub fn calculate_dtor( | |
362 | self, | |
363 | adt_did: DefId, | |
364 | validate: &mut FnMut(Self, DefId) -> Result<(), ErrorReported> | |
365 | ) -> Option<ty::Destructor> { | |
366 | let drop_trait = if let Some(def_id) = self.lang_items.drop_trait() { | |
367 | def_id | |
368 | } else { | |
369 | return None; | |
370 | }; | |
371 | ||
372 | ty::queries::coherent_trait::get(self, DUMMY_SP, (LOCAL_CRATE, drop_trait)); | |
373 | ||
374 | let mut dtor_did = None; | |
375 | let ty = self.item_type(adt_did); | |
376 | self.lookup_trait_def(drop_trait).for_each_relevant_impl(self, ty, |impl_did| { | |
377 | if let Some(item) = self.associated_items(impl_did).next() { | |
378 | if let Ok(()) = validate(self, impl_did) { | |
379 | dtor_did = Some(item.def_id); | |
380 | } | |
381 | } | |
382 | }); | |
383 | ||
384 | let dtor_did = match dtor_did { | |
e9174d1e | 385 | Some(dtor) => dtor, |
cc61c64b XL |
386 | None => return None, |
387 | }; | |
388 | ||
389 | Some(ty::Destructor { did: dtor_did }) | |
390 | } | |
391 | ||
392 | /// Return the set of types that are required to be alive in | |
393 | /// order to run the destructor of `def` (see RFCs 769 and | |
394 | /// 1238). | |
395 | /// | |
396 | /// Note that this returns only the constraints for the | |
397 | /// destructor of `def` itself. For the destructors of the | |
398 | /// contents, you need `adt_dtorck_constraint`. | |
399 | pub fn destructor_constraints(self, def: &'tcx ty::AdtDef) | |
400 | -> Vec<ty::subst::Kind<'tcx>> | |
401 | { | |
402 | let dtor = match def.destructor(self) { | |
403 | None => { | |
404 | debug!("destructor_constraints({:?}) - no dtor", def.did); | |
405 | return vec![] | |
406 | } | |
407 | Some(dtor) => dtor.did | |
e9174d1e | 408 | }; |
b039eaaf SL |
409 | |
410 | // RFC 1238: if the destructor method is tagged with the | |
411 | // attribute `unsafe_destructor_blind_to_params`, then the | |
412 | // compiler is being instructed to *assume* that the | |
413 | // destructor will not access borrowed data, | |
414 | // even if such data is otherwise reachable. | |
e9174d1e | 415 | // |
b039eaaf SL |
416 | // Such access can be in plain sight (e.g. dereferencing |
417 | // `*foo.0` of `Foo<'a>(&'a u32)`) or indirectly hidden | |
418 | // (e.g. calling `foo.0.clone()` of `Foo<T:Clone>`). | |
cc61c64b XL |
419 | if self.has_attr(dtor, "unsafe_destructor_blind_to_params") { |
420 | debug!("destructor_constraint({:?}) - blind", def.did); | |
421 | return vec![]; | |
422 | } | |
423 | ||
424 | let impl_def_id = self.associated_item(dtor).container.id(); | |
425 | let impl_generics = self.item_generics(impl_def_id); | |
426 | ||
427 | // We have a destructor - all the parameters that are not | |
428 | // pure_wrt_drop (i.e, don't have a #[may_dangle] attribute) | |
429 | // must be live. | |
430 | ||
431 | // We need to return the list of parameters from the ADTs | |
432 | // generics/substs that correspond to impure parameters on the | |
433 | // impl's generics. This is a bit ugly, but conceptually simple: | |
434 | // | |
435 | // Suppose our ADT looks like the following | |
436 | // | |
437 | // struct S<X, Y, Z>(X, Y, Z); | |
438 | // | |
439 | // and the impl is | |
440 | // | |
441 | // impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0> | |
442 | // | |
443 | // We want to return the parameters (X, Y). For that, we match | |
444 | // up the item-substs <X, Y, Z> with the substs on the impl ADT, | |
445 | // <P1, P2, P0>, and then look up which of the impl substs refer to | |
446 | // parameters marked as pure. | |
447 | ||
448 | let impl_substs = match self.item_type(impl_def_id).sty { | |
449 | ty::TyAdt(def_, substs) if def_ == def => substs, | |
450 | _ => bug!() | |
451 | }; | |
452 | ||
453 | let item_substs = match self.item_type(def.did).sty { | |
454 | ty::TyAdt(def_, substs) if def_ == def => substs, | |
455 | _ => bug!() | |
456 | }; | |
457 | ||
458 | let result = item_substs.iter().zip(impl_substs.iter()) | |
459 | .filter(|&(_, &k)| { | |
460 | if let Some(&ty::Region::ReEarlyBound(ref ebr)) = k.as_region() { | |
461 | !impl_generics.region_param(ebr).pure_wrt_drop | |
462 | } else if let Some(&ty::TyS { | |
463 | sty: ty::TypeVariants::TyParam(ref pt), .. | |
464 | }) = k.as_type() { | |
465 | !impl_generics.type_param(pt).pure_wrt_drop | |
466 | } else { | |
467 | // not a type or region param - this should be reported | |
468 | // as an error. | |
469 | false | |
470 | } | |
471 | }).map(|(&item_param, _)| item_param).collect(); | |
472 | debug!("destructor_constraint({:?}) = {:?}", def.did, result); | |
473 | result | |
b039eaaf | 474 | } |
9e0c209e | 475 | |
cc61c64b XL |
476 | /// Return a set of constraints that needs to be satisfied in |
477 | /// order for `ty` to be valid for destruction. | |
478 | pub fn dtorck_constraint_for_ty(self, | |
479 | span: Span, | |
480 | for_ty: Ty<'tcx>, | |
481 | depth: usize, | |
482 | ty: Ty<'tcx>) | |
483 | -> Result<ty::DtorckConstraint<'tcx>, ErrorReported> | |
484 | { | |
485 | debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})", | |
486 | span, for_ty, depth, ty); | |
487 | ||
488 | if depth >= self.sess.recursion_limit.get() { | |
489 | let mut err = struct_span_err!( | |
490 | self.sess, span, E0320, | |
491 | "overflow while adding drop-check rules for {}", for_ty); | |
492 | err.note(&format!("overflowed on {}", ty)); | |
493 | err.emit(); | |
494 | return Err(ErrorReported); | |
495 | } | |
496 | ||
497 | let result = match ty.sty { | |
498 | ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | | |
499 | ty::TyFloat(_) | ty::TyStr | ty::TyNever | | |
500 | ty::TyRawPtr(..) | ty::TyRef(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) => { | |
501 | // these types never have a destructor | |
502 | Ok(ty::DtorckConstraint::empty()) | |
503 | } | |
504 | ||
505 | ty::TyArray(ety, _) | ty::TySlice(ety) => { | |
506 | // single-element containers, behave like their element | |
507 | self.dtorck_constraint_for_ty(span, for_ty, depth+1, ety) | |
508 | } | |
509 | ||
510 | ty::TyTuple(tys, _) => { | |
511 | tys.iter().map(|ty| { | |
512 | self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty) | |
513 | }).collect() | |
514 | } | |
515 | ||
516 | ty::TyClosure(def_id, substs) => { | |
517 | substs.upvar_tys(def_id, self).map(|ty| { | |
518 | self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty) | |
519 | }).collect() | |
520 | } | |
521 | ||
522 | ty::TyAdt(def, substs) => { | |
523 | let ty::DtorckConstraint { | |
524 | dtorck_types, outlives | |
525 | } = ty::queries::adt_dtorck_constraint::get(self, span, def.did); | |
526 | Ok(ty::DtorckConstraint { | |
527 | // FIXME: we can try to recursively `dtorck_constraint_on_ty` | |
528 | // there, but that needs some way to handle cycles. | |
529 | dtorck_types: dtorck_types.subst(self, substs), | |
530 | outlives: outlives.subst(self, substs) | |
531 | }) | |
532 | } | |
533 | ||
534 | // Objects must be alive in order for their destructor | |
535 | // to be called. | |
536 | ty::TyDynamic(..) => Ok(ty::DtorckConstraint { | |
537 | outlives: vec![Kind::from(ty)], | |
538 | dtorck_types: vec![], | |
539 | }), | |
540 | ||
541 | // Types that can't be resolved. Pass them forward. | |
542 | ty::TyProjection(..) | ty::TyAnon(..) | ty::TyParam(..) => { | |
543 | Ok(ty::DtorckConstraint { | |
544 | outlives: vec![], | |
545 | dtorck_types: vec![ty], | |
546 | }) | |
547 | } | |
548 | ||
549 | ty::TyInfer(..) | ty::TyError => { | |
550 | self.sess.delay_span_bug(span, "unresolved type in dtorck"); | |
551 | Err(ErrorReported) | |
552 | } | |
553 | }; | |
554 | ||
555 | debug!("dtorck_constraint_for_ty({:?}) = {:?}", ty, result); | |
556 | result | |
557 | } | |
558 | ||
559 | pub fn closure_base_def_id(self, def_id: DefId) -> DefId { | |
476ff2be SL |
560 | let mut def_id = def_id; |
561 | while self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr { | |
562 | def_id = self.parent_def_id(def_id).unwrap_or_else(|| { | |
563 | bug!("closure {:?} has no parent", def_id); | |
564 | }); | |
565 | } | |
566 | def_id | |
9e0c209e | 567 | } |
cc61c64b XL |
568 | |
569 | /// Given the def-id of some item that has no type parameters, make | |
570 | /// a suitable "empty substs" for it. | |
571 | pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> &'tcx ty::Substs<'tcx> { | |
572 | ty::Substs::for_item(self, item_def_id, | |
573 | |_, _| self.types.re_erased, | |
574 | |_, _| { | |
575 | bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id) | |
576 | }) | |
577 | } | |
9e0c209e SL |
578 | } |
579 | ||
476ff2be | 580 | pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> { |
5bcae85e | 581 | tcx: TyCtxt<'a, 'gcx, 'tcx>, |
476ff2be | 582 | state: StableHasher<W>, |
5bcae85e SL |
583 | } |
584 | ||
476ff2be SL |
585 | impl<'a, 'gcx, 'tcx, W> TypeIdHasher<'a, 'gcx, 'tcx, W> |
586 | where W: StableHasherResult | |
587 | { | |
588 | pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self { | |
589 | TypeIdHasher { tcx: tcx, state: StableHasher::new() } | |
9e0c209e SL |
590 | } |
591 | ||
476ff2be SL |
592 | pub fn finish(self) -> W { |
593 | self.state.finish() | |
5bcae85e SL |
594 | } |
595 | ||
476ff2be SL |
596 | pub fn hash<T: Hash>(&mut self, x: T) { |
597 | x.hash(&mut self.state); | |
9e0c209e SL |
598 | } |
599 | ||
5bcae85e SL |
600 | fn hash_discriminant_u8<T>(&mut self, x: &T) { |
601 | let v = unsafe { | |
602 | intrinsics::discriminant_value(x) | |
603 | }; | |
604 | let b = v as u8; | |
605 | assert_eq!(v, b as u64); | |
606 | self.hash(b) | |
607 | } | |
608 | ||
609 | fn def_id(&mut self, did: DefId) { | |
9e0c209e | 610 | // Hash the DefPath corresponding to the DefId, which is independent |
cc61c64b XL |
611 | // of compiler internal state. We already have a stable hash value of |
612 | // all DefPaths available via tcx.def_path_hash(), so we just feed that | |
613 | // into the hasher. | |
614 | let hash = self.tcx.def_path_hash(did); | |
615 | self.hash(hash); | |
5bcae85e SL |
616 | } |
617 | } | |
618 | ||
476ff2be SL |
619 | impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W> |
620 | where W: StableHasherResult | |
621 | { | |
5bcae85e SL |
622 | fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool { |
623 | // Distinguish between the Ty variants uniformly. | |
624 | self.hash_discriminant_u8(&ty.sty); | |
625 | ||
626 | match ty.sty { | |
627 | TyInt(i) => self.hash(i), | |
628 | TyUint(u) => self.hash(u), | |
629 | TyFloat(f) => self.hash(f), | |
5bcae85e SL |
630 | TyArray(_, n) => self.hash(n), |
631 | TyRawPtr(m) | | |
632 | TyRef(_, m) => self.hash(m.mutbl), | |
633 | TyClosure(def_id, _) | | |
634 | TyAnon(def_id, _) | | |
9e0c209e SL |
635 | TyFnDef(def_id, ..) => self.def_id(def_id), |
636 | TyAdt(d, _) => self.def_id(d.did), | |
5bcae85e | 637 | TyFnPtr(f) => { |
8bb4bdeb XL |
638 | self.hash(f.unsafety()); |
639 | self.hash(f.abi()); | |
640 | self.hash(f.variadic()); | |
641 | self.hash(f.inputs().skip_binder().len()); | |
5bcae85e | 642 | } |
476ff2be SL |
643 | TyDynamic(ref data, ..) => { |
644 | if let Some(p) = data.principal() { | |
645 | self.def_id(p.def_id()); | |
646 | } | |
647 | for d in data.auto_traits() { | |
648 | self.def_id(d); | |
649 | } | |
5bcae85e | 650 | } |
8bb4bdeb | 651 | TyTuple(tys, defaulted) => { |
5bcae85e | 652 | self.hash(tys.len()); |
8bb4bdeb | 653 | self.hash(defaulted); |
5bcae85e SL |
654 | } |
655 | TyParam(p) => { | |
5bcae85e SL |
656 | self.hash(p.idx); |
657 | self.hash(p.name.as_str()); | |
658 | } | |
659 | TyProjection(ref data) => { | |
660 | self.def_id(data.trait_ref.def_id); | |
661 | self.hash(data.item_name.as_str()); | |
662 | } | |
663 | TyNever | | |
664 | TyBool | | |
665 | TyChar | | |
666 | TyStr | | |
9e0c209e SL |
667 | TySlice(_) => {} |
668 | ||
669 | TyError | | |
670 | TyInfer(_) => bug!("TypeIdHasher: unexpected type {}", ty) | |
5bcae85e SL |
671 | } |
672 | ||
673 | ty.super_visit_with(self) | |
674 | } | |
675 | ||
9e0c209e | 676 | fn visit_region(&mut self, r: &'tcx ty::Region) -> bool { |
cc61c64b | 677 | self.hash_discriminant_u8(r); |
9e0c209e | 678 | match *r { |
cc61c64b XL |
679 | ty::ReErased | |
680 | ty::ReStatic | | |
681 | ty::ReEmpty => { | |
682 | // No variant fields to hash for these ... | |
5bcae85e SL |
683 | } |
684 | ty::ReLateBound(db, ty::BrAnon(i)) => { | |
cc61c64b | 685 | self.hash(db.depth); |
5bcae85e SL |
686 | self.hash(i); |
687 | } | |
cc61c64b XL |
688 | ty::ReEarlyBound(ty::EarlyBoundRegion { index, name }) => { |
689 | self.hash(index); | |
690 | self.hash(name.as_str()); | |
691 | } | |
5bcae85e SL |
692 | ty::ReLateBound(..) | |
693 | ty::ReFree(..) | | |
694 | ty::ReScope(..) | | |
695 | ty::ReVar(..) | | |
696 | ty::ReSkolemized(..) => { | |
9e0c209e | 697 | bug!("TypeIdHasher: unexpected region {:?}", r) |
5bcae85e SL |
698 | } |
699 | } | |
700 | false | |
701 | } | |
702 | ||
703 | fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, x: &ty::Binder<T>) -> bool { | |
704 | // Anonymize late-bound regions so that, for example: | |
705 | // `for<'a, b> fn(&'a &'b T)` and `for<'a, b> fn(&'b &'a T)` | |
706 | // result in the same TypeId (the two types are equivalent). | |
707 | self.tcx.anonymize_late_bound_regions(x).super_visit_with(self) | |
708 | } | |
709 | } | |
710 | ||
a7813a04 XL |
711 | impl<'a, 'tcx> ty::TyS<'tcx> { |
712 | fn impls_bound(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
713 | param_env: &ParameterEnvironment<'tcx>, | |
476ff2be SL |
714 | def_id: DefId, |
715 | cache: &RefCell<FxHashMap<Ty<'tcx>, bool>>, | |
9e0c209e | 716 | span: Span) -> bool |
e9174d1e | 717 | { |
9e0c209e SL |
718 | if self.has_param_types() || self.has_self_ty() { |
719 | if let Some(result) = cache.borrow().get(self) { | |
720 | return *result; | |
721 | } | |
722 | } | |
723 | let result = | |
8bb4bdeb | 724 | tcx.infer_ctxt(param_env.clone(), Reveal::UserFacing) |
9e0c209e | 725 | .enter(|infcx| { |
476ff2be | 726 | traits::type_known_to_meet_bound(&infcx, self, def_id, span) |
9e0c209e SL |
727 | }); |
728 | if self.has_param_types() || self.has_self_ty() { | |
729 | cache.borrow_mut().insert(self, result); | |
730 | } | |
731 | return result; | |
e9174d1e SL |
732 | } |
733 | ||
734 | // FIXME (@jroesch): I made this public to use it, not sure if should be private | |
a7813a04 XL |
735 | pub fn moves_by_default(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, |
736 | param_env: &ParameterEnvironment<'tcx>, | |
737 | span: Span) -> bool { | |
e9174d1e SL |
738 | if self.flags.get().intersects(TypeFlags::MOVENESS_CACHED) { |
739 | return self.flags.get().intersects(TypeFlags::MOVES_BY_DEFAULT); | |
740 | } | |
741 | ||
742 | assert!(!self.needs_infer()); | |
743 | ||
744 | // Fast-path for primitive types | |
745 | let result = match self.sty { | |
5bcae85e | 746 | TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) | TyNever | |
54a0048b | 747 | TyRawPtr(..) | TyFnDef(..) | TyFnPtr(_) | TyRef(_, TypeAndMut { |
e9174d1e SL |
748 | mutbl: hir::MutImmutable, .. |
749 | }) => Some(false), | |
750 | ||
32a655c1 | 751 | TyStr | TyRef(_, TypeAndMut { |
e9174d1e SL |
752 | mutbl: hir::MutMutable, .. |
753 | }) => Some(true), | |
754 | ||
476ff2be | 755 | TyArray(..) | TySlice(..) | TyDynamic(..) | TyTuple(..) | |
9e0c209e | 756 | TyClosure(..) | TyAdt(..) | TyAnon(..) | |
e9174d1e | 757 | TyProjection(..) | TyParam(..) | TyInfer(..) | TyError => None |
9e0c209e | 758 | }.unwrap_or_else(|| { |
476ff2be SL |
759 | !self.impls_bound(tcx, param_env, |
760 | tcx.require_lang_item(lang_items::CopyTraitLangItem), | |
761 | ¶m_env.is_copy_cache, span) }); | |
e9174d1e SL |
762 | |
763 | if !self.has_param_types() && !self.has_self_ty() { | |
764 | self.flags.set(self.flags.get() | if result { | |
765 | TypeFlags::MOVENESS_CACHED | TypeFlags::MOVES_BY_DEFAULT | |
766 | } else { | |
767 | TypeFlags::MOVENESS_CACHED | |
768 | }); | |
769 | } | |
770 | ||
771 | result | |
772 | } | |
773 | ||
774 | #[inline] | |
a7813a04 XL |
775 | pub fn is_sized(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, |
776 | param_env: &ParameterEnvironment<'tcx>, | |
777 | span: Span) -> bool | |
e9174d1e SL |
778 | { |
779 | if self.flags.get().intersects(TypeFlags::SIZEDNESS_CACHED) { | |
780 | return self.flags.get().intersects(TypeFlags::IS_SIZED); | |
781 | } | |
782 | ||
a7813a04 | 783 | self.is_sized_uncached(tcx, param_env, span) |
e9174d1e SL |
784 | } |
785 | ||
a7813a04 XL |
786 | fn is_sized_uncached(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, |
787 | param_env: &ParameterEnvironment<'tcx>, | |
788 | span: Span) -> bool { | |
e9174d1e SL |
789 | assert!(!self.needs_infer()); |
790 | ||
791 | // Fast-path for primitive types | |
792 | let result = match self.sty { | |
793 | TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) | | |
32a655c1 | 794 | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) | |
5bcae85e | 795 | TyArray(..) | TyTuple(..) | TyClosure(..) | TyNever => Some(true), |
e9174d1e | 796 | |
476ff2be | 797 | TyStr | TyDynamic(..) | TySlice(_) => Some(false), |
e9174d1e | 798 | |
9e0c209e | 799 | TyAdt(..) | TyProjection(..) | TyParam(..) | |
5bcae85e | 800 | TyInfer(..) | TyAnon(..) | TyError => None |
9e0c209e | 801 | }.unwrap_or_else(|| { |
476ff2be SL |
802 | self.impls_bound(tcx, param_env, tcx.require_lang_item(lang_items::SizedTraitLangItem), |
803 | ¶m_env.is_sized_cache, span) }); | |
e9174d1e SL |
804 | |
805 | if !self.has_param_types() && !self.has_self_ty() { | |
806 | self.flags.set(self.flags.get() | if result { | |
807 | TypeFlags::SIZEDNESS_CACHED | TypeFlags::IS_SIZED | |
808 | } else { | |
809 | TypeFlags::SIZEDNESS_CACHED | |
810 | }); | |
811 | } | |
812 | ||
813 | result | |
814 | } | |
815 | ||
cc61c64b XL |
816 | /// Returns `true` if and only if there are no `UnsafeCell`s |
817 | /// nested within the type (ignoring `PhantomData` or pointers). | |
818 | #[inline] | |
819 | pub fn is_freeze(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
820 | param_env: &ParameterEnvironment<'tcx>, | |
821 | span: Span) -> bool | |
822 | { | |
823 | if self.flags.get().intersects(TypeFlags::FREEZENESS_CACHED) { | |
824 | return self.flags.get().intersects(TypeFlags::IS_FREEZE); | |
825 | } | |
826 | ||
827 | self.is_freeze_uncached(tcx, param_env, span) | |
828 | } | |
829 | ||
830 | fn is_freeze_uncached(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
831 | param_env: &ParameterEnvironment<'tcx>, | |
832 | span: Span) -> bool { | |
833 | assert!(!self.needs_infer()); | |
834 | ||
835 | // Fast-path for primitive types | |
836 | let result = match self.sty { | |
837 | TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) | | |
838 | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) | | |
839 | TyStr | TyNever => Some(true), | |
840 | ||
841 | TyArray(..) | TySlice(_) | | |
842 | TyTuple(..) | TyClosure(..) | TyAdt(..) | | |
843 | TyDynamic(..) | TyProjection(..) | TyParam(..) | | |
844 | TyInfer(..) | TyAnon(..) | TyError => None | |
845 | }.unwrap_or_else(|| { | |
846 | self.impls_bound(tcx, param_env, tcx.require_lang_item(lang_items::FreezeTraitLangItem), | |
847 | ¶m_env.is_freeze_cache, span) }); | |
848 | ||
849 | if !self.has_param_types() && !self.has_self_ty() { | |
850 | self.flags.set(self.flags.get() | if result { | |
851 | TypeFlags::FREEZENESS_CACHED | TypeFlags::IS_FREEZE | |
852 | } else { | |
853 | TypeFlags::FREEZENESS_CACHED | |
854 | }); | |
855 | } | |
856 | ||
857 | result | |
858 | } | |
859 | ||
860 | /// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely | |
861 | /// non-copy and *might* have a destructor attached; if it returns | |
862 | /// `false`, then `ty` definitely has no destructor (i.e. no drop glue). | |
863 | /// | |
864 | /// (Note that this implies that if `ty` has a destructor attached, | |
865 | /// then `needs_drop` will definitely return `true` for `ty`.) | |
866 | #[inline] | |
867 | pub fn needs_drop(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
868 | param_env: &ty::ParameterEnvironment<'tcx>) -> bool { | |
869 | if self.flags.get().intersects(TypeFlags::NEEDS_DROP_CACHED) { | |
870 | return self.flags.get().intersects(TypeFlags::NEEDS_DROP); | |
871 | } | |
872 | ||
873 | self.needs_drop_uncached(tcx, param_env, &mut FxHashSet()) | |
874 | } | |
875 | ||
876 | fn needs_drop_inner(&'tcx self, | |
877 | tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
878 | param_env: &ty::ParameterEnvironment<'tcx>, | |
879 | stack: &mut FxHashSet<Ty<'tcx>>) | |
880 | -> bool { | |
881 | if self.flags.get().intersects(TypeFlags::NEEDS_DROP_CACHED) { | |
882 | return self.flags.get().intersects(TypeFlags::NEEDS_DROP); | |
883 | } | |
884 | ||
885 | // This should be reported as an error by `check_representable`. | |
886 | // | |
887 | // Consider the type as not needing drop in the meanwhile to avoid | |
888 | // further errors. | |
889 | if let Some(_) = stack.replace(self) { | |
890 | return false; | |
891 | } | |
892 | ||
893 | let needs_drop = self.needs_drop_uncached(tcx, param_env, stack); | |
894 | ||
895 | // "Pop" the cycle detection "stack". | |
896 | stack.remove(self); | |
897 | ||
898 | needs_drop | |
899 | } | |
900 | ||
901 | fn needs_drop_uncached(&'tcx self, | |
902 | tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
903 | param_env: &ty::ParameterEnvironment<'tcx>, | |
904 | stack: &mut FxHashSet<Ty<'tcx>>) | |
905 | -> bool { | |
906 | assert!(!self.needs_infer()); | |
907 | ||
908 | let result = match self.sty { | |
909 | // Fast-path for primitive types | |
910 | ty::TyInfer(ty::FreshIntTy(_)) | ty::TyInfer(ty::FreshFloatTy(_)) | | |
911 | ty::TyBool | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) | ty::TyNever | | |
912 | ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar | | |
913 | ty::TyRawPtr(_) | ty::TyRef(..) | ty::TyStr => false, | |
914 | ||
915 | // Issue #22536: We first query type_moves_by_default. It sees a | |
916 | // normalized version of the type, and therefore will definitely | |
917 | // know whether the type implements Copy (and thus needs no | |
918 | // cleanup/drop/zeroing) ... | |
919 | _ if !self.moves_by_default(tcx, param_env, DUMMY_SP) => false, | |
920 | ||
921 | // ... (issue #22536 continued) but as an optimization, still use | |
922 | // prior logic of asking for the structural "may drop". | |
923 | ||
924 | // FIXME(#22815): Note that this is a conservative heuristic; | |
925 | // it may report that the type "may drop" when actual type does | |
926 | // not actually have a destructor associated with it. But since | |
927 | // the type absolutely did not have the `Copy` bound attached | |
928 | // (see above), it is sound to treat it as having a destructor. | |
929 | ||
930 | // User destructors are the only way to have concrete drop types. | |
931 | ty::TyAdt(def, _) if def.has_dtor(tcx) => true, | |
932 | ||
933 | // Can refer to a type which may drop. | |
934 | // FIXME(eddyb) check this against a ParameterEnvironment. | |
935 | ty::TyDynamic(..) | ty::TyProjection(..) | ty::TyParam(_) | | |
936 | ty::TyAnon(..) | ty::TyInfer(_) | ty::TyError => true, | |
937 | ||
938 | // Structural recursion. | |
939 | ty::TyArray(ty, _) | ty::TySlice(ty) => { | |
940 | ty.needs_drop_inner(tcx, param_env, stack) | |
941 | } | |
942 | ||
943 | ty::TyClosure(def_id, ref substs) => { | |
944 | substs.upvar_tys(def_id, tcx) | |
945 | .any(|ty| ty.needs_drop_inner(tcx, param_env, stack)) | |
946 | } | |
947 | ||
948 | ty::TyTuple(ref tys, _) => { | |
949 | tys.iter().any(|ty| ty.needs_drop_inner(tcx, param_env, stack)) | |
950 | } | |
951 | ||
952 | // unions don't have destructors regardless of the child types | |
953 | ty::TyAdt(def, _) if def.is_union() => false, | |
954 | ||
955 | ty::TyAdt(def, substs) => { | |
956 | def.variants.iter().any(|v| { | |
957 | v.fields.iter().any(|f| { | |
958 | f.ty(tcx, substs).needs_drop_inner(tcx, param_env, stack) | |
959 | }) | |
960 | }) | |
961 | } | |
962 | }; | |
963 | ||
964 | if !self.has_param_types() && !self.has_self_ty() { | |
965 | self.flags.set(self.flags.get() | if result { | |
966 | TypeFlags::NEEDS_DROP_CACHED | TypeFlags::NEEDS_DROP | |
967 | } else { | |
968 | TypeFlags::NEEDS_DROP_CACHED | |
969 | }); | |
970 | } | |
971 | ||
972 | result | |
973 | } | |
974 | ||
54a0048b | 975 | #[inline] |
a7813a04 XL |
976 | pub fn layout<'lcx>(&'tcx self, infcx: &InferCtxt<'a, 'tcx, 'lcx>) |
977 | -> Result<&'tcx Layout, LayoutError<'tcx>> { | |
978 | let tcx = infcx.tcx.global_tcx(); | |
54a0048b SL |
979 | let can_cache = !self.has_param_types() && !self.has_self_ty(); |
980 | if can_cache { | |
a7813a04 | 981 | if let Some(&cached) = tcx.layout_cache.borrow().get(&self) { |
54a0048b SL |
982 | return Ok(cached); |
983 | } | |
984 | } | |
985 | ||
9e0c209e SL |
986 | let rec_limit = tcx.sess.recursion_limit.get(); |
987 | let depth = tcx.layout_depth.get(); | |
988 | if depth > rec_limit { | |
989 | tcx.sess.fatal( | |
990 | &format!("overflow representing the type `{}`", self)); | |
991 | } | |
992 | ||
993 | tcx.layout_depth.set(depth+1); | |
8bb4bdeb XL |
994 | let layout = Layout::compute_uncached(self, infcx); |
995 | tcx.layout_depth.set(depth); | |
996 | let layout = layout?; | |
54a0048b | 997 | if can_cache { |
a7813a04 | 998 | tcx.layout_cache.borrow_mut().insert(self, layout); |
54a0048b SL |
999 | } |
1000 | Ok(layout) | |
1001 | } | |
1002 | ||
e9174d1e SL |
1003 | |
1004 | /// Check whether a type is representable. This means it cannot contain unboxed | |
1005 | /// structural recursion. This check is needed for structs and enums. | |
a7813a04 XL |
1006 | pub fn is_representable(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span) |
1007 | -> Representability { | |
e9174d1e SL |
1008 | |
1009 | // Iterate until something non-representable is found | |
a7813a04 XL |
1010 | fn find_nonrepresentable<'a, 'tcx, It>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
1011 | sp: Span, | |
1012 | seen: &mut Vec<Ty<'tcx>>, | |
1013 | iter: It) | |
1014 | -> Representability | |
1015 | where It: Iterator<Item=Ty<'tcx>> { | |
e9174d1e | 1016 | iter.fold(Representability::Representable, |
a7813a04 | 1017 | |r, ty| cmp::max(r, is_type_structurally_recursive(tcx, sp, seen, ty))) |
e9174d1e SL |
1018 | } |
1019 | ||
a7813a04 XL |
1020 | fn are_inner_types_recursive<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, |
1021 | seen: &mut Vec<Ty<'tcx>>, ty: Ty<'tcx>) | |
1022 | -> Representability { | |
e9174d1e | 1023 | match ty.sty { |
8bb4bdeb | 1024 | TyTuple(ref ts, _) => { |
a7813a04 | 1025 | find_nonrepresentable(tcx, sp, seen, ts.iter().cloned()) |
e9174d1e SL |
1026 | } |
1027 | // Fixed-length vectors. | |
1028 | // FIXME(#11924) Behavior undecided for zero-length vectors. | |
1029 | TyArray(ty, _) => { | |
a7813a04 | 1030 | is_type_structurally_recursive(tcx, sp, seen, ty) |
e9174d1e | 1031 | } |
9e0c209e | 1032 | TyAdt(def, substs) => { |
a7813a04 | 1033 | find_nonrepresentable(tcx, |
e9174d1e SL |
1034 | sp, |
1035 | seen, | |
a7813a04 | 1036 | def.all_fields().map(|f| f.ty(tcx, substs))) |
e9174d1e SL |
1037 | } |
1038 | TyClosure(..) => { | |
1039 | // this check is run on type definitions, so we don't expect | |
1040 | // to see closure types | |
54a0048b | 1041 | bug!("requires check invoked on inapplicable type: {:?}", ty) |
e9174d1e SL |
1042 | } |
1043 | _ => Representability::Representable, | |
1044 | } | |
1045 | } | |
1046 | ||
476ff2be | 1047 | fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: &'tcx ty::AdtDef) -> bool { |
e9174d1e | 1048 | match ty.sty { |
9e0c209e | 1049 | TyAdt(ty_def, _) => { |
e9174d1e SL |
1050 | ty_def == def |
1051 | } | |
1052 | _ => false | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { | |
1057 | match (&a.sty, &b.sty) { | |
9e0c209e | 1058 | (&TyAdt(did_a, substs_a), &TyAdt(did_b, substs_b)) => { |
e9174d1e SL |
1059 | if did_a != did_b { |
1060 | return false; | |
1061 | } | |
1062 | ||
9e0c209e | 1063 | substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type(a, b)) |
e9174d1e | 1064 | } |
cc61c64b | 1065 | _ => a == b, |
e9174d1e SL |
1066 | } |
1067 | } | |
1068 | ||
1069 | // Does the type `ty` directly (without indirection through a pointer) | |
1070 | // contain any types on stack `seen`? | |
a7813a04 XL |
1071 | fn is_type_structurally_recursive<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, |
1072 | sp: Span, | |
1073 | seen: &mut Vec<Ty<'tcx>>, | |
1074 | ty: Ty<'tcx>) -> Representability { | |
e9174d1e SL |
1075 | debug!("is_type_structurally_recursive: {:?}", ty); |
1076 | ||
1077 | match ty.sty { | |
9e0c209e | 1078 | TyAdt(def, _) => { |
e9174d1e SL |
1079 | { |
1080 | // Iterate through stack of previously seen types. | |
1081 | let mut iter = seen.iter(); | |
1082 | ||
1083 | // The first item in `seen` is the type we are actually curious about. | |
1084 | // We want to return SelfRecursive if this type contains itself. | |
1085 | // It is important that we DON'T take generic parameters into account | |
1086 | // for this check, so that Bar<T> in this example counts as SelfRecursive: | |
1087 | // | |
1088 | // struct Foo; | |
1089 | // struct Bar<T> { x: Bar<Foo> } | |
1090 | ||
3157f602 XL |
1091 | if let Some(&seen_type) = iter.next() { |
1092 | if same_struct_or_enum(seen_type, def) { | |
1093 | debug!("SelfRecursive: {:?} contains {:?}", | |
1094 | seen_type, | |
1095 | ty); | |
1096 | return Representability::SelfRecursive; | |
e9174d1e | 1097 | } |
e9174d1e SL |
1098 | } |
1099 | ||
1100 | // We also need to know whether the first item contains other types | |
1101 | // that are structurally recursive. If we don't catch this case, we | |
1102 | // will recurse infinitely for some inputs. | |
1103 | // | |
1104 | // It is important that we DO take generic parameters into account | |
1105 | // here, so that code like this is considered SelfRecursive, not | |
1106 | // ContainsRecursive: | |
1107 | // | |
1108 | // struct Foo { Option<Option<Foo>> } | |
1109 | ||
1110 | for &seen_type in iter { | |
1111 | if same_type(ty, seen_type) { | |
1112 | debug!("ContainsRecursive: {:?} contains {:?}", | |
1113 | seen_type, | |
1114 | ty); | |
1115 | return Representability::ContainsRecursive; | |
1116 | } | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | // For structs and enums, track all previously seen types by pushing them | |
1121 | // onto the 'seen' stack. | |
1122 | seen.push(ty); | |
a7813a04 | 1123 | let out = are_inner_types_recursive(tcx, sp, seen, ty); |
e9174d1e SL |
1124 | seen.pop(); |
1125 | out | |
1126 | } | |
1127 | _ => { | |
1128 | // No need to push in other cases. | |
a7813a04 | 1129 | are_inner_types_recursive(tcx, sp, seen, ty) |
e9174d1e SL |
1130 | } |
1131 | } | |
1132 | } | |
1133 | ||
1134 | debug!("is_type_representable: {:?}", self); | |
1135 | ||
1136 | // To avoid a stack overflow when checking an enum variant or struct that | |
1137 | // contains a different, structurally recursive type, maintain a stack | |
1138 | // of seen types and check recursion for each of them (issues #3008, #3779). | |
1139 | let mut seen: Vec<Ty> = Vec::new(); | |
a7813a04 | 1140 | let r = is_type_structurally_recursive(tcx, sp, &mut seen, self); |
e9174d1e SL |
1141 | debug!("is_type_representable: {:?} is {:?}", self, r); |
1142 | r | |
1143 | } | |
1144 | } |