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
14 use middle::const_eval::{self, ConstVal, ErrKind};
15 use middle::const_eval::EvalHint::UncheckedExprHint;
16 use middle::def_id::DefId;
17 use middle::subst::{self, Subst, Substs};
21 use middle::ty::{self, Ty, TypeAndMut, TypeFlags};
22 use middle::ty::{Disr, ParameterEnvironment};
23 use middle::ty::{HasTypeFlags, RegionEscape};
24 use middle::ty::TypeVariants::*;
25 use util::num::ToPrimitive;
28 use std::hash::{Hash, SipHasher, Hasher};
30 use syntax::ast::{self, Name};
31 use syntax::attr::{self, AttrMetaMethods, SignedInt, UnsignedInt};
32 use syntax::codemap::Span;
36 pub trait IntTypeExt {
37 fn to_ty<'tcx>(&self, cx: &ty::ctxt<'tcx>) -> Ty<'tcx>;
38 fn i64_to_disr(&self, val: i64) -> Option<Disr>;
39 fn u64_to_disr(&self, val: u64) -> Option<Disr>;
40 fn disr_incr(&self, val: Disr) -> Option<Disr>;
41 fn disr_string(&self, val: Disr) -> String;
42 fn disr_wrap_incr(&self, val: Option<Disr>) -> Disr;
45 impl IntTypeExt for attr::IntType {
46 fn to_ty<'tcx>(&self, cx: &ty::ctxt<'tcx>) -> Ty<'tcx> {
48 SignedInt(ast::TyI8) => cx.types.i8,
49 SignedInt(ast::TyI16) => cx.types.i16,
50 SignedInt(ast::TyI32) => cx.types.i32,
51 SignedInt(ast::TyI64) => cx.types.i64,
52 SignedInt(ast::TyIs) => cx.types.isize,
53 UnsignedInt(ast::TyU8) => cx.types.u8,
54 UnsignedInt(ast::TyU16) => cx.types.u16,
55 UnsignedInt(ast::TyU32) => cx.types.u32,
56 UnsignedInt(ast::TyU64) => cx.types.u64,
57 UnsignedInt(ast::TyUs) => cx.types.usize,
61 fn i64_to_disr(&self, val: i64) -> Option<Disr> {
63 SignedInt(ast::TyI8) => val.to_i8() .map(|v| v as Disr),
64 SignedInt(ast::TyI16) => val.to_i16() .map(|v| v as Disr),
65 SignedInt(ast::TyI32) => val.to_i32() .map(|v| v as Disr),
66 SignedInt(ast::TyI64) => val.to_i64() .map(|v| v as Disr),
67 UnsignedInt(ast::TyU8) => val.to_u8() .map(|v| v as Disr),
68 UnsignedInt(ast::TyU16) => val.to_u16() .map(|v| v as Disr),
69 UnsignedInt(ast::TyU32) => val.to_u32() .map(|v| v as Disr),
70 UnsignedInt(ast::TyU64) => val.to_u64() .map(|v| v as Disr),
72 UnsignedInt(ast::TyUs) |
73 SignedInt(ast::TyIs) => unreachable!(),
77 fn u64_to_disr(&self, val: u64) -> Option<Disr> {
79 SignedInt(ast::TyI8) => val.to_i8() .map(|v| v as Disr),
80 SignedInt(ast::TyI16) => val.to_i16() .map(|v| v as Disr),
81 SignedInt(ast::TyI32) => val.to_i32() .map(|v| v as Disr),
82 SignedInt(ast::TyI64) => val.to_i64() .map(|v| v as Disr),
83 UnsignedInt(ast::TyU8) => val.to_u8() .map(|v| v as Disr),
84 UnsignedInt(ast::TyU16) => val.to_u16() .map(|v| v as Disr),
85 UnsignedInt(ast::TyU32) => val.to_u32() .map(|v| v as Disr),
86 UnsignedInt(ast::TyU64) => val.to_u64() .map(|v| v as Disr),
88 UnsignedInt(ast::TyUs) |
89 SignedInt(ast::TyIs) => unreachable!(),
93 fn disr_incr(&self, val: Disr) -> Option<Disr> {
95 ($e:expr) => { $e.and_then(|v|v.checked_add(1)).map(|v| v as Disr) }
98 // SignedInt repr means we *want* to reinterpret the bits
99 // treating the highest bit of Disr as a sign-bit, so
100 // cast to i64 before range-checking.
101 SignedInt(ast::TyI8) => add1!((val as i64).to_i8()),
102 SignedInt(ast::TyI16) => add1!((val as i64).to_i16()),
103 SignedInt(ast::TyI32) => add1!((val as i64).to_i32()),
104 SignedInt(ast::TyI64) => add1!(Some(val as i64)),
106 UnsignedInt(ast::TyU8) => add1!(val.to_u8()),
107 UnsignedInt(ast::TyU16) => add1!(val.to_u16()),
108 UnsignedInt(ast::TyU32) => add1!(val.to_u32()),
109 UnsignedInt(ast::TyU64) => add1!(Some(val)),
111 UnsignedInt(ast::TyUs) |
112 SignedInt(ast::TyIs) => unreachable!(),
116 // This returns a String because (1.) it is only used for
117 // rendering an error message and (2.) a string can represent the
118 // full range from `i64::MIN` through `u64::MAX`.
119 fn disr_string(&self, val: Disr) -> String {
121 SignedInt(ast::TyI8) => format!("{}", val as i8 ),
122 SignedInt(ast::TyI16) => format!("{}", val as i16),
123 SignedInt(ast::TyI32) => format!("{}", val as i32),
124 SignedInt(ast::TyI64) => format!("{}", val as i64),
125 UnsignedInt(ast::TyU8) => format!("{}", val as u8 ),
126 UnsignedInt(ast::TyU16) => format!("{}", val as u16),
127 UnsignedInt(ast::TyU32) => format!("{}", val as u32),
128 UnsignedInt(ast::TyU64) => format!("{}", val as u64),
130 UnsignedInt(ast::TyUs) |
131 SignedInt(ast::TyIs) => unreachable!(),
135 fn disr_wrap_incr(&self, val: Option<Disr>) -> Disr {
137 ($e:expr) => { ($e).wrapping_add(1) as Disr }
139 let val = val.unwrap_or(ty::INITIAL_DISCRIMINANT_VALUE);
141 SignedInt(ast::TyI8) => add1!(val as i8 ),
142 SignedInt(ast::TyI16) => add1!(val as i16),
143 SignedInt(ast::TyI32) => add1!(val as i32),
144 SignedInt(ast::TyI64) => add1!(val as i64),
145 UnsignedInt(ast::TyU8) => add1!(val as u8 ),
146 UnsignedInt(ast::TyU16) => add1!(val as u16),
147 UnsignedInt(ast::TyU32) => add1!(val as u32),
148 UnsignedInt(ast::TyU64) => add1!(val as u64),
150 UnsignedInt(ast::TyUs) |
151 SignedInt(ast::TyIs) => unreachable!(),
157 #[derive(Copy, Clone)]
158 pub enum CopyImplementationError {
159 InfrigingField(Name),
160 InfrigingVariant(Name),
165 /// Describes whether a type is representable. For types that are not
166 /// representable, 'SelfRecursive' and 'ContainsRecursive' are used to
167 /// distinguish between types that are recursive with themselves and types that
168 /// contain a different recursive type. These cases can therefore be treated
169 /// differently when reporting errors.
171 /// The ordering of the cases is significant. They are sorted so that cmp::max
172 /// will keep the "more erroneous" of two values.
173 #[derive(Copy, Clone, PartialOrd, Ord, Eq, PartialEq, Debug)]
174 pub enum Representability {
180 impl<'a, 'tcx> ParameterEnvironment<'a, 'tcx> {
181 pub fn can_type_implement_copy(&self, self_type: Ty<'tcx>, span: Span)
182 -> Result<(),CopyImplementationError> {
185 // FIXME: (@jroesch) float this code up
186 let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, Some(self.clone()), false);
188 let adt = match self_type.sty {
189 ty::TyStruct(struct_def, substs) => {
190 for field in struct_def.all_fields() {
191 let field_ty = field.ty(tcx, substs);
192 if infcx.type_moves_by_default(field_ty, span) {
193 return Err(CopyImplementationError::InfrigingField(
199 ty::TyEnum(enum_def, substs) => {
200 for variant in &enum_def.variants {
201 for field in &variant.fields {
202 let field_ty = field.ty(tcx, substs);
203 if infcx.type_moves_by_default(field_ty, span) {
204 return Err(CopyImplementationError::InfrigingVariant(
211 _ => return Err(CopyImplementationError::NotAnAdt),
215 return Err(CopyImplementationError::HasDestructor)
222 impl<'tcx> ty::ctxt<'tcx> {
223 pub fn pat_contains_ref_binding(&self, pat: &hir::Pat) -> Option<hir::Mutability> {
224 pat_util::pat_contains_ref_binding(&self.def_map, pat)
227 pub fn arm_contains_ref_binding(&self, arm: &hir::Arm) -> Option<hir::Mutability> {
228 pat_util::arm_contains_ref_binding(&self.def_map, arm)
231 /// Returns the type of element at index `i` in tuple or tuple-like type `t`.
232 /// For an enum `t`, `variant` is None only if `t` is a univariant enum.
233 pub fn positional_element_ty(&self,
236 variant: Option<DefId>) -> Option<Ty<'tcx>> {
237 match (&ty.sty, variant) {
238 (&TyStruct(def, substs), None) => {
239 def.struct_variant().fields.get(i).map(|f| f.ty(self, substs))
241 (&TyEnum(def, substs), Some(vid)) => {
242 def.variant_with_id(vid).fields.get(i).map(|f| f.ty(self, substs))
244 (&TyEnum(def, substs), None) => {
245 assert!(def.is_univariant());
246 def.variants[0].fields.get(i).map(|f| f.ty(self, substs))
248 (&TyTuple(ref v), None) => v.get(i).cloned(),
253 /// Returns the type of element at field `n` in struct or struct-like type `t`.
254 /// For an enum `t`, `variant` must be some def id.
255 pub fn named_element_ty(&self,
258 variant: Option<DefId>) -> Option<Ty<'tcx>> {
259 match (&ty.sty, variant) {
260 (&TyStruct(def, substs), None) => {
261 def.struct_variant().find_field_named(n).map(|f| f.ty(self, substs))
263 (&TyEnum(def, substs), Some(vid)) => {
264 def.variant_with_id(vid).find_field_named(n).map(|f| f.ty(self, substs))
270 /// Returns `(normalized_type, ty)`, where `normalized_type` is the
271 /// IntType representation of one of {i64,i32,i16,i8,u64,u32,u16,u8},
272 /// and `ty` is the original type (i.e. may include `isize` or
274 pub fn enum_repr_type(&self, opt_hint: Option<&attr::ReprAttr>)
275 -> (attr::IntType, Ty<'tcx>) {
276 let repr_type = match opt_hint {
277 // Feed in the given type
278 Some(&attr::ReprInt(_, int_t)) => int_t,
279 // ... but provide sensible default if none provided
281 // NB. Historically `fn enum_variants` generate i64 here, while
282 // rustc_typeck::check would generate isize.
283 _ => SignedInt(ast::TyIs),
286 let repr_type_ty = repr_type.to_ty(self);
287 let repr_type = match repr_type {
288 SignedInt(ast::TyIs) =>
289 SignedInt(self.sess.target.int_type),
290 UnsignedInt(ast::TyUs) =>
291 UnsignedInt(self.sess.target.uint_type),
295 (repr_type, repr_type_ty)
298 /// Returns the deeply last field of nested structures, or the same type,
299 /// if not a structure at all. Corresponds to the only possible unsized
300 /// field, and its type can be used to determine unsizing strategy.
301 pub fn struct_tail(&self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
302 while let TyStruct(def, substs) = ty.sty {
303 match def.struct_variant().fields.last() {
304 Some(f) => ty = f.ty(self, substs),
311 /// Same as applying struct_tail on `source` and `target`, but only
312 /// keeps going as long as the two types are instances of the same
313 /// structure definitions.
314 /// For `(Foo<Foo<T>>, Foo<Trait>)`, the result will be `(Foo<T>, Trait)`,
315 /// whereas struct_tail produces `T`, and `Trait`, respectively.
316 pub fn struct_lockstep_tails(&self,
319 -> (Ty<'tcx>, Ty<'tcx>) {
320 let (mut a, mut b) = (source, target);
321 while let (&TyStruct(a_def, a_substs), &TyStruct(b_def, b_substs)) = (&a.sty, &b.sty) {
325 if let Some(f) = a_def.struct_variant().fields.last() {
326 a = f.ty(self, a_substs);
327 b = f.ty(self, b_substs);
335 /// Returns the repeat count for a repeating vector expression.
336 pub fn eval_repeat_count(&self, count_expr: &hir::Expr) -> usize {
337 let hint = UncheckedExprHint(self.types.usize);
338 match const_eval::eval_const_expr_partial(self, count_expr, hint, None) {
340 let found = match val {
341 ConstVal::Uint(count) => return count as usize,
342 ConstVal::Int(count) if count >= 0 => return count as usize,
343 const_val => const_val.description(),
345 span_err!(self.sess, count_expr.span, E0306,
346 "expected positive integer for repeat count, found {}",
350 let err_msg = match count_expr.node {
351 hir::ExprPath(None, hir::Path {
355 }) if segments.len() == 1 =>
356 format!("found variable"),
357 _ => match err.kind {
358 ErrKind::MiscCatchAll => format!("but found {}", err.description()),
359 _ => format!("but {}", err.description())
362 span_err!(self.sess, count_expr.span, E0307,
363 "expected constant integer for repeat count, {}", err_msg);
369 /// Given a set of predicates that apply to an object type, returns
370 /// the region bounds that the (erased) `Self` type must
371 /// outlive. Precisely *because* the `Self` type is erased, the
372 /// parameter `erased_self_ty` must be supplied to indicate what type
373 /// has been used to represent `Self` in the predicates
374 /// themselves. This should really be a unique type; `FreshTy(0)` is a
377 /// NB: in some cases, particularly around higher-ranked bounds,
378 /// this function returns a kind of conservative approximation.
379 /// That is, all regions returned by this function are definitely
380 /// required, but there may be other region bounds that are not
381 /// returned, as well as requirements like `for<'a> T: 'a`.
383 /// Requires that trait definitions have been processed so that we can
384 /// elaborate predicates and walk supertraits.
385 pub fn required_region_bounds(&self,
386 erased_self_ty: Ty<'tcx>,
387 predicates: Vec<ty::Predicate<'tcx>>)
389 debug!("required_region_bounds(erased_self_ty={:?}, predicates={:?})",
393 assert!(!erased_self_ty.has_escaping_regions());
395 traits::elaborate_predicates(self, predicates)
396 .filter_map(|predicate| {
398 ty::Predicate::Projection(..) |
399 ty::Predicate::Trait(..) |
400 ty::Predicate::Equate(..) |
401 ty::Predicate::WellFormed(..) |
402 ty::Predicate::ObjectSafe(..) |
403 ty::Predicate::RegionOutlives(..) => {
406 ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(t, r))) => {
407 // Search for a bound of the form `erased_self_ty
408 // : 'a`, but be wary of something like `for<'a>
409 // erased_self_ty : 'a` (we interpret a
410 // higher-ranked bound like that as 'static,
411 // though at present the code in `fulfill.rs`
412 // considers such bounds to be unsatisfiable, so
413 // it's kind of a moot point since you could never
414 // construct such an object, but this seems
415 // correct even if that code changes).
416 if t == erased_self_ty && !r.has_escaping_regions() {
427 /// Creates a hash of the type `Ty` which will be the same no matter what crate
428 /// context it's calculated within. This is used by the `type_id` intrinsic.
429 pub fn hash_crate_independent(&self, ty: Ty<'tcx>, svh: &Svh) -> u64 {
430 let mut state = SipHasher::new();
431 helper(self, ty, svh, &mut state);
432 return state.finish();
434 fn helper<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>, svh: &Svh,
435 state: &mut SipHasher) {
436 macro_rules! byte { ($b:expr) => { ($b as u8).hash(state) } }
437 macro_rules! hash { ($e:expr) => { $e.hash(state) } }
439 let region = |state: &mut SipHasher, r: ty::Region| {
442 ty::ReLateBound(db, ty::BrAnon(i)) => {
447 ty::ReEarlyBound(..) |
448 ty::ReLateBound(..) |
452 ty::ReSkolemized(..) => {
453 tcx.sess.bug("unexpected region found when hashing a type")
457 let did = |state: &mut SipHasher, did: DefId| {
458 let h = if did.is_local() {
461 tcx.sess.cstore.crate_hash(did.krate)
463 h.as_str().hash(state);
464 did.index.hash(state);
466 let mt = |state: &mut SipHasher, mt: TypeAndMut| {
467 mt.mutbl.hash(state);
469 let fn_sig = |state: &mut SipHasher, sig: &ty::Binder<ty::FnSig<'tcx>>| {
470 let sig = tcx.anonymize_late_bound_regions(sig).0;
471 for a in &sig.inputs { helper(tcx, *a, svh, state); }
472 if let ty::FnConverging(output) = sig.output {
473 helper(tcx, output, svh, state);
518 TyBareFn(opt_def_id, ref b) => {
523 fn_sig(state, &b.sig);
526 TyTrait(ref data) => {
528 did(state, data.principal_def_id());
531 let principal = tcx.anonymize_late_bound_regions(&data.principal).0;
532 for subty in &principal.substs.types {
533 helper(tcx, subty, svh, state);
542 TyTuple(ref inner) => {
550 hash!(p.name.as_str());
552 TyInfer(_) => unreachable!(),
553 TyError => byte!(21),
558 TyProjection(ref data) => {
560 did(state, data.trait_ref.def_id);
561 hash!(data.item_name.as_str());
569 /// Returns true if this ADT is a dtorck type.
571 /// Invoking the destructor of a dtorck type during usual cleanup
572 /// (e.g. the glue emitted for stack unwinding) requires all
573 /// lifetimes in the type-structure of `adt` to strictly outlive
574 /// the adt value itself.
576 /// If `adt` is not dtorck, then the adt's destructor can be
577 /// invoked even when there are lifetimes in the type-structure of
578 /// `adt` that do not strictly outlive the adt value itself.
579 /// (This allows programs to make cyclic structures without
580 /// resorting to unasfe means; see RFCs 769 and 1238).
581 pub fn is_adt_dtorck(&self, adt: ty::AdtDef<'tcx>) -> bool {
582 let dtor_method = match adt.destructor() {
587 // RFC 1238: if the destructor method is tagged with the
588 // attribute `unsafe_destructor_blind_to_params`, then the
589 // compiler is being instructed to *assume* that the
590 // destructor will not access borrowed data,
591 // even if such data is otherwise reachable.
593 // Such access can be in plain sight (e.g. dereferencing
594 // `*foo.0` of `Foo<'a>(&'a u32)`) or indirectly hidden
595 // (e.g. calling `foo.0.clone()` of `Foo<T:Clone>`).
596 return !self.has_attr(dtor_method, "unsafe_destructor_blind_to_params");
601 pub struct ImplMethod<'tcx> {
602 pub method: Rc<ty::Method<'tcx>>,
603 pub substs: Substs<'tcx>,
604 pub is_provided: bool
607 impl<'tcx> ty::ctxt<'tcx> {
608 #[inline(never)] // is this perfy enough?
609 pub fn get_impl_method(&self,
611 substs: Substs<'tcx>,
615 // there don't seem to be nicer accessors to these:
616 let impl_or_trait_items_map = self.impl_or_trait_items.borrow();
618 for impl_item in &self.impl_items.borrow()[&impl_def_id] {
619 if let ty::MethodTraitItem(ref meth) =
620 impl_or_trait_items_map[&impl_item.def_id()] {
621 if meth.name == name {
623 method: meth.clone(),
631 // It is not in the impl - get the default from the trait.
632 let trait_ref = self.impl_trait_ref(impl_def_id).unwrap();
633 for trait_item in self.trait_items(trait_ref.def_id).iter() {
634 if let &ty::MethodTraitItem(ref meth) = trait_item {
635 if meth.name == name {
636 let impl_to_trait_substs = self
637 .make_substs_for_receiver_types(&trait_ref, meth);
639 method: meth.clone(),
640 substs: impl_to_trait_substs.subst(self, &substs),
647 self.sess.bug(&format!("method {:?} not found in {:?}",
652 impl<'tcx> ty::TyS<'tcx> {
653 fn impls_bound<'a>(&'tcx self, param_env: &ParameterEnvironment<'a,'tcx>,
654 bound: ty::BuiltinBound,
658 let tcx = param_env.tcx;
659 let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, Some(param_env.clone()), false);
661 let is_impld = traits::type_known_to_meet_builtin_bound(&infcx,
664 debug!("Ty::impls_bound({:?}, {:?}) = {:?}",
665 self, bound, is_impld);
670 // FIXME (@jroesch): I made this public to use it, not sure if should be private
671 pub fn moves_by_default<'a>(&'tcx self, param_env: &ParameterEnvironment<'a,'tcx>,
672 span: Span) -> bool {
673 if self.flags.get().intersects(TypeFlags::MOVENESS_CACHED) {
674 return self.flags.get().intersects(TypeFlags::MOVES_BY_DEFAULT);
677 assert!(!self.needs_infer());
679 // Fast-path for primitive types
680 let result = match self.sty {
681 TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
682 TyRawPtr(..) | TyBareFn(..) | TyRef(_, TypeAndMut {
683 mutbl: hir::MutImmutable, ..
686 TyStr | TyBox(..) | TyRef(_, TypeAndMut {
687 mutbl: hir::MutMutable, ..
690 TyArray(..) | TySlice(_) | TyTrait(..) | TyTuple(..) |
691 TyClosure(..) | TyEnum(..) | TyStruct(..) |
692 TyProjection(..) | TyParam(..) | TyInfer(..) | TyError => None
693 }.unwrap_or_else(|| !self.impls_bound(param_env, ty::BoundCopy, span));
695 if !self.has_param_types() && !self.has_self_ty() {
696 self.flags.set(self.flags.get() | if result {
697 TypeFlags::MOVENESS_CACHED | TypeFlags::MOVES_BY_DEFAULT
699 TypeFlags::MOVENESS_CACHED
707 pub fn is_sized<'a>(&'tcx self, param_env: &ParameterEnvironment<'a,'tcx>,
710 if self.flags.get().intersects(TypeFlags::SIZEDNESS_CACHED) {
711 return self.flags.get().intersects(TypeFlags::IS_SIZED);
714 self.is_sized_uncached(param_env, span)
717 fn is_sized_uncached<'a>(&'tcx self, param_env: &ParameterEnvironment<'a,'tcx>,
718 span: Span) -> bool {
719 assert!(!self.needs_infer());
721 // Fast-path for primitive types
722 let result = match self.sty {
723 TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
724 TyBox(..) | TyRawPtr(..) | TyRef(..) | TyBareFn(..) |
725 TyArray(..) | TyTuple(..) | TyClosure(..) => Some(true),
727 TyStr | TyTrait(..) | TySlice(_) => Some(false),
729 TyEnum(..) | TyStruct(..) | TyProjection(..) | TyParam(..) |
730 TyInfer(..) | TyError => None
731 }.unwrap_or_else(|| self.impls_bound(param_env, ty::BoundSized, span));
733 if !self.has_param_types() && !self.has_self_ty() {
734 self.flags.set(self.flags.get() | if result {
735 TypeFlags::SIZEDNESS_CACHED | TypeFlags::IS_SIZED
737 TypeFlags::SIZEDNESS_CACHED
745 /// Check whether a type is representable. This means it cannot contain unboxed
746 /// structural recursion. This check is needed for structs and enums.
747 pub fn is_representable(&'tcx self, cx: &ty::ctxt<'tcx>, sp: Span) -> Representability {
749 // Iterate until something non-representable is found
750 fn find_nonrepresentable<'tcx, It: Iterator<Item=Ty<'tcx>>>(cx: &ty::ctxt<'tcx>,
752 seen: &mut Vec<Ty<'tcx>>,
754 -> Representability {
755 iter.fold(Representability::Representable,
756 |r, ty| cmp::max(r, is_type_structurally_recursive(cx, sp, seen, ty)))
759 fn are_inner_types_recursive<'tcx>(cx: &ty::ctxt<'tcx>, sp: Span,
760 seen: &mut Vec<Ty<'tcx>>, ty: Ty<'tcx>)
761 -> Representability {
764 find_nonrepresentable(cx, sp, seen, ts.iter().cloned())
766 // Fixed-length vectors.
767 // FIXME(#11924) Behavior undecided for zero-length vectors.
769 is_type_structurally_recursive(cx, sp, seen, ty)
771 TyStruct(def, substs) | TyEnum(def, substs) => {
772 find_nonrepresentable(cx,
775 def.all_fields().map(|f| f.ty(cx, substs)))
778 // this check is run on type definitions, so we don't expect
779 // to see closure types
780 cx.sess.bug(&format!("requires check invoked on inapplicable type: {:?}", ty))
782 _ => Representability::Representable,
786 fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: ty::AdtDef<'tcx>) -> bool {
788 TyStruct(ty_def, _) | TyEnum(ty_def, _) => {
795 fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
796 match (&a.sty, &b.sty) {
797 (&TyStruct(did_a, ref substs_a), &TyStruct(did_b, ref substs_b)) |
798 (&TyEnum(did_a, ref substs_a), &TyEnum(did_b, ref substs_b)) => {
803 let types_a = substs_a.types.get_slice(subst::TypeSpace);
804 let types_b = substs_b.types.get_slice(subst::TypeSpace);
806 let mut pairs = types_a.iter().zip(types_b);
808 pairs.all(|(&a, &b)| same_type(a, b))
816 // Does the type `ty` directly (without indirection through a pointer)
817 // contain any types on stack `seen`?
818 fn is_type_structurally_recursive<'tcx>(cx: &ty::ctxt<'tcx>,
820 seen: &mut Vec<Ty<'tcx>>,
821 ty: Ty<'tcx>) -> Representability {
822 debug!("is_type_structurally_recursive: {:?}", ty);
825 TyStruct(def, _) | TyEnum(def, _) => {
827 // Iterate through stack of previously seen types.
828 let mut iter = seen.iter();
830 // The first item in `seen` is the type we are actually curious about.
831 // We want to return SelfRecursive if this type contains itself.
832 // It is important that we DON'T take generic parameters into account
833 // for this check, so that Bar<T> in this example counts as SelfRecursive:
836 // struct Bar<T> { x: Bar<Foo> }
839 Some(&seen_type) => {
840 if same_struct_or_enum(seen_type, def) {
841 debug!("SelfRecursive: {:?} contains {:?}",
844 return Representability::SelfRecursive;
850 // We also need to know whether the first item contains other types
851 // that are structurally recursive. If we don't catch this case, we
852 // will recurse infinitely for some inputs.
854 // It is important that we DO take generic parameters into account
855 // here, so that code like this is considered SelfRecursive, not
856 // ContainsRecursive:
858 // struct Foo { Option<Option<Foo>> }
860 for &seen_type in iter {
861 if same_type(ty, seen_type) {
862 debug!("ContainsRecursive: {:?} contains {:?}",
865 return Representability::ContainsRecursive;
870 // For structs and enums, track all previously seen types by pushing them
871 // onto the 'seen' stack.
873 let out = are_inner_types_recursive(cx, sp, seen, ty);
878 // No need to push in other cases.
879 are_inner_types_recursive(cx, sp, seen, ty)
884 debug!("is_type_representable: {:?}", self);
886 // To avoid a stack overflow when checking an enum variant or struct that
887 // contains a different, structurally recursive type, maintain a stack
888 // of seen types and check recursion for each of them (issues #3008, #3779).
889 let mut seen: Vec<Ty> = Vec::new();
890 let r = is_type_structurally_recursive(cx, sp, &mut seen, self);
891 debug!("is_type_representable: {:?} is {:?}", self, r);