2015-09-14 11:55:56 +00:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! misc. type-system utilities too small to deserve their own file
|
|
|
|
|
2016-03-29 09:54:26 +00:00
|
|
|
use hir::def_id::DefId;
|
2016-03-11 00:31:38 +00:00
|
|
|
use infer::InferCtxt;
|
2016-03-29 09:54:26 +00:00
|
|
|
use hir::pat_util;
|
2016-06-30 18:22:47 +00:00
|
|
|
use traits::{self, Reveal};
|
2016-09-05 22:26:02 +00:00
|
|
|
use ty::{self, Ty, AdtKind, TyCtxt, TypeAndMut, TypeFlags, TypeFoldable};
|
2016-03-22 15:30:57 +00:00
|
|
|
use ty::{Disr, ParameterEnvironment};
|
2016-08-05 21:50:13 +00:00
|
|
|
use ty::fold::TypeVisitor;
|
2016-04-19 06:11:46 +00:00
|
|
|
use ty::layout::{Layout, LayoutError};
|
2016-03-22 15:30:57 +00:00
|
|
|
use ty::TypeVariants::*;
|
2015-12-16 17:44:15 +00:00
|
|
|
|
2016-03-15 11:33:13 +00:00
|
|
|
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
use std::cmp;
|
|
|
|
use std::hash::{Hash, SipHasher, Hasher};
|
2016-08-05 21:50:13 +00:00
|
|
|
use std::intrinsics;
|
2015-09-14 09:58:20 +00:00
|
|
|
use syntax::ast::{self, Name};
|
2016-04-12 13:58:55 +00:00
|
|
|
use syntax::attr::{self, SignedInt, UnsignedInt};
|
2016-06-21 22:08:13 +00:00
|
|
|
use syntax_pos::Span;
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2016-03-29 05:50:44 +00:00
|
|
|
use hir;
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
pub trait IntTypeExt {
|
2016-05-03 02:23:22 +00:00
|
|
|
fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx>;
|
|
|
|
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
|
|
|
|
-> Option<Disr>;
|
2015-12-16 17:44:15 +00:00
|
|
|
fn assert_ty_matches(&self, val: Disr);
|
2016-05-03 02:23:22 +00:00
|
|
|
fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr;
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntTypeExt for attr::IntType {
|
2016-05-03 02:23:22 +00:00
|
|
|
fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
|
2015-09-14 11:55:56 +00:00
|
|
|
match *self {
|
2016-05-03 01:02:41 +00:00
|
|
|
SignedInt(ast::IntTy::I8) => tcx.types.i8,
|
|
|
|
SignedInt(ast::IntTy::I16) => tcx.types.i16,
|
|
|
|
SignedInt(ast::IntTy::I32) => tcx.types.i32,
|
|
|
|
SignedInt(ast::IntTy::I64) => tcx.types.i64,
|
|
|
|
SignedInt(ast::IntTy::Is) => tcx.types.isize,
|
|
|
|
UnsignedInt(ast::UintTy::U8) => tcx.types.u8,
|
|
|
|
UnsignedInt(ast::UintTy::U16) => tcx.types.u16,
|
|
|
|
UnsignedInt(ast::UintTy::U32) => tcx.types.u32,
|
|
|
|
UnsignedInt(ast::UintTy::U64) => tcx.types.u64,
|
|
|
|
UnsignedInt(ast::UintTy::Us) => tcx.types.usize,
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:23:22 +00:00
|
|
|
fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr {
|
2015-09-14 11:55:56 +00:00
|
|
|
match *self {
|
2015-12-16 17:44:15 +00:00
|
|
|
SignedInt(ast::IntTy::I8) => ConstInt::I8(0),
|
|
|
|
SignedInt(ast::IntTy::I16) => ConstInt::I16(0),
|
|
|
|
SignedInt(ast::IntTy::I32) => ConstInt::I32(0),
|
|
|
|
SignedInt(ast::IntTy::I64) => ConstInt::I64(0),
|
|
|
|
SignedInt(ast::IntTy::Is) => match tcx.sess.target.int_type {
|
2016-05-06 13:31:11 +00:00
|
|
|
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16(0)),
|
2015-12-16 17:44:15 +00:00
|
|
|
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32(0)),
|
|
|
|
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64(0)),
|
2016-03-25 17:46:11 +00:00
|
|
|
_ => bug!(),
|
2015-12-16 17:44:15 +00:00
|
|
|
},
|
|
|
|
UnsignedInt(ast::UintTy::U8) => ConstInt::U8(0),
|
|
|
|
UnsignedInt(ast::UintTy::U16) => ConstInt::U16(0),
|
|
|
|
UnsignedInt(ast::UintTy::U32) => ConstInt::U32(0),
|
|
|
|
UnsignedInt(ast::UintTy::U64) => ConstInt::U64(0),
|
|
|
|
UnsignedInt(ast::UintTy::Us) => match tcx.sess.target.uint_type {
|
2016-05-06 13:31:11 +00:00
|
|
|
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(0)),
|
2015-12-16 17:44:15 +00:00
|
|
|
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(0)),
|
|
|
|
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(0)),
|
2016-03-25 17:46:11 +00:00
|
|
|
_ => bug!(),
|
2015-12-16 17:44:15 +00:00
|
|
|
},
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:44:15 +00:00
|
|
|
fn assert_ty_matches(&self, val: Disr) {
|
|
|
|
match (*self, val) {
|
|
|
|
(SignedInt(ast::IntTy::I8), ConstInt::I8(_)) => {},
|
|
|
|
(SignedInt(ast::IntTy::I16), ConstInt::I16(_)) => {},
|
|
|
|
(SignedInt(ast::IntTy::I32), ConstInt::I32(_)) => {},
|
|
|
|
(SignedInt(ast::IntTy::I64), ConstInt::I64(_)) => {},
|
|
|
|
(SignedInt(ast::IntTy::Is), ConstInt::Isize(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::U8), ConstInt::U8(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::U16), ConstInt::U16(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::U32), ConstInt::U32(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::U64), ConstInt::U64(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::Us), ConstInt::Usize(_)) => {},
|
2016-03-26 18:59:04 +00:00
|
|
|
_ => bug!("disr type mismatch: {:?} vs {:?}", self, val),
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:23:22 +00:00
|
|
|
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
|
|
|
|
-> Option<Disr> {
|
2016-03-11 14:33:42 +00:00
|
|
|
if let Some(val) = val {
|
|
|
|
self.assert_ty_matches(val);
|
|
|
|
(val + ConstInt::Infer(1)).ok()
|
|
|
|
} else {
|
|
|
|
Some(self.initial_discriminant(tcx))
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum CopyImplementationError {
|
|
|
|
InfrigingField(Name),
|
|
|
|
InfrigingVariant(Name),
|
|
|
|
NotAnAdt,
|
|
|
|
HasDestructor
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Describes whether a type is representable. For types that are not
|
|
|
|
/// representable, 'SelfRecursive' and 'ContainsRecursive' are used to
|
|
|
|
/// distinguish between types that are recursive with themselves and types that
|
|
|
|
/// contain a different recursive type. These cases can therefore be treated
|
|
|
|
/// differently when reporting errors.
|
|
|
|
///
|
|
|
|
/// The ordering of the cases is significant. They are sorted so that cmp::max
|
|
|
|
/// will keep the "more erroneous" of two values.
|
|
|
|
#[derive(Copy, Clone, PartialOrd, Ord, Eq, PartialEq, Debug)]
|
|
|
|
pub enum Representability {
|
|
|
|
Representable,
|
|
|
|
ContainsRecursive,
|
|
|
|
SelfRecursive,
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:22:52 +00:00
|
|
|
impl<'tcx> ParameterEnvironment<'tcx> {
|
|
|
|
pub fn can_type_implement_copy<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
self_type: Ty<'tcx>, span: Span)
|
|
|
|
-> Result<(),CopyImplementationError> {
|
2015-09-14 11:55:56 +00:00
|
|
|
// FIXME: (@jroesch) float this code up
|
2016-06-30 18:22:47 +00:00
|
|
|
tcx.infer_ctxt(None, Some(self.clone()), Reveal::ExactMatch).enter(|infcx| {
|
2016-05-11 01:14:41 +00:00
|
|
|
let adt = match self_type.sty {
|
2016-09-05 22:26:02 +00:00
|
|
|
ty::TyAdt(adt, substs) => match adt.adt_kind() {
|
|
|
|
AdtKind::Struct | AdtKind::Union => {
|
|
|
|
for field in adt.all_fields() {
|
2016-03-25 03:22:44 +00:00
|
|
|
let field_ty = field.ty(tcx, substs);
|
|
|
|
if infcx.type_moves_by_default(field_ty, span) {
|
2016-09-05 22:26:02 +00:00
|
|
|
return Err(CopyImplementationError::InfrigingField(
|
|
|
|
field.name))
|
2016-03-25 03:22:44 +00:00
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-09-05 22:26:02 +00:00
|
|
|
adt
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-09-05 22:26:02 +00:00
|
|
|
AdtKind::Enum => {
|
|
|
|
for variant in &adt.variants {
|
|
|
|
for field in &variant.fields {
|
|
|
|
let field_ty = field.ty(tcx, substs);
|
|
|
|
if infcx.type_moves_by_default(field_ty, span) {
|
|
|
|
return Err(CopyImplementationError::InfrigingVariant(
|
|
|
|
variant.name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adt
|
|
|
|
}
|
|
|
|
},
|
2016-05-11 01:14:41 +00:00
|
|
|
_ => return Err(CopyImplementationError::NotAnAdt)
|
|
|
|
};
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2016-05-11 01:14:41 +00:00
|
|
|
if adt.has_dtor() {
|
|
|
|
return Err(CopyImplementationError::HasDestructor);
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2016-05-11 01:14:41 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn pat_contains_ref_binding(self, pat: &hir::Pat) -> Option<hir::Mutability> {
|
2016-03-06 12:54:44 +00:00
|
|
|
pat_util::pat_contains_ref_binding(pat)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn arm_contains_ref_binding(self, arm: &hir::Arm) -> Option<hir::Mutability> {
|
2016-03-06 12:54:44 +00:00
|
|
|
pat_util::arm_contains_ref_binding(arm)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:58:30 +00:00
|
|
|
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
|
|
|
|
match ty.sty {
|
2016-09-05 22:26:02 +00:00
|
|
|
ty::TyAdt(def, substs) => {
|
2016-07-30 15:58:30 +00:00
|
|
|
for field in def.all_fields() {
|
|
|
|
let field_ty = field.ty(self, substs);
|
|
|
|
if let TyError = field_ty.sty {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2015-09-14 11:55:56 +00:00
|
|
|
/// Returns the type of element at index `i` in tuple or tuple-like type `t`.
|
|
|
|
/// For an enum `t`, `variant` is None only if `t` is a univariant enum.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn positional_element_ty(self,
|
2015-09-14 11:55:56 +00:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
i: usize,
|
|
|
|
variant: Option<DefId>) -> Option<Ty<'tcx>> {
|
|
|
|
match (&ty.sty, variant) {
|
2016-09-05 22:26:02 +00:00
|
|
|
(&TyAdt(adt, substs), Some(vid)) => {
|
|
|
|
adt.variant_with_id(vid).fields.get(i).map(|f| f.ty(self, substs))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-09-05 22:26:02 +00:00
|
|
|
(&TyAdt(adt, substs), None) => {
|
|
|
|
// Don't use `struct_variant`, this may be a univariant enum.
|
|
|
|
adt.variants[0].fields.get(i).map(|f| f.ty(self, substs))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
(&TyTuple(ref v), None) => v.get(i).cloned(),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type of element at field `n` in struct or struct-like type `t`.
|
|
|
|
/// For an enum `t`, `variant` must be some def id.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn named_element_ty(self,
|
2015-09-14 11:55:56 +00:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
n: Name,
|
|
|
|
variant: Option<DefId>) -> Option<Ty<'tcx>> {
|
|
|
|
match (&ty.sty, variant) {
|
2016-09-05 22:26:02 +00:00
|
|
|
(&TyAdt(adt, substs), Some(vid)) => {
|
|
|
|
adt.variant_with_id(vid).find_field_named(n).map(|f| f.ty(self, substs))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-09-05 22:26:02 +00:00
|
|
|
(&TyAdt(adt, substs), None) => {
|
|
|
|
adt.struct_variant().find_field_named(n).map(|f| f.ty(self, substs))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
_ => return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:44:15 +00:00
|
|
|
/// Returns the IntType representation.
|
|
|
|
/// This used to ensure `int_ty` doesn't contain `usize` and `isize`
|
|
|
|
/// by converting them to their actual types. That doesn't happen anymore.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn enum_repr_type(self, opt_hint: Option<&attr::ReprAttr>) -> attr::IntType {
|
2015-12-16 17:44:15 +00:00
|
|
|
match opt_hint {
|
2015-09-14 11:55:56 +00:00
|
|
|
// Feed in the given type
|
|
|
|
Some(&attr::ReprInt(_, int_t)) => int_t,
|
|
|
|
// ... but provide sensible default if none provided
|
|
|
|
//
|
|
|
|
// NB. Historically `fn enum_variants` generate i64 here, while
|
|
|
|
// rustc_typeck::check would generate isize.
|
2016-02-08 15:20:57 +00:00
|
|
|
_ => SignedInt(ast::IntTy::Is),
|
2015-12-16 17:44:15 +00:00
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the deeply last field of nested structures, or the same type,
|
|
|
|
/// if not a structure at all. Corresponds to the only possible unsized
|
|
|
|
/// field, and its type can be used to determine unsizing strategy.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn struct_tail(self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
|
2016-09-05 22:26:02 +00:00
|
|
|
while let TyAdt(def, substs) = ty.sty {
|
|
|
|
if !def.is_struct() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
match def.struct_variant().fields.last() {
|
|
|
|
Some(f) => ty = f.ty(self, substs),
|
|
|
|
None => break
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as applying struct_tail on `source` and `target`, but only
|
|
|
|
/// keeps going as long as the two types are instances of the same
|
|
|
|
/// structure definitions.
|
|
|
|
/// For `(Foo<Foo<T>>, Foo<Trait>)`, the result will be `(Foo<T>, Trait)`,
|
|
|
|
/// whereas struct_tail produces `T`, and `Trait`, respectively.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn struct_lockstep_tails(self,
|
2015-09-14 11:55:56 +00:00
|
|
|
source: Ty<'tcx>,
|
|
|
|
target: Ty<'tcx>)
|
|
|
|
-> (Ty<'tcx>, Ty<'tcx>) {
|
|
|
|
let (mut a, mut b) = (source, target);
|
2016-09-05 22:26:02 +00:00
|
|
|
while let (&TyAdt(a_def, a_substs), &TyAdt(b_def, b_substs)) = (&a.sty, &b.sty) {
|
|
|
|
if a_def != b_def || !a_def.is_struct() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
match a_def.struct_variant().fields.last() {
|
|
|
|
Some(f) => {
|
|
|
|
a = f.ty(self, a_substs);
|
|
|
|
b = f.ty(self, b_substs);
|
2016-09-05 22:26:02 +00:00
|
|
|
}
|
|
|
|
_ => break
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a set of predicates that apply to an object type, returns
|
|
|
|
/// the region bounds that the (erased) `Self` type must
|
|
|
|
/// outlive. Precisely *because* the `Self` type is erased, the
|
|
|
|
/// parameter `erased_self_ty` must be supplied to indicate what type
|
|
|
|
/// has been used to represent `Self` in the predicates
|
|
|
|
/// themselves. This should really be a unique type; `FreshTy(0)` is a
|
|
|
|
/// popular choice.
|
|
|
|
///
|
|
|
|
/// NB: in some cases, particularly around higher-ranked bounds,
|
|
|
|
/// this function returns a kind of conservative approximation.
|
|
|
|
/// That is, all regions returned by this function are definitely
|
|
|
|
/// required, but there may be other region bounds that are not
|
|
|
|
/// returned, as well as requirements like `for<'a> T: 'a`.
|
|
|
|
///
|
|
|
|
/// Requires that trait definitions have been processed so that we can
|
|
|
|
/// elaborate predicates and walk supertraits.
|
2016-05-03 01:56:42 +00:00
|
|
|
pub fn required_region_bounds(self,
|
2015-09-14 11:55:56 +00:00
|
|
|
erased_self_ty: Ty<'tcx>,
|
|
|
|
predicates: Vec<ty::Predicate<'tcx>>)
|
2016-08-25 20:58:52 +00:00
|
|
|
-> Vec<&'tcx ty::Region> {
|
2015-09-14 11:55:56 +00:00
|
|
|
debug!("required_region_bounds(erased_self_ty={:?}, predicates={:?})",
|
|
|
|
erased_self_ty,
|
|
|
|
predicates);
|
|
|
|
|
|
|
|
assert!(!erased_self_ty.has_escaping_regions());
|
|
|
|
|
|
|
|
traits::elaborate_predicates(self, predicates)
|
|
|
|
.filter_map(|predicate| {
|
|
|
|
match predicate {
|
|
|
|
ty::Predicate::Projection(..) |
|
|
|
|
ty::Predicate::Trait(..) |
|
|
|
|
ty::Predicate::Equate(..) |
|
|
|
|
ty::Predicate::WellFormed(..) |
|
|
|
|
ty::Predicate::ObjectSafe(..) |
|
2016-04-06 07:20:59 +00:00
|
|
|
ty::Predicate::ClosureKind(..) |
|
2015-09-14 11:55:56 +00:00
|
|
|
ty::Predicate::RegionOutlives(..) => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
ty::Predicate::TypeOutlives(ty::Binder(ty::OutlivesPredicate(t, r))) => {
|
|
|
|
// Search for a bound of the form `erased_self_ty
|
|
|
|
// : 'a`, but be wary of something like `for<'a>
|
|
|
|
// erased_self_ty : 'a` (we interpret a
|
|
|
|
// higher-ranked bound like that as 'static,
|
|
|
|
// though at present the code in `fulfill.rs`
|
|
|
|
// considers such bounds to be unsatisfiable, so
|
|
|
|
// it's kind of a moot point since you could never
|
|
|
|
// construct such an object, but this seems
|
|
|
|
// correct even if that code changes).
|
|
|
|
if t == erased_self_ty && !r.has_escaping_regions() {
|
|
|
|
Some(r)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a hash of the type `Ty` which will be the same no matter what crate
|
|
|
|
/// context it's calculated within. This is used by the `type_id` intrinsic.
|
2016-08-05 21:50:13 +00:00
|
|
|
pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 {
|
|
|
|
let mut hasher = TypeIdHasher {
|
|
|
|
tcx: self,
|
|
|
|
state: SipHasher::new()
|
|
|
|
};
|
|
|
|
hasher.visit_ty(ty);
|
|
|
|
hasher.state.finish()
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 16:42:14 +00:00
|
|
|
/// Returns true if this ADT is a dtorck type.
|
|
|
|
///
|
|
|
|
/// Invoking the destructor of a dtorck type during usual cleanup
|
|
|
|
/// (e.g. the glue emitted for stack unwinding) requires all
|
|
|
|
/// lifetimes in the type-structure of `adt` to strictly outlive
|
|
|
|
/// the adt value itself.
|
|
|
|
///
|
|
|
|
/// If `adt` is not dtorck, then the adt's destructor can be
|
|
|
|
/// invoked even when there are lifetimes in the type-structure of
|
|
|
|
/// `adt` that do not strictly outlive the adt value itself.
|
|
|
|
/// (This allows programs to make cyclic structures without
|
|
|
|
/// resorting to unasfe means; see RFCs 769 and 1238).
|
2016-05-02 15:07:47 +00:00
|
|
|
pub fn is_adt_dtorck(self, adt: ty::AdtDef) -> bool {
|
2015-09-14 11:55:56 +00:00
|
|
|
let dtor_method = match adt.destructor() {
|
|
|
|
Some(dtor) => dtor,
|
|
|
|
None => return false
|
|
|
|
};
|
|
|
|
|
2015-07-16 12:56:03 +00:00
|
|
|
// RFC 1238: if the destructor method is tagged with the
|
|
|
|
// attribute `unsafe_destructor_blind_to_params`, then the
|
|
|
|
// compiler is being instructed to *assume* that the
|
2015-10-07 11:17:12 +00:00
|
|
|
// destructor will not access borrowed data,
|
|
|
|
// even if such data is otherwise reachable.
|
2015-09-14 11:55:56 +00:00
|
|
|
//
|
2015-10-07 11:17:12 +00:00
|
|
|
// Such access can be in plain sight (e.g. dereferencing
|
|
|
|
// `*foo.0` of `Foo<'a>(&'a u32)`) or indirectly hidden
|
|
|
|
// (e.g. calling `foo.0.clone()` of `Foo<T:Clone>`).
|
|
|
|
return !self.has_attr(dtor_method, "unsafe_destructor_blind_to_params");
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:50:13 +00:00
|
|
|
struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
state: SipHasher
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> TypeIdHasher<'a, 'gcx, 'tcx> {
|
|
|
|
fn hash<T: Hash>(&mut self, x: T) {
|
|
|
|
x.hash(&mut self.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_discriminant_u8<T>(&mut self, x: &T) {
|
|
|
|
let v = unsafe {
|
|
|
|
intrinsics::discriminant_value(x)
|
|
|
|
};
|
|
|
|
let b = v as u8;
|
|
|
|
assert_eq!(v, b as u64);
|
|
|
|
self.hash(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn def_id(&mut self, did: DefId) {
|
|
|
|
// Hash the crate identification information.
|
|
|
|
let name = self.tcx.crate_name(did.krate);
|
|
|
|
let disambiguator = self.tcx.crate_disambiguator(did.krate);
|
|
|
|
self.hash((name, disambiguator));
|
|
|
|
|
|
|
|
// Hash the item path within that crate.
|
|
|
|
// FIXME(#35379) This should use a deterministic
|
|
|
|
// DefPath hashing mechanism, not the DefIndex.
|
|
|
|
self.hash(did.index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx> {
|
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
|
|
|
|
// Distinguish between the Ty variants uniformly.
|
|
|
|
self.hash_discriminant_u8(&ty.sty);
|
|
|
|
|
|
|
|
match ty.sty {
|
|
|
|
TyInt(i) => self.hash(i),
|
|
|
|
TyUint(u) => self.hash(u),
|
|
|
|
TyFloat(f) => self.hash(f),
|
2016-09-05 22:26:02 +00:00
|
|
|
TyAdt(d, _) => self.def_id(d.did),
|
2016-08-05 21:50:13 +00:00
|
|
|
TyArray(_, n) => self.hash(n),
|
|
|
|
TyRawPtr(m) |
|
|
|
|
TyRef(_, m) => self.hash(m.mutbl),
|
|
|
|
TyClosure(def_id, _) |
|
2016-07-22 15:56:22 +00:00
|
|
|
TyAnon(def_id, _) |
|
2016-08-26 16:23:42 +00:00
|
|
|
TyFnDef(def_id, ..) => self.def_id(def_id),
|
2016-08-05 21:50:13 +00:00
|
|
|
TyFnPtr(f) => {
|
|
|
|
self.hash(f.unsafety);
|
|
|
|
self.hash(f.abi);
|
|
|
|
self.hash(f.sig.variadic());
|
|
|
|
}
|
|
|
|
TyTrait(ref data) => {
|
|
|
|
// Trait objects have a list of projection bounds
|
|
|
|
// that are not guaranteed to be sorted in an order
|
|
|
|
// that gets preserved across crates, so we need
|
|
|
|
// to sort them again by the name, in string form.
|
|
|
|
|
|
|
|
// Hash the whole principal trait ref.
|
2016-08-04 12:52:57 +00:00
|
|
|
self.def_id(data.principal.def_id());
|
2016-08-05 21:50:13 +00:00
|
|
|
data.principal.visit_with(self);
|
|
|
|
|
|
|
|
// Hash region and builtin bounds.
|
2016-08-04 12:52:57 +00:00
|
|
|
data.region_bound.visit_with(self);
|
|
|
|
self.hash(data.builtin_bounds);
|
2016-08-05 21:50:13 +00:00
|
|
|
|
2016-09-13 12:40:14 +00:00
|
|
|
// Only projection bounds are left, hash them.
|
|
|
|
self.hash(data.projection_bounds.len());
|
|
|
|
for bound in &data.projection_bounds {
|
2016-08-04 12:52:57 +00:00
|
|
|
self.def_id(bound.0.trait_ref.def_id);
|
2016-09-13 12:40:14 +00:00
|
|
|
self.hash(bound.0.item_name);
|
2016-08-05 21:50:13 +00:00
|
|
|
bound.visit_with(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass super_visit_with, we've visited everything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TyTuple(tys) => {
|
|
|
|
self.hash(tys.len());
|
|
|
|
}
|
|
|
|
TyParam(p) => {
|
|
|
|
self.hash(p.idx);
|
|
|
|
self.hash(p.name.as_str());
|
|
|
|
}
|
|
|
|
TyProjection(ref data) => {
|
|
|
|
self.def_id(data.trait_ref.def_id);
|
|
|
|
self.hash(data.item_name.as_str());
|
|
|
|
}
|
2016-08-02 07:56:20 +00:00
|
|
|
TyNever |
|
2016-08-05 21:50:13 +00:00
|
|
|
TyBool |
|
|
|
|
TyChar |
|
|
|
|
TyStr |
|
|
|
|
TyBox(_) |
|
|
|
|
TySlice(_) |
|
|
|
|
TyError => {}
|
|
|
|
TyInfer(_) => bug!()
|
|
|
|
}
|
|
|
|
|
|
|
|
ty.super_visit_with(self)
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:58:52 +00:00
|
|
|
fn visit_region(&mut self, r: &'tcx ty::Region) -> bool {
|
|
|
|
match *r {
|
2016-08-05 21:50:13 +00:00
|
|
|
ty::ReStatic | ty::ReErased => {
|
|
|
|
self.hash::<u32>(0);
|
|
|
|
}
|
|
|
|
ty::ReLateBound(db, ty::BrAnon(i)) => {
|
|
|
|
assert!(db.depth > 0);
|
|
|
|
self.hash::<u32>(db.depth);
|
|
|
|
self.hash(i);
|
|
|
|
}
|
|
|
|
ty::ReEmpty |
|
|
|
|
ty::ReEarlyBound(..) |
|
|
|
|
ty::ReLateBound(..) |
|
|
|
|
ty::ReFree(..) |
|
|
|
|
ty::ReScope(..) |
|
|
|
|
ty::ReVar(..) |
|
|
|
|
ty::ReSkolemized(..) => {
|
|
|
|
bug!("unexpected region found when hashing a type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, x: &ty::Binder<T>) -> bool {
|
|
|
|
// Anonymize late-bound regions so that, for example:
|
|
|
|
// `for<'a, b> fn(&'a &'b T)` and `for<'a, b> fn(&'b &'a T)`
|
|
|
|
// result in the same TypeId (the two types are equivalent).
|
|
|
|
self.tcx.anonymize_late_bound_regions(x).super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:56:42 +00:00
|
|
|
impl<'a, 'tcx> ty::TyS<'tcx> {
|
2016-03-25 03:22:52 +00:00
|
|
|
fn impls_bound(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: &ParameterEnvironment<'tcx>,
|
|
|
|
bound: ty::BuiltinBound, span: Span) -> bool
|
2015-09-14 11:55:56 +00:00
|
|
|
{
|
2016-06-30 18:22:47 +00:00
|
|
|
tcx.infer_ctxt(None, Some(param_env.clone()), Reveal::ExactMatch).enter(|infcx| {
|
2016-03-25 03:22:44 +00:00
|
|
|
traits::type_known_to_meet_builtin_bound(&infcx, self, bound, span)
|
|
|
|
})
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME (@jroesch): I made this public to use it, not sure if should be private
|
2016-03-25 03:22:52 +00:00
|
|
|
pub fn moves_by_default(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: &ParameterEnvironment<'tcx>,
|
2016-05-03 01:56:42 +00:00
|
|
|
span: Span) -> bool {
|
2015-09-14 11:55:56 +00:00
|
|
|
if self.flags.get().intersects(TypeFlags::MOVENESS_CACHED) {
|
|
|
|
return self.flags.get().intersects(TypeFlags::MOVES_BY_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(!self.needs_infer());
|
|
|
|
|
|
|
|
// Fast-path for primitive types
|
|
|
|
let result = match self.sty {
|
2016-08-02 07:56:20 +00:00
|
|
|
TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) | TyNever |
|
2015-06-13 20:15:03 +00:00
|
|
|
TyRawPtr(..) | TyFnDef(..) | TyFnPtr(_) | TyRef(_, TypeAndMut {
|
2015-09-14 11:55:56 +00:00
|
|
|
mutbl: hir::MutImmutable, ..
|
|
|
|
}) => Some(false),
|
|
|
|
|
|
|
|
TyStr | TyBox(..) | TyRef(_, TypeAndMut {
|
|
|
|
mutbl: hir::MutMutable, ..
|
|
|
|
}) => Some(true),
|
|
|
|
|
2016-09-05 22:26:02 +00:00
|
|
|
TyArray(..) | TySlice(..) | TyTrait(..) | TyTuple(..) |
|
|
|
|
TyClosure(..) | TyAdt(..) | TyAnon(..) |
|
2015-09-14 11:55:56 +00:00
|
|
|
TyProjection(..) | TyParam(..) | TyInfer(..) | TyError => None
|
2016-03-25 03:22:52 +00:00
|
|
|
}.unwrap_or_else(|| !self.impls_bound(tcx, param_env, ty::BoundCopy, span));
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
if !self.has_param_types() && !self.has_self_ty() {
|
|
|
|
self.flags.set(self.flags.get() | if result {
|
|
|
|
TypeFlags::MOVENESS_CACHED | TypeFlags::MOVES_BY_DEFAULT
|
|
|
|
} else {
|
|
|
|
TypeFlags::MOVENESS_CACHED
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-03-25 03:22:52 +00:00
|
|
|
pub fn is_sized(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: &ParameterEnvironment<'tcx>,
|
2016-05-03 01:56:42 +00:00
|
|
|
span: Span) -> bool
|
2015-09-14 11:55:56 +00:00
|
|
|
{
|
|
|
|
if self.flags.get().intersects(TypeFlags::SIZEDNESS_CACHED) {
|
|
|
|
return self.flags.get().intersects(TypeFlags::IS_SIZED);
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:22:52 +00:00
|
|
|
self.is_sized_uncached(tcx, param_env, span)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 03:22:52 +00:00
|
|
|
fn is_sized_uncached(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: &ParameterEnvironment<'tcx>,
|
2016-05-03 01:56:42 +00:00
|
|
|
span: Span) -> bool {
|
2015-09-14 11:55:56 +00:00
|
|
|
assert!(!self.needs_infer());
|
|
|
|
|
|
|
|
// Fast-path for primitive types
|
|
|
|
let result = match self.sty {
|
|
|
|
TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
|
2015-06-13 20:15:03 +00:00
|
|
|
TyBox(..) | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
|
2016-08-02 07:56:20 +00:00
|
|
|
TyArray(..) | TyTuple(..) | TyClosure(..) | TyNever => Some(true),
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
TyStr | TyTrait(..) | TySlice(_) => Some(false),
|
|
|
|
|
2016-09-05 22:26:02 +00:00
|
|
|
TyAdt(..) | TyProjection(..) | TyParam(..) |
|
2016-07-22 15:56:22 +00:00
|
|
|
TyInfer(..) | TyAnon(..) | TyError => None
|
2016-03-25 03:22:52 +00:00
|
|
|
}.unwrap_or_else(|| self.impls_bound(tcx, param_env, ty::BoundSized, span));
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
if !self.has_param_types() && !self.has_self_ty() {
|
|
|
|
self.flags.set(self.flags.get() | if result {
|
|
|
|
TypeFlags::SIZEDNESS_CACHED | TypeFlags::IS_SIZED
|
|
|
|
} else {
|
|
|
|
TypeFlags::SIZEDNESS_CACHED
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-04-19 06:11:46 +00:00
|
|
|
#[inline]
|
2016-05-11 01:14:41 +00:00
|
|
|
pub fn layout<'lcx>(&'tcx self, infcx: &InferCtxt<'a, 'tcx, 'lcx>)
|
|
|
|
-> Result<&'tcx Layout, LayoutError<'tcx>> {
|
|
|
|
let tcx = infcx.tcx.global_tcx();
|
2016-04-19 06:11:46 +00:00
|
|
|
let can_cache = !self.has_param_types() && !self.has_self_ty();
|
|
|
|
if can_cache {
|
2016-05-11 01:14:41 +00:00
|
|
|
if let Some(&cached) = tcx.layout_cache.borrow().get(&self) {
|
2016-04-19 06:11:46 +00:00
|
|
|
return Ok(cached);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let layout = Layout::compute_uncached(self, infcx)?;
|
|
|
|
if can_cache {
|
2016-05-11 01:14:41 +00:00
|
|
|
tcx.layout_cache.borrow_mut().insert(self, layout);
|
2016-04-19 06:11:46 +00:00
|
|
|
}
|
|
|
|
Ok(layout)
|
|
|
|
}
|
|
|
|
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
/// Check whether a type is representable. This means it cannot contain unboxed
|
|
|
|
/// structural recursion. This check is needed for structs and enums.
|
2016-05-03 02:23:22 +00:00
|
|
|
pub fn is_representable(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span)
|
|
|
|
-> Representability {
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
// Iterate until something non-representable is found
|
2016-05-03 02:23:22 +00:00
|
|
|
fn find_nonrepresentable<'a, 'tcx, It>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 01:56:42 +00:00
|
|
|
sp: Span,
|
|
|
|
seen: &mut Vec<Ty<'tcx>>,
|
|
|
|
iter: It)
|
|
|
|
-> Representability
|
|
|
|
where It: Iterator<Item=Ty<'tcx>> {
|
2015-09-14 11:55:56 +00:00
|
|
|
iter.fold(Representability::Representable,
|
2016-05-03 01:02:41 +00:00
|
|
|
|r, ty| cmp::max(r, is_type_structurally_recursive(tcx, sp, seen, ty)))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 02:23:22 +00:00
|
|
|
fn are_inner_types_recursive<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span,
|
2016-05-03 01:56:42 +00:00
|
|
|
seen: &mut Vec<Ty<'tcx>>, ty: Ty<'tcx>)
|
|
|
|
-> Representability {
|
2015-09-14 11:55:56 +00:00
|
|
|
match ty.sty {
|
|
|
|
TyTuple(ref ts) => {
|
2016-05-03 01:02:41 +00:00
|
|
|
find_nonrepresentable(tcx, sp, seen, ts.iter().cloned())
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
// Fixed-length vectors.
|
|
|
|
// FIXME(#11924) Behavior undecided for zero-length vectors.
|
|
|
|
TyArray(ty, _) => {
|
2016-05-03 01:02:41 +00:00
|
|
|
is_type_structurally_recursive(tcx, sp, seen, ty)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-09-05 22:26:02 +00:00
|
|
|
TyAdt(def, substs) => {
|
2016-05-03 01:02:41 +00:00
|
|
|
find_nonrepresentable(tcx,
|
2015-09-14 11:55:56 +00:00
|
|
|
sp,
|
|
|
|
seen,
|
2016-05-03 01:02:41 +00:00
|
|
|
def.all_fields().map(|f| f.ty(tcx, substs)))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
TyClosure(..) => {
|
|
|
|
// this check is run on type definitions, so we don't expect
|
|
|
|
// to see closure types
|
2016-03-25 00:14:29 +00:00
|
|
|
bug!("requires check invoked on inapplicable type: {:?}", ty)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
_ => Representability::Representable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: ty::AdtDef<'tcx>) -> bool {
|
|
|
|
match ty.sty {
|
2016-09-05 22:26:02 +00:00
|
|
|
TyAdt(ty_def, _) => {
|
2015-09-14 11:55:56 +00:00
|
|
|
ty_def == def
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
|
|
|
|
match (&a.sty, &b.sty) {
|
2016-09-05 22:26:02 +00:00
|
|
|
(&TyAdt(did_a, substs_a), &TyAdt(did_b, substs_b)) => {
|
2015-09-14 11:55:56 +00:00
|
|
|
if did_a != did_b {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-26 22:13:48 +00:00
|
|
|
substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type(a, b))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
a == b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the type `ty` directly (without indirection through a pointer)
|
|
|
|
// contain any types on stack `seen`?
|
2016-05-03 02:23:22 +00:00
|
|
|
fn is_type_structurally_recursive<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 01:56:42 +00:00
|
|
|
sp: Span,
|
|
|
|
seen: &mut Vec<Ty<'tcx>>,
|
|
|
|
ty: Ty<'tcx>) -> Representability {
|
2015-09-14 11:55:56 +00:00
|
|
|
debug!("is_type_structurally_recursive: {:?}", ty);
|
|
|
|
|
|
|
|
match ty.sty {
|
2016-09-05 22:26:02 +00:00
|
|
|
TyAdt(def, _) => {
|
2015-09-14 11:55:56 +00:00
|
|
|
{
|
|
|
|
// Iterate through stack of previously seen types.
|
|
|
|
let mut iter = seen.iter();
|
|
|
|
|
|
|
|
// The first item in `seen` is the type we are actually curious about.
|
|
|
|
// We want to return SelfRecursive if this type contains itself.
|
|
|
|
// It is important that we DON'T take generic parameters into account
|
|
|
|
// for this check, so that Bar<T> in this example counts as SelfRecursive:
|
|
|
|
//
|
|
|
|
// struct Foo;
|
|
|
|
// struct Bar<T> { x: Bar<Foo> }
|
|
|
|
|
2016-07-03 21:38:37 +00:00
|
|
|
if let Some(&seen_type) = iter.next() {
|
|
|
|
if same_struct_or_enum(seen_type, def) {
|
|
|
|
debug!("SelfRecursive: {:?} contains {:?}",
|
|
|
|
seen_type,
|
|
|
|
ty);
|
|
|
|
return Representability::SelfRecursive;
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We also need to know whether the first item contains other types
|
|
|
|
// that are structurally recursive. If we don't catch this case, we
|
|
|
|
// will recurse infinitely for some inputs.
|
|
|
|
//
|
|
|
|
// It is important that we DO take generic parameters into account
|
|
|
|
// here, so that code like this is considered SelfRecursive, not
|
|
|
|
// ContainsRecursive:
|
|
|
|
//
|
|
|
|
// struct Foo { Option<Option<Foo>> }
|
|
|
|
|
|
|
|
for &seen_type in iter {
|
|
|
|
if same_type(ty, seen_type) {
|
|
|
|
debug!("ContainsRecursive: {:?} contains {:?}",
|
|
|
|
seen_type,
|
|
|
|
ty);
|
|
|
|
return Representability::ContainsRecursive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For structs and enums, track all previously seen types by pushing them
|
|
|
|
// onto the 'seen' stack.
|
|
|
|
seen.push(ty);
|
2016-05-03 01:02:41 +00:00
|
|
|
let out = are_inner_types_recursive(tcx, sp, seen, ty);
|
2015-09-14 11:55:56 +00:00
|
|
|
seen.pop();
|
|
|
|
out
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// No need to push in other cases.
|
2016-05-03 01:02:41 +00:00
|
|
|
are_inner_types_recursive(tcx, sp, seen, ty)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("is_type_representable: {:?}", self);
|
|
|
|
|
|
|
|
// To avoid a stack overflow when checking an enum variant or struct that
|
|
|
|
// contains a different, structurally recursive type, maintain a stack
|
|
|
|
// of seen types and check recursion for each of them (issues #3008, #3779).
|
|
|
|
let mut seen: Vec<Ty> = Vec::new();
|
2016-05-03 01:02:41 +00:00
|
|
|
let r = is_type_structurally_recursive(tcx, sp, &mut seen, self);
|
2015-09-14 11:55:56 +00:00
|
|
|
debug!("is_type_representable: {:?} is {:?}", self, r);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|