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.
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.
11 //! misc. type-system utilities too small to deserve their own file
13 use hir::def_id::{DefId, LOCAL_CRATE};
14 use hir::map::DefPathData;
16 use ich::{StableHashingContext, NodeIdHashingMode};
17 use traits::{self, Reveal};
18 use ty::{self, Ty, TyCtxt, TypeAndMut, TypeFlags, TypeFoldable};
19 use ty::ParameterEnvironment;
20 use ty::fold::TypeVisitor;
21 use ty::layout::{Layout, LayoutError};
22 use ty::subst::{Subst, Kind};
23 use ty::TypeVariants::*;
24 use util::common::ErrorReported;
25 use util::nodemap::{FxHashMap, FxHashSet};
26 use middle::lang_items;
28 use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
29 use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
31 use std::cell::RefCell;
35 use syntax::ast::{self, Name};
36 use syntax::attr::{self, SignedInt, UnsignedInt};
37 use syntax_pos::{Span, DUMMY_SP};
43 pub trait IntTypeExt {
44 fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
45 fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
47 fn assert_ty_matches(&self, val: Disr);
48 fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr;
52 macro_rules! typed_literal {
53 ($tcx:expr, $ty:expr, $lit:expr) => {
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)),
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)),
81 impl IntTypeExt for attr::IntType {
82 fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
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,
88 SignedInt(ast::IntTy::I128) => tcx.types.i128,
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,
94 UnsignedInt(ast::UintTy::U128) => tcx.types.u128,
95 UnsignedInt(ast::UintTy::Us) => tcx.types.usize,
99 fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr {
100 typed_literal!(tcx, *self, 0)
103 fn assert_ty_matches(&self, val: Disr) {
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(_)) => {},
109 (SignedInt(ast::IntTy::I128), ConstInt::I128(_)) => {},
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(_)) => {},
115 (UnsignedInt(ast::UintTy::U128), ConstInt::U128(_)) => {},
116 (UnsignedInt(ast::UintTy::Us), ConstInt::Usize(_)) => {},
117 _ => bug!("disr type mismatch: {:?} vs {:?}", self, val),
121 fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
123 if let Some(val) = val {
124 self.assert_ty_matches(val);
125 (val + typed_literal!(tcx, *self, 1)).ok()
127 Some(self.initial_discriminant(tcx))
133 #[derive(Copy, Clone)]
134 pub enum CopyImplementationError<'tcx> {
135 InfrigingField(&'tcx ty::FieldDef),
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.
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 {
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)
158 -> Result<(), CopyImplementationError> {
159 // FIXME: (@jroesch) float this code up
160 tcx.infer_ctxt(self.clone(), Reveal::UserFacing).enter(|infcx| {
161 let (adt, substs) = match self_type.sty {
162 ty::TyAdt(adt, substs) => (adt, substs),
163 _ => return Err(CopyImplementationError::NotAnAdt),
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),
174 for variant in &adt.variants {
175 for field in &variant.fields {
176 if !field_implements_copy(field) {
177 return Err(CopyImplementationError::InfrigingField(field));
182 if adt.has_dtor(tcx) {
183 return Err(CopyImplementationError::HasDestructor);
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);
198 hcx.while_hashing_spans(false, |hcx| {
199 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
200 ty.hash_stable(hcx, &mut hasher);
207 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
208 pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
210 ty::TyAdt(def, substs) => {
211 for field in def.all_fields() {
212 let field_ty = field.ty(self, substs);
213 if let TyError = field_ty.sty {
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.
225 pub fn positional_element_ty(self,
228 variant: Option<DefId>) -> Option<Ty<'tcx>> {
229 match (&ty.sty, variant) {
230 (&TyAdt(adt, substs), Some(vid)) => {
231 adt.variant_with_id(vid).fields.get(i).map(|f| f.ty(self, substs))
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))
237 (&TyTuple(ref v, _), None) => v.get(i).cloned(),
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.
244 pub fn named_element_ty(self,
247 variant: Option<DefId>) -> Option<Ty<'tcx>> {
248 match (&ty.sty, variant) {
249 (&TyAdt(adt, substs), Some(vid)) => {
250 adt.variant_with_id(vid).find_field_named(n).map(|f| f.ty(self, substs))
252 (&TyAdt(adt, substs), None) => {
253 adt.struct_variant().find_field_named(n).map(|f| f.ty(self, substs))
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.
262 pub fn struct_tail(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
263 while let TyAdt(def, substs) = ty.sty {
264 if !def.is_struct() {
267 match def.struct_variant().fields.last() {
268 Some(f) => ty = f.ty(self, substs),
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.
280 pub fn struct_lockstep_tails(self,
283 -> (Ty<'tcx>, Ty<'tcx>) {
284 let (mut a, mut b) = (source, target);
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() {
289 match a_def.struct_variant().fields.last() {
291 a = f.ty(self, a_substs);
292 b = f.ty(self, b_substs);
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
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`.
314 /// Requires that trait definitions have been processed so that we can
315 /// elaborate predicates and walk supertraits.
316 pub fn required_region_bounds(self,
317 erased_self_ty: Ty<'tcx>,
318 predicates: Vec<ty::Predicate<'tcx>>)
319 -> Vec<&'tcx ty::Region> {
320 debug!("required_region_bounds(erased_self_ty={:?}, predicates={:?})",
324 assert!(!erased_self_ty.has_escaping_regions());
326 traits::elaborate_predicates(self, predicates)
327 .filter_map(|predicate| {
329 ty::Predicate::Projection(..) |
330 ty::Predicate::Trait(..) |
331 ty::Predicate::Equate(..) |
332 ty::Predicate::Subtype(..) |
333 ty::Predicate::WellFormed(..) |
334 ty::Predicate::ObjectSafe(..) |
335 ty::Predicate::ClosureKind(..) |
336 ty::Predicate::RegionOutlives(..) => {
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() {
360 /// Calculate the destructor of a given type.
361 pub fn calculate_dtor(
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() {
372 self.coherent_trait((LOCAL_CRATE, drop_trait));
374 let mut dtor_did = None;
375 let ty = self.type_of(adt_did);
376 self.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);
384 let dtor_did = match dtor_did {
389 Some(ty::Destructor { did: dtor_did })
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
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>>
402 let dtor = match def.destructor(self) {
404 debug!("destructor_constraints({:?}) - no dtor", def.did);
407 Some(dtor) => dtor.did
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.
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>`).
419 if self.has_attr(dtor, "unsafe_destructor_blind_to_params") {
420 debug!("destructor_constraint({:?}) - blind", def.did);
424 let impl_def_id = self.associated_item(dtor).container.id();
425 let impl_generics = self.generics_of(impl_def_id);
427 // We have a destructor - all the parameters that are not
428 // pure_wrt_drop (i.e, don't have a #[may_dangle] attribute)
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:
435 // Suppose our ADT looks like the following
437 // struct S<X, Y, Z>(X, Y, Z);
441 // impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0>
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.
448 let impl_substs = match self.type_of(impl_def_id).sty {
449 ty::TyAdt(def_, substs) if def_ == def => substs,
453 let item_substs = match self.type_of(def.did).sty {
454 ty::TyAdt(def_, substs) if def_ == def => substs,
458 let result = item_substs.iter().zip(impl_substs.iter())
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), ..
465 !impl_generics.type_param(pt).pure_wrt_drop
467 // not a type or region param - this should be reported
471 }).map(|(&item_param, _)| item_param).collect();
472 debug!("destructor_constraint({:?}) = {:?}", def.did, result);
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,
483 -> Result<ty::DtorckConstraint<'tcx>, ErrorReported>
485 debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})",
486 span, for_ty, depth, ty);
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));
494 return Err(ErrorReported);
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())
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)
510 ty::TyTuple(tys, _) => {
511 tys.iter().map(|ty| {
512 self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty)
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)
522 ty::TyAdt(def, substs) => {
523 let ty::DtorckConstraint {
524 dtorck_types, outlives
525 } = self.at(span).adt_dtorck_constraint(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)
534 // Objects must be alive in order for their destructor
536 ty::TyDynamic(..) => Ok(ty::DtorckConstraint {
537 outlives: vec![Kind::from(ty)],
538 dtorck_types: vec![],
541 // Types that can't be resolved. Pass them forward.
542 ty::TyProjection(..) | ty::TyAnon(..) | ty::TyParam(..) => {
543 Ok(ty::DtorckConstraint {
545 dtorck_types: vec![ty],
549 ty::TyInfer(..) | ty::TyError => {
550 self.sess.delay_span_bug(span, "unresolved type in dtorck");
555 debug!("dtorck_constraint_for_ty({:?}) = {:?}", ty, result);
559 pub fn closure_base_def_id(self, def_id: DefId) -> DefId {
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);
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,
575 bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
580 pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {
581 tcx: TyCtxt<'a, 'gcx, 'tcx>,
582 state: StableHasher<W>,
585 impl<'a, 'gcx, 'tcx, W> TypeIdHasher<'a, 'gcx, 'tcx, W>
586 where W: StableHasherResult
588 pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self {
589 TypeIdHasher { tcx: tcx, state: StableHasher::new() }
592 pub fn finish(self) -> W {
596 pub fn hash<T: Hash>(&mut self, x: T) {
597 x.hash(&mut self.state);
600 fn hash_discriminant_u8<T>(&mut self, x: &T) {
602 intrinsics::discriminant_value(x)
605 assert_eq!(v, b as u64);
609 fn def_id(&mut self, did: DefId) {
610 // Hash the DefPath corresponding to the DefId, which is independent
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
614 let hash = self.tcx.def_path_hash(did);
619 impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
620 where W: StableHasherResult
622 fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
623 // Distinguish between the Ty variants uniformly.
624 self.hash_discriminant_u8(&ty.sty);
627 TyInt(i) => self.hash(i),
628 TyUint(u) => self.hash(u),
629 TyFloat(f) => self.hash(f),
630 TyArray(_, n) => self.hash(n),
632 TyRef(_, m) => self.hash(m.mutbl),
633 TyClosure(def_id, _) |
635 TyFnDef(def_id, ..) => self.def_id(def_id),
636 TyAdt(d, _) => self.def_id(d.did),
638 self.hash(f.unsafety());
640 self.hash(f.variadic());
641 self.hash(f.inputs().skip_binder().len());
643 TyDynamic(ref data, ..) => {
644 if let Some(p) = data.principal() {
645 self.def_id(p.def_id());
647 for d in data.auto_traits() {
651 TyTuple(tys, defaulted) => {
652 self.hash(tys.len());
653 self.hash(defaulted);
657 self.hash(p.name.as_str());
659 TyProjection(ref data) => {
660 self.def_id(data.trait_ref.def_id);
661 self.hash(data.item_name.as_str());
670 TyInfer(_) => bug!("TypeIdHasher: unexpected type {}", ty)
673 ty.super_visit_with(self)
676 fn visit_region(&mut self, r: &'tcx ty::Region) -> bool {
677 self.hash_discriminant_u8(r);
682 // No variant fields to hash for these ...
684 ty::ReLateBound(db, ty::BrAnon(i)) => {
688 ty::ReEarlyBound(ty::EarlyBoundRegion { index, name }) => {
690 self.hash(name.as_str());
692 ty::ReLateBound(..) |
696 ty::ReSkolemized(..) => {
697 bug!("TypeIdHasher: unexpected region {:?}", r)
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)
711 impl<'a, 'tcx> ty::TyS<'tcx> {
712 fn impls_bound(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
713 param_env: &ParameterEnvironment<'tcx>,
715 cache: &RefCell<FxHashMap<Ty<'tcx>, bool>>,
718 if self.has_param_types() || self.has_self_ty() {
719 if let Some(result) = cache.borrow().get(self) {
724 tcx.infer_ctxt(param_env.clone(), Reveal::UserFacing)
726 traits::type_known_to_meet_bound(&infcx, self, def_id, span)
728 if self.has_param_types() || self.has_self_ty() {
729 cache.borrow_mut().insert(self, result);
734 // FIXME (@jroesch): I made this public to use it, not sure if should be private
735 pub fn moves_by_default(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
736 param_env: &ParameterEnvironment<'tcx>,
737 span: Span) -> bool {
738 if self.flags.get().intersects(TypeFlags::MOVENESS_CACHED) {
739 return self.flags.get().intersects(TypeFlags::MOVES_BY_DEFAULT);
742 assert!(!self.needs_infer());
744 // Fast-path for primitive types
745 let result = match self.sty {
746 TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) | TyNever |
747 TyRawPtr(..) | TyFnDef(..) | TyFnPtr(_) | TyRef(_, TypeAndMut {
748 mutbl: hir::MutImmutable, ..
751 TyStr | TyRef(_, TypeAndMut {
752 mutbl: hir::MutMutable, ..
755 TyArray(..) | TySlice(..) | TyDynamic(..) | TyTuple(..) |
756 TyClosure(..) | TyAdt(..) | TyAnon(..) |
757 TyProjection(..) | TyParam(..) | TyInfer(..) | TyError => None
758 }.unwrap_or_else(|| {
759 !self.impls_bound(tcx, param_env,
760 tcx.require_lang_item(lang_items::CopyTraitLangItem),
761 ¶m_env.is_copy_cache, span) });
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
767 TypeFlags::MOVENESS_CACHED
775 pub fn is_sized(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
776 param_env: &ParameterEnvironment<'tcx>,
779 if self.flags.get().intersects(TypeFlags::SIZEDNESS_CACHED) {
780 return self.flags.get().intersects(TypeFlags::IS_SIZED);
783 self.is_sized_uncached(tcx, param_env, span)
786 fn is_sized_uncached(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
787 param_env: &ParameterEnvironment<'tcx>,
788 span: Span) -> bool {
789 assert!(!self.needs_infer());
791 // Fast-path for primitive types
792 let result = match self.sty {
793 TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
794 TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
795 TyArray(..) | TyTuple(..) | TyClosure(..) | TyNever => Some(true),
797 TyStr | TyDynamic(..) | TySlice(_) => Some(false),
799 TyAdt(..) | TyProjection(..) | TyParam(..) |
800 TyInfer(..) | TyAnon(..) | TyError => None
801 }.unwrap_or_else(|| {
802 self.impls_bound(tcx, param_env, tcx.require_lang_item(lang_items::SizedTraitLangItem),
803 ¶m_env.is_sized_cache, span) });
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
809 TypeFlags::SIZEDNESS_CACHED
816 /// Returns `true` if and only if there are no `UnsafeCell`s
817 /// nested within the type (ignoring `PhantomData` or pointers).
819 pub fn is_freeze(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
820 param_env: &ParameterEnvironment<'tcx>,
823 if self.flags.get().intersects(TypeFlags::FREEZENESS_CACHED) {
824 return self.flags.get().intersects(TypeFlags::IS_FREEZE);
827 self.is_freeze_uncached(tcx, param_env, span)
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());
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),
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) });
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
853 TypeFlags::FREEZENESS_CACHED
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).
864 /// (Note that this implies that if `ty` has a destructor attached,
865 /// then `needs_drop` will definitely return `true` for `ty`.)
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);
873 self.needs_drop_uncached(tcx, param_env, &mut FxHashSet())
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>>)
881 if self.flags.get().intersects(TypeFlags::NEEDS_DROP_CACHED) {
882 return self.flags.get().intersects(TypeFlags::NEEDS_DROP);
885 // This should be reported as an error by `check_representable`.
887 // Consider the type as not needing drop in the meanwhile to avoid
889 if let Some(_) = stack.replace(self) {
893 let needs_drop = self.needs_drop_uncached(tcx, param_env, stack);
895 // "Pop" the cycle detection "stack".
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>>)
906 assert!(!self.needs_infer());
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,
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,
921 // ... (issue #22536 continued) but as an optimization, still use
922 // prior logic of asking for the structural "may drop".
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.
930 // User destructors are the only way to have concrete drop types.
931 ty::TyAdt(def, _) if def.has_dtor(tcx) => true,
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,
938 // Structural recursion.
939 ty::TyArray(ty, _) | ty::TySlice(ty) => {
940 ty.needs_drop_inner(tcx, param_env, stack)
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))
948 ty::TyTuple(ref tys, _) => {
949 tys.iter().any(|ty| ty.needs_drop_inner(tcx, param_env, stack))
952 // unions don't have destructors regardless of the child types
953 ty::TyAdt(def, _) if def.is_union() => false,
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)
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
968 TypeFlags::NEEDS_DROP_CACHED
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();
979 let can_cache = !self.has_param_types() && !self.has_self_ty();
981 if let Some(&cached) = tcx.layout_cache.borrow().get(&self) {
986 let rec_limit = tcx.sess.recursion_limit.get();
987 let depth = tcx.layout_depth.get();
988 if depth > rec_limit {
990 &format!("overflow representing the type `{}`", self));
993 tcx.layout_depth.set(depth+1);
994 let layout = Layout::compute_uncached(self, infcx);
995 tcx.layout_depth.set(depth);
996 let layout = layout?;
998 tcx.layout_cache.borrow_mut().insert(self, layout);
1004 /// Check whether a type is representable. This means it cannot contain unboxed
1005 /// structural recursion. This check is needed for structs and enums.
1006 pub fn is_representable(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span)
1007 -> Representability {
1009 // Iterate until something non-representable is found
1010 fn find_nonrepresentable<'a, 'tcx, It>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1012 seen: &mut Vec<Ty<'tcx>>,
1015 where It: Iterator<Item=Ty<'tcx>> {
1016 iter.fold(Representability::Representable,
1017 |r, ty| cmp::max(r, is_type_structurally_recursive(tcx, sp, seen, ty)))
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 {
1024 TyTuple(ref ts, _) => {
1025 find_nonrepresentable(tcx, sp, seen, ts.iter().cloned())
1027 // Fixed-length vectors.
1028 // FIXME(#11924) Behavior undecided for zero-length vectors.
1030 is_type_structurally_recursive(tcx, sp, seen, ty)
1032 TyAdt(def, substs) => {
1033 find_nonrepresentable(tcx,
1036 def.all_fields().map(|f| f.ty(tcx, substs)))
1039 // this check is run on type definitions, so we don't expect
1040 // to see closure types
1041 bug!("requires check invoked on inapplicable type: {:?}", ty)
1043 _ => Representability::Representable,
1047 fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: &'tcx ty::AdtDef) -> bool {
1049 TyAdt(ty_def, _) => {
1056 fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
1057 match (&a.sty, &b.sty) {
1058 (&TyAdt(did_a, substs_a), &TyAdt(did_b, substs_b)) => {
1063 substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type(a, b))
1069 // Does the type `ty` directly (without indirection through a pointer)
1070 // contain any types on stack `seen`?
1071 fn is_type_structurally_recursive<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1073 seen: &mut Vec<Ty<'tcx>>,
1074 ty: Ty<'tcx>) -> Representability {
1075 debug!("is_type_structurally_recursive: {:?}", ty);
1080 // Iterate through stack of previously seen types.
1081 let mut iter = seen.iter();
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:
1089 // struct Bar<T> { x: Bar<Foo> }
1091 if let Some(&seen_type) = iter.next() {
1092 if same_struct_or_enum(seen_type, def) {
1093 debug!("SelfRecursive: {:?} contains {:?}",
1096 return Representability::SelfRecursive;
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.
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:
1108 // struct Foo { Option<Option<Foo>> }
1110 for &seen_type in iter {
1111 if same_type(ty, seen_type) {
1112 debug!("ContainsRecursive: {:?} contains {:?}",
1115 return Representability::ContainsRecursive;
1120 // For structs and enums, track all previously seen types by pushing them
1121 // onto the 'seen' stack.
1123 let out = are_inner_types_recursive(tcx, sp, seen, ty);
1128 // No need to push in other cases.
1129 are_inner_types_recursive(tcx, sp, seen, ty)
1134 debug!("is_type_representable: {:?}", self);
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();
1140 let r = is_type_structurally_recursive(tcx, sp, &mut seen, self);
1141 debug!("is_type_representable: {:?} is {:?}", self, r);