Skip to content

Commit

Permalink
Auto merge of #39230 - petrochenkov:nobox, r=eddyb
Browse files Browse the repository at this point in the history
Refactoring TyBox -> TyAdt

r? @eddyb
cc @Manishearth
  • Loading branch information
bors committed Jan 31, 2017
2 parents 8921707 + 93e3f63 commit 8e9e055
Show file tree
Hide file tree
Showing 76 changed files with 328 additions and 396 deletions.
9 changes: 9 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
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 Expand Up @@ -292,6 +293,14 @@ impl<T: ?Sized> Box<T> {
}
}

#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
fn drop(&mut self) {
// FIXME: Do nothing, drop is currently performed by compiler.
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Default> Default for Box<T> {
/// Creates a `Box<T>`, with the `Default` value for T.
Expand Down
1 change: 0 additions & 1 deletion src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
27 changes: 3 additions & 24 deletions src/librustc/ty/contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ def_type_content_sets! {
// InteriorAll = 0b00000000__00000000__1111,

// Things that are owned by the value (second and third nibbles):
OwnsOwned = 0b0000_0000__0000_0001__0000,
OwnsDtor = 0b0000_0000__0000_0010__0000,
OwnsAll = 0b0000_0000__1111_1111__0000,

// Things that mean drop glue is necessary
NeedsDrop = 0b0000_0000__0000_0111__0000,
// OwnsAll = 0b0000_0000__1111_1111__0000,

// All bits
All = 0b1111_1111__1111_1111__1111
Expand All @@ -77,10 +73,6 @@ impl TypeContents {
(self.bits & tc.bits) != 0
}

pub fn owns_owned(&self) -> bool {
self.intersects(TC::OwnsOwned)
}

pub fn interior_param(&self) -> bool {
self.intersects(TC::InteriorParam)
}
Expand All @@ -90,12 +82,7 @@ impl TypeContents {
}

pub fn needs_drop(&self, _: TyCtxt) -> bool {
self.intersects(TC::NeedsDrop)
}

/// Includes only those bits that still apply when indirected through a `Box` pointer
pub fn owned_pointer(&self) -> TypeContents {
TC::OwnsOwned | (*self & TC::OwnsAll)
self.intersects(TC::OwnsDtor)
}

pub fn union<I, T, F>(v: I, mut f: F) -> TypeContents where
Expand All @@ -104,10 +91,6 @@ impl TypeContents {
{
v.into_iter().fold(TC::None, |tc, ty| tc | f(ty))
}

pub fn has_dtor(&self) -> bool {
self.intersects(TC::OwnsDtor)
}
}

impl ops::BitOr for TypeContents {
Expand Down Expand Up @@ -191,10 +174,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 @@ -237,7 +216,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {

if def.is_union() {
// unions don't have destructors regardless of the child types
res = res - TC::NeedsDrop;
res = res - TC::OwnsDtor;
}

if def.has_dtor() {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 8e9e055

Please sign in to comment.