Skip to content

Commit 8e9e055

Browse files
committed
Auto merge of #39230 - petrochenkov:nobox, r=eddyb
Refactoring TyBox -> TyAdt r? @eddyb cc @Manishearth
2 parents 8921707 + 93e3f63 commit 8e9e055

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+328
-396
lines changed

Diff for: src/liballoc/boxed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct ExchangeHeapSingleton {
103103
///
104104
/// See the [module-level documentation](../../std/boxed/index.html) for more.
105105
#[lang = "owned_box"]
106+
#[fundamental]
106107
#[stable(feature = "rust1", since = "1.0.0")]
107108
pub struct Box<T: ?Sized>(Unique<T>);
108109

@@ -292,6 +293,14 @@ impl<T: ?Sized> Box<T> {
292293
}
293294
}
294295

296+
#[cfg(not(stage0))]
297+
#[stable(feature = "rust1", since = "1.0.0")]
298+
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
299+
fn drop(&mut self) {
300+
// FIXME: Do nothing, drop is currently performed by compiler.
301+
}
302+
}
303+
295304
#[stable(feature = "rust1", since = "1.0.0")]
296305
impl<T: Default> Default for Box<T> {
297306
/// Creates a `Box<T>`, with the `Default` value for T.

Diff for: src/librustc/infer/freshen.rs

-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
156156
ty::TyUint(..) |
157157
ty::TyFloat(..) |
158158
ty::TyAdt(..) |
159-
ty::TyBox(..) |
160159
ty::TyStr |
161160
ty::TyError |
162161
ty::TyArray(..) |

Diff for: src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
961961
-> cmt<'tcx>
962962
{
963963
let ptr = match base_cmt.ty.sty {
964-
ty::TyBox(..) => Unique,
964+
ty::TyAdt(def, ..) if def.is_box() => Unique,
965965
ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
966966
ty::TyRef(r, mt) => {
967967
let bk = ty::BorrowKind::from_mutbl(mt.mutbl);

Diff for: src/librustc/traits/coherence.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn orphan_check_trait_ref<'tcx>(tcx: TyCtxt,
199199

200200
fn uncovered_tys<'tcx>(tcx: TyCtxt, ty: Ty<'tcx>, infer_is_local: InferIsLocal)
201201
-> Vec<Ty<'tcx>> {
202-
if ty_is_local_constructor(tcx, ty, infer_is_local) {
202+
if ty_is_local_constructor(ty, infer_is_local) {
203203
vec![]
204204
} else if fundamental_ty(tcx, ty) {
205205
ty.walk_shallow()
@@ -219,13 +219,13 @@ fn is_type_parameter(ty: Ty) -> bool {
219219
}
220220

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

226226
fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
227227
match ty.sty {
228-
ty::TyBox(..) | ty::TyRef(..) => true,
228+
ty::TyRef(..) => true,
229229
ty::TyAdt(def, _) => def.is_fundamental(),
230230
ty::TyDynamic(ref data, ..) => {
231231
data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental"))
@@ -234,7 +234,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
234234
}
235235
}
236236

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

240240
match ty.sty {
@@ -265,11 +265,6 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
265265
def.did.is_local()
266266
}
267267

268-
ty::TyBox(_) => { // Box<T>
269-
let krate = tcx.lang_items.owned_box().map(|d| d.krate);
270-
krate == Some(LOCAL_CRATE)
271-
}
272-
273268
ty::TyDynamic(ref tt, ..) => {
274269
tt.principal().map_or(false, |p| p.def_id().is_local())
275270
}

Diff for: src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
154154
ty::TyStr => Some(2),
155155
ty::TyInt(..) | ty::TyUint(..) | ty::TyInfer(ty::IntVar(..)) => Some(3),
156156
ty::TyFloat(..) | ty::TyInfer(ty::FloatVar(..)) => Some(4),
157-
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
157+
ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
158158
ty::TyArray(..) | ty::TySlice(..) => Some(6),
159159
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(7),
160160
ty::TyDynamic(..) => Some(8),

Diff for: src/librustc/traits/select.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17351735
ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) |
17361736
ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
17371737
ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyRawPtr(..) |
1738-
ty::TyChar | ty::TyBox(_) | ty::TyRef(..) |
1738+
ty::TyChar | ty::TyRef(..) |
17391739
ty::TyArray(..) | ty::TyClosure(..) | ty::TyNever |
17401740
ty::TyError => {
17411741
// safe for everything
@@ -1788,7 +1788,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17881788
Where(ty::Binder(Vec::new()))
17891789
}
17901790

1791-
ty::TyBox(_) | ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
1791+
ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
17921792
ty::TyClosure(..) |
17931793
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
17941794
Never
@@ -1865,10 +1865,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
18651865
t);
18661866
}
18671867

1868-
ty::TyBox(referent_ty) => { // Box<T>
1869-
vec![referent_ty]
1870-
}
1871-
18721868
ty::TyRawPtr(ty::TypeAndMut { ty: element_ty, ..}) |
18731869
ty::TyRef(_, ty::TypeAndMut { ty: element_ty, ..}) => {
18741870
vec![element_ty]

Diff for: src/librustc/ty/contents.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ def_type_content_sets! {
5656
// InteriorAll = 0b00000000__00000000__1111,
5757

5858
// Things that are owned by the value (second and third nibbles):
59-
OwnsOwned = 0b0000_0000__0000_0001__0000,
6059
OwnsDtor = 0b0000_0000__0000_0010__0000,
61-
OwnsAll = 0b0000_0000__1111_1111__0000,
62-
63-
// Things that mean drop glue is necessary
64-
NeedsDrop = 0b0000_0000__0000_0111__0000,
60+
// OwnsAll = 0b0000_0000__1111_1111__0000,
6561

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

80-
pub fn owns_owned(&self) -> bool {
81-
self.intersects(TC::OwnsOwned)
82-
}
83-
8476
pub fn interior_param(&self) -> bool {
8577
self.intersects(TC::InteriorParam)
8678
}
@@ -90,12 +82,7 @@ impl TypeContents {
9082
}
9183

9284
pub fn needs_drop(&self, _: TyCtxt) -> bool {
93-
self.intersects(TC::NeedsDrop)
94-
}
95-
96-
/// Includes only those bits that still apply when indirected through a `Box` pointer
97-
pub fn owned_pointer(&self) -> TypeContents {
98-
TC::OwnsOwned | (*self & TC::OwnsAll)
85+
self.intersects(TC::OwnsDtor)
9986
}
10087

10188
pub fn union<I, T, F>(v: I, mut f: F) -> TypeContents where
@@ -104,10 +91,6 @@ impl TypeContents {
10491
{
10592
v.into_iter().fold(TC::None, |tc, ty| tc | f(ty))
10693
}
107-
108-
pub fn has_dtor(&self) -> bool {
109-
self.intersects(TC::OwnsDtor)
110-
}
11194
}
11295

11396
impl ops::BitOr for TypeContents {
@@ -191,10 +174,6 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
191174
TC::None
192175
}
193176

194-
ty::TyBox(typ) => {
195-
tc_ty(tcx, typ, cache).owned_pointer()
196-
}
197-
198177
ty::TyDynamic(..) => {
199178
TC::All - TC::InteriorParam
200179
}
@@ -237,7 +216,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
237216

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

243222
if def.has_dtor() {

Diff for: src/librustc/ty/context.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1919
use hir::map as hir_map;
2020
use hir::map::DisambiguatedDefPathData;
2121
use middle::free_region::FreeRegionMap;
22+
use middle::lang_items;
2223
use middle::region::RegionMaps;
2324
use middle::resolve_lifetime;
2425
use middle::stability;
@@ -1088,7 +1089,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
10881089
pub fn print_debug_stats(self) {
10891090
sty_debug_print!(
10901091
self,
1091-
TyAdt, TyBox, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
1092+
TyAdt, TyArray, TySlice, TyRawPtr, TyRef, TyFnDef, TyFnPtr,
10921093
TyDynamic, TyClosure, TyTuple, TyParam, TyInfer, TyProjection, TyAnon);
10931094

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

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

13421346
pub fn mk_ptr(self, tm: TypeAndMut<'tcx>) -> Ty<'tcx> {

Diff for: src/librustc/ty/error.rs

-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
181181
ty::TyTuple(ref tys) if tys.is_empty() => self.to_string(),
182182

183183
ty::TyAdt(def, _) => format!("{} `{}`", def.descr(), tcx.item_path_str(def.did)),
184-
ty::TyBox(_) => "box".to_string(),
185184
ty::TyArray(_, n) => format!("array of {} elements", n),
186185
ty::TySlice(_) => "slice".to_string(),
187186
ty::TyRawPtr(_) => "*-ptr".to_string(),

Diff for: src/librustc/ty/fast_reject.rs

-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use hir::def_id::DefId;
1212
use ty::{self, Ty, TyCtxt};
1313
use syntax::ast;
14-
use middle::lang_items::OwnedBoxLangItem;
1514

1615
use self::SimplifiedType::*;
1716

@@ -69,10 +68,6 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
6968
// view of possibly unifying
7069
simplify_type(tcx, mt.ty, can_simplify_params)
7170
}
72-
ty::TyBox(_) => {
73-
// treat like we would treat `Box`
74-
Some(AdtSimplifiedType(tcx.require_lang_item(OwnedBoxLangItem)))
75-
}
7671
ty::TyClosure(def_id, _) => {
7772
Some(ClosureSimplifiedType(def_id))
7873
}

Diff for: src/librustc/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl FlagComputation {
138138
self.add_region(r);
139139
}
140140

141-
&ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
141+
&ty::TyArray(tt, _) | &ty::TySlice(tt) => {
142142
self.add_ty(tt)
143143
}
144144

Diff for: src/librustc/ty/item_path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
314314
ty::TyDynamic(data, ..) => data.principal().map(|p| p.def_id()),
315315

316316
ty::TyArray(subty, _) |
317-
ty::TySlice(subty) |
318-
ty::TyBox(subty) => characteristic_def_id_of_type(subty),
317+
ty::TySlice(subty) => characteristic_def_id_of_type(subty),
319318

320319
ty::TyRawPtr(mt) |
321320
ty::TyRef(_, mt) => characteristic_def_id_of_type(mt.ty),

Diff for: src/librustc/ty/layout.rs

+44-33
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,23 @@ impl<'a, 'gcx, 'tcx> Layout {
10531053
let dl = &tcx.data_layout;
10541054
assert!(!ty.has_infer_types());
10551055

1056+
let ptr_layout = |pointee: Ty<'gcx>| {
1057+
let non_zero = !ty.is_unsafe_ptr();
1058+
let pointee = normalize_associated_type(infcx, pointee);
1059+
if pointee.is_sized(tcx, &infcx.parameter_environment, DUMMY_SP) {
1060+
Ok(Scalar { value: Pointer, non_zero: non_zero })
1061+
} else {
1062+
let unsized_part = tcx.struct_tail(pointee);
1063+
let meta = match unsized_part.sty {
1064+
ty::TySlice(_) | ty::TyStr => {
1065+
Int(dl.ptr_sized_integer())
1066+
}
1067+
ty::TyDynamic(..) => Pointer,
1068+
_ => return Err(LayoutError::Unknown(unsized_part))
1069+
};
1070+
Ok(FatPointer { metadata: meta, non_zero: non_zero })
1071+
}
1072+
};
10561073

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

10841101
// Potentially-fat pointers.
1085-
ty::TyBox(pointee) |
10861102
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
10871103
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
1088-
let non_zero = !ty.is_unsafe_ptr();
1089-
let pointee = normalize_associated_type(infcx, pointee);
1090-
if pointee.is_sized(tcx, &infcx.parameter_environment, DUMMY_SP) {
1091-
Scalar { value: Pointer, non_zero: non_zero }
1092-
} else {
1093-
let unsized_part = tcx.struct_tail(pointee);
1094-
let meta = match unsized_part.sty {
1095-
ty::TySlice(_) | ty::TyStr => {
1096-
Int(dl.ptr_sized_integer())
1097-
}
1098-
ty::TyDynamic(..) => Pointer,
1099-
_ => return Err(LayoutError::Unknown(unsized_part))
1100-
};
1101-
FatPointer { metadata: meta, non_zero: non_zero }
1102-
}
1104+
ptr_layout(pointee)?
1105+
}
1106+
ty::TyAdt(def, _) if def.is_box() => {
1107+
ptr_layout(ty.boxed_ty())?
11031108
}
11041109

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

1568+
let ptr_skeleton = |pointee: Ty<'gcx>| {
1569+
let non_zero = !ty.is_unsafe_ptr();
1570+
let tail = tcx.struct_tail(pointee);
1571+
match tail.sty {
1572+
ty::TyParam(_) | ty::TyProjection(_) => {
1573+
assert!(tail.has_param_types() || tail.has_self_ty());
1574+
Ok(SizeSkeleton::Pointer {
1575+
non_zero: non_zero,
1576+
tail: tcx.erase_regions(&tail)
1577+
})
1578+
}
1579+
_ => {
1580+
bug!("SizeSkeleton::compute({}): layout errored ({}), yet \
1581+
tail `{}` is not a type parameter or a projection",
1582+
ty, err, tail)
1583+
}
1584+
}
1585+
};
1586+
15631587
match ty.sty {
1564-
ty::TyBox(pointee) |
15651588
ty::TyRef(_, ty::TypeAndMut { ty: pointee, .. }) |
15661589
ty::TyRawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
1567-
let non_zero = !ty.is_unsafe_ptr();
1568-
let tail = tcx.struct_tail(pointee);
1569-
match tail.sty {
1570-
ty::TyParam(_) | ty::TyProjection(_) => {
1571-
assert!(tail.has_param_types() || tail.has_self_ty());
1572-
Ok(SizeSkeleton::Pointer {
1573-
non_zero: non_zero,
1574-
tail: tcx.erase_regions(&tail)
1575-
})
1576-
}
1577-
_ => {
1578-
bug!("SizeSkeleton::compute({}): layout errored ({}), yet \
1579-
tail `{}` is not a type parameter or a projection",
1580-
ty, err, tail)
1581-
}
1582-
}
1590+
ptr_skeleton(pointee)
1591+
}
1592+
ty::TyAdt(def, _) if def.is_box() => {
1593+
ptr_skeleton(ty.boxed_ty())
15831594
}
15841595

15851596
ty::TyAdt(def, substs) => {

0 commit comments

Comments
 (0)