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
|
|
|
|
|
2017-03-01 16:42:26 +00:00
|
|
|
use hir::def_id::{DefId, LOCAL_CRATE};
|
2016-11-03 20:19:33 +00:00
|
|
|
use hir::map::DefPathData;
|
2016-03-11 00:31:38 +00:00
|
|
|
use infer::InferCtxt;
|
2017-04-05 21:39:02 +00:00
|
|
|
use ich::{StableHashingContext, NodeIdHashingMode};
|
2016-06-30 18:22:47 +00:00
|
|
|
use traits::{self, Reveal};
|
2017-05-12 15:44:31 +00:00
|
|
|
use ty::{self, Ty, TyCtxt, TypeFoldable};
|
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};
|
2017-04-23 14:43:23 +00:00
|
|
|
use ty::subst::{Subst, Kind};
|
2016-03-22 15:30:57 +00:00
|
|
|
use ty::TypeVariants::*;
|
2017-03-01 16:42:26 +00:00
|
|
|
use util::common::ErrorReported;
|
2016-11-14 02:42:15 +00:00
|
|
|
use middle::lang_items;
|
2015-12-16 17:44:15 +00:00
|
|
|
|
2017-02-05 05:01:48 +00:00
|
|
|
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize};
|
2017-04-05 21:39:02 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
|
|
|
|
HashStable};
|
2015-09-14 11:55:56 +00:00
|
|
|
use std::cmp;
|
2016-12-13 23:45:03 +00:00
|
|
|
use std::hash::Hash;
|
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};
|
2017-03-01 16:42:26 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2017-02-05 05:01:48 +00:00
|
|
|
type Disr = ConstInt;
|
|
|
|
|
2017-03-13 00:12:13 +00:00
|
|
|
pub trait IntTypeExt {
|
2017-02-05 05:01:48 +00:00
|
|
|
fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
|
|
|
|
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
|
|
|
|
-> Option<Disr>;
|
|
|
|
fn assert_ty_matches(&self, val: Disr);
|
|
|
|
fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr;
|
2017-03-13 00:12:13 +00:00
|
|
|
}
|
2017-02-05 05:01:48 +00:00
|
|
|
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2017-02-15 13:00:20 +00:00
|
|
|
macro_rules! typed_literal {
|
|
|
|
($tcx:expr, $ty:expr, $lit:expr) => {
|
|
|
|
match $ty {
|
|
|
|
SignedInt(ast::IntTy::I8) => ConstInt::I8($lit),
|
|
|
|
SignedInt(ast::IntTy::I16) => ConstInt::I16($lit),
|
|
|
|
SignedInt(ast::IntTy::I32) => ConstInt::I32($lit),
|
|
|
|
SignedInt(ast::IntTy::I64) => ConstInt::I64($lit),
|
|
|
|
SignedInt(ast::IntTy::I128) => ConstInt::I128($lit),
|
|
|
|
SignedInt(ast::IntTy::Is) => match $tcx.sess.target.int_type {
|
|
|
|
ast::IntTy::I16 => ConstInt::Isize(ConstIsize::Is16($lit)),
|
|
|
|
ast::IntTy::I32 => ConstInt::Isize(ConstIsize::Is32($lit)),
|
|
|
|
ast::IntTy::I64 => ConstInt::Isize(ConstIsize::Is64($lit)),
|
|
|
|
_ => bug!(),
|
|
|
|
},
|
|
|
|
UnsignedInt(ast::UintTy::U8) => ConstInt::U8($lit),
|
|
|
|
UnsignedInt(ast::UintTy::U16) => ConstInt::U16($lit),
|
|
|
|
UnsignedInt(ast::UintTy::U32) => ConstInt::U32($lit),
|
|
|
|
UnsignedInt(ast::UintTy::U64) => ConstInt::U64($lit),
|
|
|
|
UnsignedInt(ast::UintTy::U128) => ConstInt::U128($lit),
|
|
|
|
UnsignedInt(ast::UintTy::Us) => match $tcx.sess.target.uint_type {
|
|
|
|
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16($lit)),
|
|
|
|
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32($lit)),
|
|
|
|
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64($lit)),
|
|
|
|
_ => bug!(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 11:55:56 +00:00
|
|
|
impl IntTypeExt for attr::IntType {
|
2017-02-05 05:01:48 +00:00
|
|
|
fn to_ty<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
|
|
|
|
match *self {
|
|
|
|
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::I128) => tcx.types.i128,
|
|
|
|
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::U128) => tcx.types.u128,
|
|
|
|
UnsignedInt(ast::UintTy::Us) => tcx.types.usize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initial_discriminant<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Disr {
|
2017-02-15 13:00:20 +00:00
|
|
|
typed_literal!(tcx, *self, 0)
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 05:01:48 +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::I128), ConstInt::I128(_)) => {},
|
|
|
|
(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::U128), ConstInt::U128(_)) => {},
|
|
|
|
(UnsignedInt(ast::UintTy::Us), ConstInt::Usize(_)) => {},
|
|
|
|
_ => bug!("disr type mismatch: {:?} vs {:?}", self, val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
|
|
|
|
-> Option<Disr> {
|
|
|
|
if let Some(val) = val {
|
|
|
|
self.assert_ty_matches(val);
|
2017-02-15 13:00:20 +00:00
|
|
|
(val + typed_literal!(tcx, *self, 1)).ok()
|
2017-02-05 05:01:48 +00:00
|
|
|
} else {
|
|
|
|
Some(self.initial_discriminant(tcx))
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2017-01-03 21:54:12 +00:00
|
|
|
pub enum CopyImplementationError<'tcx> {
|
|
|
|
InfrigingField(&'tcx ty::FieldDef),
|
2015-09-14 11:55:56 +00:00
|
|
|
NotAnAdt,
|
2017-03-13 00:12:13 +00:00
|
|
|
HasDestructor,
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2017-04-15 23:18:09 +00:00
|
|
|
#[derive(Clone, PartialOrd, Ord, Eq, PartialEq, Debug)]
|
2015-09-14 11:55:56 +00:00
|
|
|
pub enum Representability {
|
|
|
|
Representable,
|
|
|
|
ContainsRecursive,
|
2017-04-15 23:18:09 +00:00
|
|
|
SelfRecursive(Vec<Span>),
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 23:36:45 +00:00
|
|
|
impl<'tcx> ty::ParamEnv<'tcx> {
|
2017-05-10 14:28:06 +00:00
|
|
|
/// Construct a trait environment suitable for contexts where
|
|
|
|
/// there are no where clauses in scope.
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self::new(ty::Slice::empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a trait environment with the given set of predicates.
|
|
|
|
pub fn new(caller_bounds: &'tcx ty::Slice<ty::Predicate<'tcx>>) -> Self {
|
2017-05-15 21:57:30 +00:00
|
|
|
ty::ParamEnv { caller_bounds }
|
2017-05-10 14:28:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 03:22:52 +00:00
|
|
|
pub fn can_type_implement_copy<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
self_type: Ty<'tcx>, span: Span)
|
2017-01-03 21:54:12 +00:00
|
|
|
-> Result<(), CopyImplementationError> {
|
2015-09-14 11:55:56 +00:00
|
|
|
// FIXME: (@jroesch) float this code up
|
2017-02-19 12:46:29 +00:00
|
|
|
tcx.infer_ctxt(self.clone(), Reveal::UserFacing).enter(|infcx| {
|
2016-12-04 00:04:08 +00:00
|
|
|
let (adt, substs) = match self_type.sty {
|
|
|
|
ty::TyAdt(adt, substs) => (adt, substs),
|
2017-03-13 00:12:13 +00:00
|
|
|
_ => return Err(CopyImplementationError::NotAnAdt),
|
2016-12-04 00:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let field_implements_copy = |field: &ty::FieldDef| {
|
|
|
|
let cause = traits::ObligationCause::dummy();
|
|
|
|
match traits::fully_normalize(&infcx, cause, &field.ty(tcx, substs)) {
|
|
|
|
Ok(ty) => !infcx.type_moves_by_default(ty, span),
|
2017-03-13 00:12:13 +00:00
|
|
|
Err(..) => false,
|
2016-12-04 00:04:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-03 21:54:12 +00:00
|
|
|
for variant in &adt.variants {
|
|
|
|
for field in &variant.fields {
|
|
|
|
if !field_implements_copy(field) {
|
|
|
|
return Err(CopyImplementationError::InfrigingField(field));
|
2016-09-05 22:26:02 +00:00
|
|
|
}
|
2016-12-04 00:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2017-02-19 12:46:29 +00:00
|
|
|
if adt.has_dtor(tcx) {
|
2016-05-11 01:14:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 21:39:02 +00:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
|
|
|
/// 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.
|
|
|
|
pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 {
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
let mut hcx = StableHashingContext::new(self);
|
|
|
|
|
|
|
|
hcx.while_hashing_spans(false, |hcx| {
|
|
|
|
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
|
|
|
|
ty.hash_stable(hcx, &mut hasher);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 00:12:13 +00:00
|
|
|
_ => (),
|
2016-07-30 15:58:30 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2017-01-11 07:58:37 +00:00
|
|
|
(&TyTuple(ref v, _), None) => v.get(i).cloned(),
|
2017-03-13 00:12:13 +00:00
|
|
|
_ => None,
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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() {
|
2017-03-13 00:12:13 +00:00
|
|
|
break;
|
2016-09-05 22:26:02 +00:00
|
|
|
}
|
|
|
|
match def.struct_variant().fields.last() {
|
|
|
|
Some(f) => ty = f.ty(self, substs),
|
2017-03-13 00:12:13 +00:00
|
|
|
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() {
|
2017-03-13 00:12:13 +00:00
|
|
|
break;
|
2016-09-05 22:26:02 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2017-03-13 00:12:13 +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.
|
2017-05-02 18:37:00 +00:00
|
|
|
///
|
|
|
|
/// FIXME callers may only have a &[Predicate], not a Vec, so that's
|
|
|
|
/// what this code should accept.
|
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>>)
|
2017-04-20 08:45:53 +00:00
|
|
|
-> Vec<ty::Region<'tcx>> {
|
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(..) |
|
2017-03-10 02:47:09 +00:00
|
|
|
ty::Predicate::Subtype(..) |
|
2015-09-14 11:55:56 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:42:26 +00:00
|
|
|
/// Calculate the destructor of a given type.
|
|
|
|
pub fn calculate_dtor(
|
|
|
|
self,
|
|
|
|
adt_did: DefId,
|
|
|
|
validate: &mut FnMut(Self, DefId) -> Result<(), ErrorReported>
|
|
|
|
) -> Option<ty::Destructor> {
|
|
|
|
let drop_trait = if let Some(def_id) = self.lang_items.drop_trait() {
|
|
|
|
def_id
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
2017-04-24 14:23:36 +00:00
|
|
|
self.coherent_trait((LOCAL_CRATE, drop_trait));
|
2017-03-01 16:42:26 +00:00
|
|
|
|
|
|
|
let mut dtor_did = None;
|
2017-04-24 12:20:46 +00:00
|
|
|
let ty = self.type_of(adt_did);
|
|
|
|
self.trait_def(drop_trait).for_each_relevant_impl(self, ty, |impl_did| {
|
2017-03-01 16:42:26 +00:00
|
|
|
if let Some(item) = self.associated_items(impl_did).next() {
|
|
|
|
if let Ok(()) = validate(self, impl_did) {
|
|
|
|
dtor_did = Some(item.def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let dtor_did = match dtor_did {
|
2015-09-14 11:55:56 +00:00
|
|
|
Some(dtor) => dtor,
|
2017-03-13 00:12:13 +00:00
|
|
|
None => return None,
|
2015-09-14 11:55:56 +00:00
|
|
|
};
|
|
|
|
|
2017-04-23 14:43:23 +00:00
|
|
|
Some(ty::Destructor { did: dtor_did })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the set of types that are required to be alive in
|
|
|
|
/// order to run the destructor of `def` (see RFCs 769 and
|
|
|
|
/// 1238).
|
|
|
|
///
|
|
|
|
/// Note that this returns only the constraints for the
|
|
|
|
/// destructor of `def` itself. For the destructors of the
|
|
|
|
/// contents, you need `adt_dtorck_constraint`.
|
|
|
|
pub fn destructor_constraints(self, def: &'tcx ty::AdtDef)
|
|
|
|
-> Vec<ty::subst::Kind<'tcx>>
|
|
|
|
{
|
|
|
|
let dtor = match def.destructor(self) {
|
|
|
|
None => {
|
|
|
|
debug!("destructor_constraints({:?}) - no dtor", def.did);
|
|
|
|
return vec![]
|
|
|
|
}
|
|
|
|
Some(dtor) => dtor.did
|
|
|
|
};
|
|
|
|
|
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>`).
|
2017-04-23 14:43:23 +00:00
|
|
|
if self.has_attr(dtor, "unsafe_destructor_blind_to_params") {
|
|
|
|
debug!("destructor_constraint({:?}) - blind", def.did);
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
let impl_def_id = self.associated_item(dtor).container.id();
|
2017-04-24 12:20:46 +00:00
|
|
|
let impl_generics = self.generics_of(impl_def_id);
|
2017-04-23 14:43:23 +00:00
|
|
|
|
|
|
|
// We have a destructor - all the parameters that are not
|
|
|
|
// pure_wrt_drop (i.e, don't have a #[may_dangle] attribute)
|
|
|
|
// must be live.
|
|
|
|
|
|
|
|
// We need to return the list of parameters from the ADTs
|
|
|
|
// generics/substs that correspond to impure parameters on the
|
|
|
|
// impl's generics. This is a bit ugly, but conceptually simple:
|
|
|
|
//
|
|
|
|
// Suppose our ADT looks like the following
|
|
|
|
//
|
|
|
|
// struct S<X, Y, Z>(X, Y, Z);
|
|
|
|
//
|
|
|
|
// and the impl is
|
|
|
|
//
|
|
|
|
// impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0>
|
|
|
|
//
|
|
|
|
// We want to return the parameters (X, Y). For that, we match
|
|
|
|
// up the item-substs <X, Y, Z> with the substs on the impl ADT,
|
|
|
|
// <P1, P2, P0>, and then look up which of the impl substs refer to
|
|
|
|
// parameters marked as pure.
|
|
|
|
|
2017-04-24 12:20:46 +00:00
|
|
|
let impl_substs = match self.type_of(impl_def_id).sty {
|
2017-04-23 14:43:23 +00:00
|
|
|
ty::TyAdt(def_, substs) if def_ == def => substs,
|
|
|
|
_ => bug!()
|
|
|
|
};
|
|
|
|
|
2017-04-24 12:20:46 +00:00
|
|
|
let item_substs = match self.type_of(def.did).sty {
|
2017-04-23 14:43:23 +00:00
|
|
|
ty::TyAdt(def_, substs) if def_ == def => substs,
|
|
|
|
_ => bug!()
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = item_substs.iter().zip(impl_substs.iter())
|
|
|
|
.filter(|&(_, &k)| {
|
2017-04-20 08:45:53 +00:00
|
|
|
if let Some(&ty::RegionKind::ReEarlyBound(ref ebr)) = k.as_region() {
|
2017-04-23 14:43:23 +00:00
|
|
|
!impl_generics.region_param(ebr).pure_wrt_drop
|
|
|
|
} else if let Some(&ty::TyS {
|
|
|
|
sty: ty::TypeVariants::TyParam(ref pt), ..
|
|
|
|
}) = k.as_type() {
|
|
|
|
!impl_generics.type_param(pt).pure_wrt_drop
|
|
|
|
} else {
|
|
|
|
// not a type or region param - this should be reported
|
|
|
|
// as an error.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}).map(|(&item_param, _)| item_param).collect();
|
|
|
|
debug!("destructor_constraint({:?}) = {:?}", def.did, result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a set of constraints that needs to be satisfied in
|
|
|
|
/// order for `ty` to be valid for destruction.
|
|
|
|
pub fn dtorck_constraint_for_ty(self,
|
|
|
|
span: Span,
|
|
|
|
for_ty: Ty<'tcx>,
|
|
|
|
depth: usize,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> Result<ty::DtorckConstraint<'tcx>, ErrorReported>
|
|
|
|
{
|
|
|
|
debug!("dtorck_constraint_for_ty({:?}, {:?}, {:?}, {:?})",
|
|
|
|
span, for_ty, depth, ty);
|
|
|
|
|
|
|
|
if depth >= self.sess.recursion_limit.get() {
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self.sess, span, E0320,
|
|
|
|
"overflow while adding drop-check rules for {}", for_ty);
|
|
|
|
err.note(&format!("overflowed on {}", ty));
|
|
|
|
err.emit();
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = match ty.sty {
|
|
|
|
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) |
|
|
|
|
ty::TyFloat(_) | ty::TyStr | ty::TyNever |
|
|
|
|
ty::TyRawPtr(..) | ty::TyRef(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) => {
|
|
|
|
// these types never have a destructor
|
|
|
|
Ok(ty::DtorckConstraint::empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyArray(ety, _) | ty::TySlice(ety) => {
|
|
|
|
// single-element containers, behave like their element
|
|
|
|
self.dtorck_constraint_for_ty(span, for_ty, depth+1, ety)
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyTuple(tys, _) => {
|
|
|
|
tys.iter().map(|ty| {
|
|
|
|
self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyClosure(def_id, substs) => {
|
|
|
|
substs.upvar_tys(def_id, self).map(|ty| {
|
|
|
|
self.dtorck_constraint_for_ty(span, for_ty, depth+1, ty)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyAdt(def, substs) => {
|
|
|
|
let ty::DtorckConstraint {
|
|
|
|
dtorck_types, outlives
|
2017-04-24 15:06:39 +00:00
|
|
|
} = self.at(span).adt_dtorck_constraint(def.did);
|
2017-04-23 14:43:23 +00:00
|
|
|
Ok(ty::DtorckConstraint {
|
|
|
|
// FIXME: we can try to recursively `dtorck_constraint_on_ty`
|
|
|
|
// there, but that needs some way to handle cycles.
|
|
|
|
dtorck_types: dtorck_types.subst(self, substs),
|
|
|
|
outlives: outlives.subst(self, substs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Objects must be alive in order for their destructor
|
|
|
|
// to be called.
|
|
|
|
ty::TyDynamic(..) => Ok(ty::DtorckConstraint {
|
|
|
|
outlives: vec![Kind::from(ty)],
|
|
|
|
dtorck_types: vec![],
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Types that can't be resolved. Pass them forward.
|
|
|
|
ty::TyProjection(..) | ty::TyAnon(..) | ty::TyParam(..) => {
|
|
|
|
Ok(ty::DtorckConstraint {
|
|
|
|
outlives: vec![],
|
|
|
|
dtorck_types: vec![ty],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyInfer(..) | ty::TyError => {
|
|
|
|
self.sess.delay_span_bug(span, "unresolved type in dtorck");
|
|
|
|
Err(ErrorReported)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("dtorck_constraint_for_ty({:?}) = {:?}", ty, result);
|
|
|
|
result
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2016-11-03 20:19:33 +00:00
|
|
|
|
2017-04-23 14:43:23 +00:00
|
|
|
pub fn closure_base_def_id(self, def_id: DefId) -> DefId {
|
2016-11-03 20:19:33 +00:00
|
|
|
let mut def_id = def_id;
|
|
|
|
while self.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr {
|
|
|
|
def_id = self.parent_def_id(def_id).unwrap_or_else(|| {
|
|
|
|
bug!("closure {:?} has no parent", def_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
def_id
|
|
|
|
}
|
2017-02-08 17:31:03 +00:00
|
|
|
|
|
|
|
/// Given the def-id of some item that has no type parameters, make
|
|
|
|
/// a suitable "empty substs" for it.
|
|
|
|
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> &'tcx ty::Substs<'tcx> {
|
|
|
|
ty::Substs::for_item(self, item_def_id,
|
2017-04-19 22:58:12 +00:00
|
|
|
|_, _| self.types.re_erased,
|
2017-02-08 17:31:03 +00:00
|
|
|
|_, _| {
|
|
|
|
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
|
|
|
|
})
|
|
|
|
}
|
2017-05-10 22:02:52 +00:00
|
|
|
|
|
|
|
pub fn const_usize(&self, val: usize) -> ConstInt {
|
|
|
|
match self.sess.target.uint_type {
|
|
|
|
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(val as u16)),
|
|
|
|
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(val as u32)),
|
|
|
|
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(val as u64)),
|
|
|
|
_ => bug!(),
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
state: StableHasher<W>,
|
2016-09-30 18:06:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
impl<'a, 'gcx, 'tcx, W> TypeIdHasher<'a, 'gcx, 'tcx, W>
|
|
|
|
where W: StableHasherResult
|
|
|
|
{
|
|
|
|
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self {
|
|
|
|
TypeIdHasher { tcx: tcx, state: StableHasher::new() }
|
2016-09-30 18:06:51 +00:00
|
|
|
}
|
2016-08-05 21:50:13 +00:00
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
pub fn finish(self) -> W {
|
|
|
|
self.state.finish()
|
2016-09-01 08:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash<T: Hash>(&mut self, x: T) {
|
2016-08-05 21:50:13 +00:00
|
|
|
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) {
|
2016-09-13 12:45:34 +00:00
|
|
|
// Hash the DefPath corresponding to the DefId, which is independent
|
2017-04-03 17:20:26 +00:00
|
|
|
// of compiler internal state. We already have a stable hash value of
|
|
|
|
// all DefPaths available via tcx.def_path_hash(), so we just feed that
|
|
|
|
// into the hasher.
|
|
|
|
let hash = self.tcx.def_path_hash(did);
|
|
|
|
self.hash(hash);
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 23:45:03 +00:00
|
|
|
impl<'a, 'gcx, 'tcx, W> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tcx, W>
|
|
|
|
where W: StableHasherResult
|
|
|
|
{
|
2016-08-05 21:50:13 +00:00
|
|
|
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-30 18:06:51 +00:00
|
|
|
TyArray(_, n) => self.hash(n),
|
2016-08-05 21:50:13 +00:00
|
|
|
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-09-19 09:37:12 +00:00
|
|
|
TyAdt(d, _) => self.def_id(d.did),
|
2016-08-05 21:50:13 +00:00
|
|
|
TyFnPtr(f) => {
|
2017-02-13 08:51:06 +00:00
|
|
|
self.hash(f.unsafety());
|
|
|
|
self.hash(f.abi());
|
|
|
|
self.hash(f.variadic());
|
|
|
|
self.hash(f.inputs().skip_binder().len());
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
2016-11-16 16:21:49 +00:00
|
|
|
TyDynamic(ref data, ..) => {
|
|
|
|
if let Some(p) = data.principal() {
|
2016-11-12 22:46:16 +00:00
|
|
|
self.def_id(p.def_id());
|
|
|
|
}
|
2016-11-13 22:25:54 +00:00
|
|
|
for d in data.auto_traits() {
|
|
|
|
self.def_id(d);
|
|
|
|
}
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
2017-01-12 16:50:15 +00:00
|
|
|
TyTuple(tys, defaulted) => {
|
2016-09-30 18:06:51 +00:00
|
|
|
self.hash(tys.len());
|
2017-01-12 16:50:15 +00:00
|
|
|
self.hash(defaulted);
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
|
|
|
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 |
|
2016-09-19 09:37:12 +00:00
|
|
|
TySlice(_) => {}
|
|
|
|
|
|
|
|
TyError |
|
|
|
|
TyInfer(_) => bug!("TypeIdHasher: unexpected type {}", ty)
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ty.super_visit_with(self)
|
|
|
|
}
|
|
|
|
|
2017-04-20 08:45:53 +00:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
|
2017-03-20 14:24:30 +00:00
|
|
|
self.hash_discriminant_u8(r);
|
2016-08-25 20:58:52 +00:00
|
|
|
match *r {
|
2017-03-20 14:24:30 +00:00
|
|
|
ty::ReErased |
|
|
|
|
ty::ReStatic |
|
2017-03-21 09:42:51 +00:00
|
|
|
ty::ReEmpty => {
|
|
|
|
// No variant fields to hash for these ...
|
|
|
|
}
|
2016-08-05 21:50:13 +00:00
|
|
|
ty::ReLateBound(db, ty::BrAnon(i)) => {
|
2017-03-20 14:24:30 +00:00
|
|
|
self.hash(db.depth);
|
2016-08-05 21:50:13 +00:00
|
|
|
self.hash(i);
|
|
|
|
}
|
2017-05-11 12:05:00 +00:00
|
|
|
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, .. }) => {
|
|
|
|
self.def_id(def_id);
|
2017-03-20 14:24:30 +00:00
|
|
|
}
|
2016-08-05 21:50:13 +00:00
|
|
|
ty::ReLateBound(..) |
|
|
|
|
ty::ReFree(..) |
|
|
|
|
ty::ReScope(..) |
|
|
|
|
ty::ReVar(..) |
|
|
|
|
ty::ReSkolemized(..) => {
|
2016-09-19 09:37:12 +00:00
|
|
|
bug!("TypeIdHasher: unexpected region {:?}", r)
|
2016-08-05 21:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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> {
|
2017-05-10 14:28:06 +00:00
|
|
|
pub fn moves_by_default(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 23:36:45 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-05-10 14:28:06 +00:00
|
|
|
span: Span)
|
|
|
|
-> bool {
|
2017-05-12 15:44:00 +00:00
|
|
|
!tcx.at(span).is_copy_raw(param_env.and(self))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:28:06 +00:00
|
|
|
pub fn is_sized(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 23:36:45 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-05-10 14:28:06 +00:00
|
|
|
span: Span)-> bool
|
2015-09-14 11:55:56 +00:00
|
|
|
{
|
2017-05-12 15:44:00 +00:00
|
|
|
tcx.at(span).is_sized_raw(param_env.and(self))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 14:28:06 +00:00
|
|
|
pub fn is_freeze(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 23:36:45 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-05-10 14:28:06 +00:00
|
|
|
span: Span)-> bool
|
2017-04-17 18:18:56 +00:00
|
|
|
{
|
2017-05-12 15:44:00 +00:00
|
|
|
tcx.at(span).is_freeze_raw(param_env.and(self))
|
2017-04-17 18:18:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 21:22:55 +00:00
|
|
|
/// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely
|
|
|
|
/// non-copy and *might* have a destructor attached; if it returns
|
|
|
|
/// `false`, then `ty` definitely has no destructor (i.e. no drop glue).
|
|
|
|
///
|
|
|
|
/// (Note that this implies that if `ty` has a destructor attached,
|
|
|
|
/// then `needs_drop` will definitely return `true` for `ty`.)
|
2017-04-17 20:26:48 +00:00
|
|
|
#[inline]
|
2017-05-10 14:28:06 +00:00
|
|
|
pub fn needs_drop(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 21:57:30 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>)
|
2017-05-10 14:28:06 +00:00
|
|
|
-> bool {
|
2017-05-12 15:44:31 +00:00
|
|
|
tcx.needs_drop_raw(param_env.and(self))
|
2017-04-17 20:26:48 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 00:44:19 +00:00
|
|
|
let rec_limit = tcx.sess.recursion_limit.get();
|
|
|
|
let depth = tcx.layout_depth.get();
|
|
|
|
if depth > rec_limit {
|
|
|
|
tcx.sess.fatal(
|
|
|
|
&format!("overflow representing the type `{}`", self));
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.layout_depth.set(depth+1);
|
2017-02-08 09:23:09 +00:00
|
|
|
let layout = Layout::compute_uncached(self, infcx);
|
|
|
|
tcx.layout_depth.set(depth);
|
|
|
|
let layout = layout?;
|
2016-04-19 06:11:46 +00:00
|
|
|
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.
|
2017-04-15 23:18:09 +00:00
|
|
|
pub fn is_representable(&'tcx self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
sp: Span)
|
2016-05-03 02:23:22 +00:00
|
|
|
-> Representability {
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
// Iterate until something non-representable is found
|
2017-04-15 23:18:09 +00:00
|
|
|
fn fold_repr<It: Iterator<Item=Representability>>(iter: It) -> Representability {
|
|
|
|
iter.fold(Representability::Representable, |r1, r2| {
|
|
|
|
match (r1, r2) {
|
|
|
|
(Representability::SelfRecursive(v1),
|
|
|
|
Representability::SelfRecursive(v2)) => {
|
|
|
|
Representability::SelfRecursive(v1.iter().map(|s| *s).chain(v2).collect())
|
|
|
|
}
|
|
|
|
(r1, r2) => cmp::max(r1, r2)
|
|
|
|
}
|
|
|
|
})
|
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 {
|
2017-01-11 07:58:37 +00:00
|
|
|
TyTuple(ref ts, _) => {
|
2017-04-15 23:18:09 +00:00
|
|
|
// Find non representable
|
|
|
|
fold_repr(ts.iter().map(|ty| {
|
|
|
|
is_type_structurally_recursive(tcx, sp, seen, ty)
|
|
|
|
}))
|
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) => {
|
2017-04-15 23:18:09 +00:00
|
|
|
// Find non representable fields with their spans
|
|
|
|
fold_repr(def.all_fields().map(|field| {
|
|
|
|
let ty = field.ty(tcx, substs);
|
|
|
|
let span = tcx.hir.span_if_local(field.did).unwrap_or(sp);
|
|
|
|
match is_type_structurally_recursive(tcx, span, seen, ty) {
|
|
|
|
Representability::SelfRecursive(_) => {
|
|
|
|
Representability::SelfRecursive(vec![span])
|
|
|
|
}
|
|
|
|
x => x,
|
|
|
|
}
|
|
|
|
}))
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 23:33:29 +00:00
|
|
|
fn same_struct_or_enum<'tcx>(ty: Ty<'tcx>, def: &'tcx ty::AdtDef) -> bool {
|
2015-09-14 11:55:56 +00:00
|
|
|
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
|
|
|
}
|
2017-03-13 00:12:13 +00:00
|
|
|
_ => a == b,
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-04-15 23:18:09 +00:00
|
|
|
debug!("is_type_structurally_recursive: {:?} {:?}", ty, sp);
|
2015-09-14 11:55:56 +00:00
|
|
|
|
|
|
|
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);
|
2017-04-15 23:18:09 +00:00
|
|
|
return Representability::SelfRecursive(vec![sp]);
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 14:28:06 +00:00
|
|
|
|
|
|
|
fn is_copy_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 21:57:30 +00:00
|
|
|
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
|
2017-05-10 14:28:06 +00:00
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
let (param_env, ty) = query.into_parts();
|
|
|
|
let trait_def_id = tcx.require_lang_item(lang_items::CopyTraitLangItem);
|
|
|
|
tcx.infer_ctxt(param_env, Reveal::UserFacing)
|
|
|
|
.enter(|infcx| traits::type_known_to_meet_bound(&infcx, ty, trait_def_id, DUMMY_SP))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_sized_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 21:57:30 +00:00
|
|
|
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
|
2017-05-10 14:28:06 +00:00
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
let (param_env, ty) = query.into_parts();
|
|
|
|
let trait_def_id = tcx.require_lang_item(lang_items::SizedTraitLangItem);
|
|
|
|
tcx.infer_ctxt(param_env, Reveal::UserFacing)
|
|
|
|
.enter(|infcx| traits::type_known_to_meet_bound(&infcx, ty, trait_def_id, DUMMY_SP))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_freeze_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 21:57:30 +00:00
|
|
|
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
|
2017-05-10 14:28:06 +00:00
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
let (param_env, ty) = query.into_parts();
|
|
|
|
let trait_def_id = tcx.require_lang_item(lang_items::FreezeTraitLangItem);
|
|
|
|
tcx.infer_ctxt(param_env, Reveal::UserFacing)
|
|
|
|
.enter(|infcx| traits::type_known_to_meet_bound(&infcx, ty, trait_def_id, DUMMY_SP))
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:44:31 +00:00
|
|
|
fn needs_drop_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-15 21:57:30 +00:00
|
|
|
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
|
2017-05-12 15:44:31 +00:00
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
let (param_env, ty) = query.into_parts();
|
|
|
|
|
|
|
|
let needs_drop = |ty: Ty<'tcx>| -> bool {
|
|
|
|
match ty::queries::needs_drop_raw::try_get(tcx, DUMMY_SP, param_env.and(ty)) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => {
|
|
|
|
// Cycles should be reported as an error by `check_representable`.
|
|
|
|
//
|
|
|
|
// Consider the type as not needing drop in the meanwhile to avoid
|
|
|
|
// further errors.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!ty.needs_infer());
|
|
|
|
|
|
|
|
match ty.sty {
|
|
|
|
// Fast-path for primitive types
|
|
|
|
ty::TyInfer(ty::FreshIntTy(_)) | ty::TyInfer(ty::FreshFloatTy(_)) |
|
|
|
|
ty::TyBool | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) | ty::TyNever |
|
|
|
|
ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar |
|
|
|
|
ty::TyRawPtr(_) | ty::TyRef(..) | ty::TyStr => false,
|
|
|
|
|
|
|
|
// Issue #22536: We first query type_moves_by_default. It sees a
|
|
|
|
// normalized version of the type, and therefore will definitely
|
|
|
|
// know whether the type implements Copy (and thus needs no
|
|
|
|
// cleanup/drop/zeroing) ...
|
|
|
|
_ if !ty.moves_by_default(tcx, param_env, DUMMY_SP) => false,
|
|
|
|
|
|
|
|
// ... (issue #22536 continued) but as an optimization, still use
|
|
|
|
// prior logic of asking for the structural "may drop".
|
|
|
|
|
|
|
|
// FIXME(#22815): Note that this is a conservative heuristic;
|
|
|
|
// it may report that the type "may drop" when actual type does
|
|
|
|
// not actually have a destructor associated with it. But since
|
|
|
|
// the type absolutely did not have the `Copy` bound attached
|
|
|
|
// (see above), it is sound to treat it as having a destructor.
|
|
|
|
|
|
|
|
// User destructors are the only way to have concrete drop types.
|
|
|
|
ty::TyAdt(def, _) if def.has_dtor(tcx) => true,
|
|
|
|
|
|
|
|
// Can refer to a type which may drop.
|
2017-05-15 21:57:30 +00:00
|
|
|
// FIXME(eddyb) check this against a ParamEnv.
|
2017-05-12 15:44:31 +00:00
|
|
|
ty::TyDynamic(..) | ty::TyProjection(..) | ty::TyParam(_) |
|
|
|
|
ty::TyAnon(..) | ty::TyInfer(_) | ty::TyError => true,
|
|
|
|
|
|
|
|
// Structural recursion.
|
|
|
|
ty::TyArray(ty, _) | ty::TySlice(ty) => needs_drop(ty),
|
|
|
|
|
|
|
|
ty::TyClosure(def_id, ref substs) => substs.upvar_tys(def_id, tcx).any(needs_drop),
|
|
|
|
|
|
|
|
ty::TyTuple(ref tys, _) => tys.iter().cloned().any(needs_drop),
|
|
|
|
|
|
|
|
// unions don't have destructors regardless of the child types
|
|
|
|
ty::TyAdt(def, _) if def.is_union() => false,
|
|
|
|
|
|
|
|
ty::TyAdt(def, substs) =>
|
|
|
|
def.variants.iter().any(
|
|
|
|
|variant| variant.fields.iter().any(
|
|
|
|
|field| needs_drop(field.ty(tcx, substs)))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 14:28:06 +00:00
|
|
|
pub fn provide(providers: &mut ty::maps::Providers) {
|
|
|
|
*providers = ty::maps::Providers {
|
|
|
|
is_copy_raw,
|
|
|
|
is_sized_raw,
|
|
|
|
is_freeze_raw,
|
2017-05-12 15:44:31 +00:00
|
|
|
needs_drop_raw,
|
2017-05-10 14:28:06 +00:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|