Skip to content

Commit

Permalink
Merge ty::TyBox into ty::TyAdt
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 30, 2017
1 parent 55f9712 commit ffba0ce
Show file tree
Hide file tree
Showing 73 changed files with 315 additions and 345 deletions.
1 change: 1 addition & 0 deletions src/liballoc/boxed.rs
Expand Up @@ -103,6 +103,7 @@ pub struct ExchangeHeapSingleton {
///
/// See the [module-level documentation](../../std/boxed/index.html) for more.
#[lang = "owned_box"]
#[fundamental]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Box<T: ?Sized>(Unique<T>);

Expand Down
1 change: 0 additions & 1 deletion src/librustc/infer/freshen.rs
Expand Up @@ -156,7 +156,6 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::TyUint(..) |
ty::TyFloat(..) |
ty::TyAdt(..) |
ty::TyBox(..) |
ty::TyStr |
ty::TyError |
ty::TyArray(..) |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/mem_categorization.rs
Expand Up @@ -961,7 +961,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
-> cmt<'tcx>
{
let ptr = match base_cmt.ty.sty {
ty::TyBox(..) => Unique,
ty::TyAdt(def, ..) if def.is_box() => Unique,
ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
ty::TyRef(r, mt) => {
let bk = ty::BorrowKind::from_mutbl(mt.mutbl);
Expand Down
13 changes: 4 additions & 9 deletions src/librustc/traits/coherence.rs
Expand Up @@ -199,7 +199,7 @@ fn orphan_check_trait_ref<'tcx>(tcx: TyCtxt,

fn uncovered_tys<'tcx>(tcx: TyCtxt, ty: Ty<'tcx>, infer_is_local: InferIsLocal)
-> Vec<Ty<'tcx>> {
if ty_is_local_constructor(tcx, ty, infer_is_local) {
if ty_is_local_constructor(ty, infer_is_local) {
vec![]
} else if fundamental_ty(tcx, ty) {
ty.walk_shallow()
Expand All @@ -219,13 +219,13 @@ fn is_type_parameter(ty: Ty) -> bool {
}

fn ty_is_local(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal) -> bool {
ty_is_local_constructor(tcx, ty, infer_is_local) ||
ty_is_local_constructor(ty, infer_is_local) ||
fundamental_ty(tcx, ty) && ty.walk_shallow().any(|t| ty_is_local(tcx, t, infer_is_local))
}

fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
match ty.sty {
ty::TyBox(..) | ty::TyRef(..) => true,
ty::TyRef(..) => true,
ty::TyAdt(def, _) => def.is_fundamental(),
ty::TyDynamic(ref data, ..) => {
data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental"))
Expand All @@ -234,7 +234,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
}
}

fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)-> bool {
fn ty_is_local_constructor(ty: Ty, infer_is_local: InferIsLocal)-> bool {
debug!("ty_is_local_constructor({:?})", ty);

match ty.sty {
Expand Down Expand Up @@ -265,11 +265,6 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
def.did.is_local()
}

ty::TyBox(_) => { // Box<T>
let krate = tcx.lang_items.owned_box().map(|d| d.krate);
krate == Some(LOCAL_CRATE)
}

ty::TyDynamic(ref tt, ..) => {
tt.principal().map_or(false, |p| p.def_id().is_local())
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/error_reporting.rs
Expand Up @@ -154,7 +154,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ty::TyStr => Some(2),
ty::TyInt(..) | ty::TyUint(..) | ty::TyInfer(ty::IntVar(..)) => Some(3),
ty::TyFloat(..) | ty::TyInfer(ty::FloatVar(..)) => Some(4),
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
ty::TyArray(..) | ty::TySlice(..) => Some(6),
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(7),
ty::TyDynamic(..) => Some(8),
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/traits/select.rs
Expand Up @@ -1735,7 +1735,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) |
ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyRawPtr(..) |
ty::TyChar | ty::TyBox(_) | ty::TyRef(..) |
ty::TyChar | ty::TyRef(..) |
ty::TyArray(..) | ty::TyClosure(..) | ty::TyNever |
ty::TyError => {
// safe for everything
Expand Down Expand Up @@ -1788,7 +1788,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
Where(ty::Binder(Vec::new()))
}

ty::TyBox(_) | ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
ty::TyClosure(..) |
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
Never
Expand Down Expand Up @@ -1865,10 +1865,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
t);
}

ty::TyBox(referent_ty) => { // Box<T>
vec![referent_ty]
}

ty::TyRawPtr(ty::TypeAndMut { ty: element_ty, ..}) |
ty::TyRef(_, ty::TypeAndMut { ty: element_ty, ..}) => {
vec![element_ty]
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/contents.rs
Expand Up @@ -191,10 +191,6 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
TC::None
}

ty::TyBox(typ) => {
tc_ty(tcx, typ, cache).owned_pointer()
}

ty::TyDynamic(..) => {
TC::All - TC::InteriorParam
}
Expand Down Expand Up @@ -227,6 +223,10 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
|ty| tc_ty(tcx, *ty, cache))
}

ty::TyAdt(def, _) if def.is_box() => {
tc_ty(tcx, ty.boxed_ty(), cache).owned_pointer()
}

ty::TyAdt(def, substs) => {
let mut res =
TypeContents::union(&def.variants, |v| {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/ty/context.rs
Expand Up @@ -19,6 +19,7 @@ use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::map as hir_map;
use hir::map::DisambiguatedDefPathData;
use middle::free_region::FreeRegionMap;
use middle::lang_items;
use middle::region::RegionMaps;
use middle::resolve_lifetime;
use middle::stability;
Expand Down Expand Up @@ -1088,7 +1089,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
pub fn print_debug_stats(self) {
sty_debug_print!(
self,
TyAdt, TyBox, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
TyAdt, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
TyDynamic, TyClosure, TyTuple, TyParam, TyInfer, TyProjection, TyAnon);

println!("Substs interner: #{}", self.interners.substs.borrow().len());
Expand Down Expand Up @@ -1336,7 +1337,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

pub fn mk_box(self, ty: Ty<'tcx>) -> Ty<'tcx> {
self.mk_ty(TyBox(ty))
let def_id = self.require_lang_item(lang_items::OwnedBoxLangItem);
let adt_def = self.lookup_adt_def(def_id);
let substs = self.mk_substs(iter::once(Kind::from(ty)));
self.mk_ty(TyAdt(adt_def, substs))
}

pub fn mk_ptr(self, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {
Expand Down
1 change: 0 additions & 1 deletion src/librustc/ty/error.rs
Expand Up @@ -181,7 +181,6 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
ty::TyTuple(ref tys) if tys.is_empty() => self.to_string(),

ty::TyAdt(def, _) => format!("{} `{}`", def.descr(), tcx.item_path_str(def.did)),
ty::TyBox(_) => "box".to_string(),
ty::TyArray(_, n) => format!("array of {} elements", n),
ty::TySlice(_) => "slice".to_string(),
ty::TyRawPtr(_) => "*-ptr".to_string(),
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/ty/fast_reject.rs
Expand Up @@ -11,7 +11,6 @@
use hir::def_id::DefId;
use ty::{self, Ty, TyCtxt};
use syntax::ast;
use middle::lang_items::OwnedBoxLangItem;

use self::SimplifiedType::*;

Expand Down Expand Up @@ -69,10 +68,6 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
// view of possibly unifying
simplify_type(tcx, mt.ty, can_simplify_params)
}
ty::TyBox(_) => {
// treat like we would treat `Box`
Some(AdtSimplifiedType(tcx.require_lang_item(OwnedBoxLangItem)))
}
ty::TyClosure(def_id, _) => {
Some(ClosureSimplifiedType(def_id))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/flags.rs
Expand Up @@ -138,7 +138,7 @@ impl FlagComputation {
self.add_region(r);
}

&ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
&ty::TyArray(tt, _) | &ty::TySlice(tt) => {
self.add_ty(tt)
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/ty/item_path.rs
Expand Up @@ -314,8 +314,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
ty::TyDynamic(data, ..) => data.principal().map(|p| p.def_id()),

ty::TyArray(subty, _) |
ty::TySlice(subty) |
ty::TyBox(subty) => characteristic_def_id_of_type(subty),
ty::TySlice(subty) => characteristic_def_id_of_type(subty),

ty::TyRawPtr(mt) |
ty::TyRef(_, mt) => characteristic_def_id_of_type(mt.ty),
Expand Down
77 changes: 44 additions & 33 deletions src/librustc/ty/layout.rs
Expand Up @@ -1053,6 +1053,23 @@ impl<'a, 'gcx, 'tcx> Layout {
let dl = &tcx.data_layout;
assert!(!ty.has_infer_types());

let ptr_layout = |pointee: Ty<'gcx>| {
let non_zero = !ty.is_unsafe_ptr();
let pointee = normalize_associated_type(infcx, pointee);
if pointee.is_sized(tcx, &infcx.parameter_environment, DUMMY_SP) {
Ok(Scalar { value: Pointer, non_zero: non_zero })
} else {
let unsized_part = tcx.struct_tail(pointee);
let meta = match unsized_part.sty {
ty::TySlice(_) | ty::TyStr => {
Int(dl.ptr_sized_integer())
}
ty::TyDynamic(..) => Pointer,
_ => return Err(LayoutError::Unknown(unsized_part))
};
Ok(FatPointer { metadata: meta, non_zero: non_zero })
}
};

let layout = match ty.sty {
// Basic scalars.
Expand Down Expand Up @@ -1082,24 +1099,12 @@ impl<'a, 'gcx, 'tcx> Layout {
},

// Potentially-fat pointers.
ty::TyBox(pointee) |
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
let non_zero = !ty.is_unsafe_ptr();
let pointee = normalize_associated_type(infcx, pointee);
if pointee.is_sized(tcx, &infcx.parameter_environment, DUMMY_SP) {
Scalar { value: Pointer, non_zero: non_zero }
} else {
let unsized_part = tcx.struct_tail(pointee);
let meta = match unsized_part.sty {
ty::TySlice(_) | ty::TyStr => {
Int(dl.ptr_sized_integer())
}
ty::TyDynamic(..) => Pointer,
_ => return Err(LayoutError::Unknown(unsized_part))
};
FatPointer { metadata: meta, non_zero: non_zero }
}
ptr_layout(pointee)?
}
ty::TyAdt(def, _) if def.is_box() => {
ptr_layout(ty.boxed_ty())?
}

// Arrays and slices.
Expand Down Expand Up @@ -1560,26 +1565,32 @@ impl<'a, 'gcx, 'tcx> SizeSkeleton<'gcx> {
Err(err) => err
};

let ptr_skeleton = |pointee: Ty<'gcx>| {
let non_zero = !ty.is_unsafe_ptr();
let tail = tcx.struct_tail(pointee);
match tail.sty {
ty::TyParam(_) | ty::TyProjection(_) => {
assert!(tail.has_param_types() || tail.has_self_ty());
Ok(SizeSkeleton::Pointer {
non_zero: non_zero,
tail: tcx.erase_regions(&tail)
})
}
_ => {
bug!("SizeSkeleton::compute({}): layout errored ({}), yet \
tail `{}` is not a type parameter or a projection",
ty, err, tail)
}
}
};

match ty.sty {
ty::TyBox(pointee) |
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
let non_zero = !ty.is_unsafe_ptr();
let tail = tcx.struct_tail(pointee);
match tail.sty {
ty::TyParam(_) | ty::TyProjection(_) => {
assert!(tail.has_param_types() || tail.has_self_ty());
Ok(SizeSkeleton::Pointer {
non_zero: non_zero,
tail: tcx.erase_regions(&tail)
})
}
_ => {
bug!("SizeSkeleton::compute({}): layout errored ({}), yet \
tail `{}` is not a type parameter or a projection",
ty, err, tail)
}
}
ptr_skeleton(pointee)
}
ty::TyAdt(def, _) if def.is_box() => {
ptr_skeleton(ty.boxed_ty())
}

ty::TyAdt(def, substs) => {
Expand Down
12 changes: 11 additions & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -1302,6 +1302,7 @@ bitflags! {
const IS_SIMD = 1 << 4,
const IS_FUNDAMENTAL = 1 << 5,
const IS_UNION = 1 << 6,
const IS_BOX = 1 << 7,
}
}

Expand Down Expand Up @@ -1376,6 +1377,9 @@ impl<'a, 'gcx, 'tcx> AdtDef {
if Some(did) == tcx.lang_items.phantom_data() {
flags = flags | AdtFlags::IS_PHANTOM_DATA;
}
if Some(did) == tcx.lang_items.owned_box() {
flags = flags | AdtFlags::IS_BOX;
}
match kind {
AdtKind::Enum => flags = flags | AdtFlags::IS_ENUM,
AdtKind::Union => flags = flags | AdtFlags::IS_UNION,
Expand Down Expand Up @@ -1468,6 +1472,12 @@ impl<'a, 'gcx, 'tcx> AdtDef {
self.flags.get().intersects(AdtFlags::IS_PHANTOM_DATA)
}

/// Returns true if this is Box<T>.
#[inline]
pub fn is_box(&self) -> bool {
self.flags.get().intersects(AdtFlags::IS_BOX)
}

/// Returns whether this type has a destructor.
pub fn has_dtor(&self) -> bool {
self.dtor_kind().is_present()
Expand Down Expand Up @@ -1641,7 +1651,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
-> Vec<Ty<'tcx>> {
let result = match ty.sty {
TyBool | TyChar | TyInt(..) | TyUint(..) | TyFloat(..) |
TyBox(..) | TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
TyRawPtr(..) | TyRef(..) | TyFnDef(..) | TyFnPtr(_) |
TyArray(..) | TyClosure(..) | TyNever => {
vec![]
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc/ty/outlives.rs
Expand Up @@ -167,7 +167,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
ty::TyFloat(..) | // OutlivesScalar
ty::TyNever | // ...
ty::TyAdt(..) | // OutlivesNominalType
ty::TyBox(..) | // OutlivesNominalType (ish)
ty::TyAnon(..) | // OutlivesNominalType (ish)
ty::TyStr | // OutlivesScalar (ish)
ty::TyArray(..) | // ...
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/ty/relate.rs
Expand Up @@ -418,12 +418,6 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
Ok(tcx.mk_closure_from_closure_substs(a_id, substs))
}

(&ty::TyBox(a_inner), &ty::TyBox(b_inner)) =>
{
let typ = relation.relate(&a_inner, &b_inner)?;
Ok(tcx.mk_box(typ))
}

(&ty::TyRawPtr(ref a_mt), &ty::TyRawPtr(ref b_mt)) =>
{
let mt = relation.relate(a_mt, b_mt)?;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -468,7 +468,6 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<Ty<'tcx>> {
impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
let sty = match self.sty {
ty::TyBox(typ) => ty::TyBox(typ.fold_with(folder)),
ty::TyRawPtr(tm) => ty::TyRawPtr(tm.fold_with(folder)),
ty::TyArray(typ, sz) => ty::TyArray(typ.fold_with(folder), sz),
ty::TySlice(typ) => ty::TySlice(typ.fold_with(folder)),
Expand Down Expand Up @@ -506,7 +505,6 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match self.sty {
ty::TyBox(typ) => typ.visit_with(visitor),
ty::TyRawPtr(ref tm) => tm.visit_with(visitor),
ty::TyArray(typ, _sz) => typ.visit_with(visitor),
ty::TySlice(typ) => typ.visit_with(visitor),
Expand Down

0 comments on commit ffba0ce

Please sign in to comment.