Skip to content

Commit 4b25f08

Browse files
committed
rustc: move trait objects from TraitRef to ExistentialTraitRef.
1 parent 5ef6af0 commit 4b25f08

39 files changed

+682
-792
lines changed

src/librustc/infer/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,9 +1248,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12481248
generics: &ty::Generics<'tcx>)
12491249
-> &'tcx subst::Substs<'tcx>
12501250
{
1251-
let type_defs = generics.types.as_full_slice();
1252-
let region_defs = generics.regions.as_full_slice();
1253-
let substs = Substs::from_param_defs(region_defs, type_defs, |def| {
1251+
let substs = Substs::from_generics(generics, |def, _| {
12541252
self.region_var_for_def(span, def)
12551253
}, |def, substs| {
12561254
self.type_var_for_def(span, def, substs)
@@ -1271,9 +1269,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12711269
assert!(generics.types.len(subst::SelfSpace) == 1);
12721270
assert!(generics.types.len(subst::FnSpace) == 0);
12731271

1274-
let type_defs = generics.types.as_full_slice();
1275-
let region_defs = generics.regions.as_full_slice();
1276-
let substs = Substs::from_param_defs(region_defs, type_defs, |def| {
1272+
let substs = Substs::from_generics(generics, |def, _| {
12771273
self.region_var_for_def(span, def)
12781274
}, |def, substs| {
12791275
if def.space == subst::SelfSpace {

src/librustc/traits/coherence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
231231
ty::TyEnum(def, _) | ty::TyStruct(def, _) =>
232232
def.is_fundamental(),
233233
ty::TyTrait(ref data) =>
234-
tcx.has_attr(data.principal_def_id(), "fundamental"),
234+
tcx.has_attr(data.principal.def_id(), "fundamental"),
235235
_ =>
236236
false
237237
}
@@ -275,7 +275,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
275275
}
276276

277277
ty::TyTrait(ref tt) => {
278-
tt.principal_def_id().is_local()
278+
tt.principal.def_id().is_local()
279279
}
280280

281281
ty::TyError => {

src/librustc/traits/project.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,10 +1133,9 @@ fn confirm_object_candidate<'cx, 'gcx, 'tcx>(
11331133
object_ty)
11341134
}
11351135
};
1136-
let projection_bounds = data.projection_bounds_with_self_ty(selcx.tcx(), object_ty);
1137-
let env_predicates = projection_bounds.iter()
1138-
.map(|p| p.to_predicate())
1139-
.collect();
1136+
let env_predicates = data.projection_bounds.iter().map(|p| {
1137+
p.with_self_ty(selcx.tcx(), object_ty).to_predicate()
1138+
}).collect();
11401139
let env_predicate = {
11411140
let env_predicates = elaborate_predicates(selcx.tcx(), env_predicates);
11421141

src/librustc/traits/select.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15281528
ty::TyTrait(ref data) => {
15291529
match this.tcx().lang_items.to_builtin_kind(obligation.predicate.def_id()) {
15301530
Some(bound @ ty::BoundSend) | Some(bound @ ty::BoundSync) => {
1531-
if data.bounds.builtin_bounds.contains(&bound) {
1531+
if data.builtin_bounds.contains(&bound) {
15321532
debug!("assemble_candidates_from_object_ty: matched builtin bound, \
15331533
pushing candidate");
15341534
candidates.vec.push(BuiltinObjectCandidate);
@@ -1538,7 +1538,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15381538
_ => {}
15391539
}
15401540

1541-
data.principal_trait_ref_with_self_ty(this.tcx(), self_ty)
1541+
data.principal.with_self_ty(this.tcx(), self_ty)
15421542
}
15431543
ty::TyInfer(ty::TyVar(_)) => {
15441544
debug!("assemble_candidates_from_object_ty: ambiguous");
@@ -1622,7 +1622,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16221622
// We always upcast when we can because of reason
16231623
// #2 (region bounds).
16241624
data_a.principal.def_id() == data_a.principal.def_id() &&
1625-
data_a.bounds.builtin_bounds.is_superset(&data_b.bounds.builtin_bounds)
1625+
data_a.builtin_bounds.is_superset(&data_b.builtin_bounds)
16261626
}
16271627

16281628
// T -> Trait.
@@ -2179,10 +2179,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21792179
match self_ty.sty {
21802180
ty::TyTrait(ref data) => {
21812181
// OK to skip the binder, it is reintroduced below
2182-
let input_types = data.principal.skip_binder().substs.types.get_slice(TypeSpace);
2183-
let assoc_types = data.bounds.projection_bounds
2184-
.iter()
2185-
.map(|pb| pb.skip_binder().ty);
2182+
let input_types = data.principal.skip_binder().input_types();
2183+
let assoc_types = data.projection_bounds.iter()
2184+
.map(|pb| pb.skip_binder().ty);
21862185
let all_types: Vec<_> = input_types.iter().cloned()
21872186
.chain(assoc_types)
21882187
.collect();
@@ -2315,7 +2314,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23152314
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
23162315
let poly_trait_ref = match self_ty.sty {
23172316
ty::TyTrait(ref data) => {
2318-
data.principal_trait_ref_with_self_ty(self.tcx(), self_ty)
2317+
data.principal.with_self_ty(self.tcx(), self_ty)
23192318
}
23202319
_ => {
23212320
span_bug!(obligation.cause.span,
@@ -2487,13 +2486,12 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24872486
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
24882487
(&ty::TyTrait(ref data_a), &ty::TyTrait(ref data_b)) => {
24892488
// See assemble_candidates_for_unsizing for more info.
2490-
let bounds = ty::ExistentialBounds {
2491-
region_bound: data_b.bounds.region_bound,
2492-
builtin_bounds: data_b.bounds.builtin_bounds,
2493-
projection_bounds: data_a.bounds.projection_bounds.clone(),
2494-
};
2495-
2496-
let new_trait = tcx.mk_trait(data_a.principal.clone(), bounds);
2489+
let new_trait = tcx.mk_trait(ty::TraitObject {
2490+
principal: data_a.principal,
2491+
region_bound: data_b.region_bound,
2492+
builtin_bounds: data_b.builtin_bounds,
2493+
projection_bounds: data_a.projection_bounds.clone(),
2494+
});
24972495
let origin = TypeOrigin::Misc(obligation.cause.span);
24982496
let InferOk { obligations, .. } =
24992497
self.infcx.sub_types(false, origin, new_trait, target)
@@ -2504,21 +2502,21 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25042502
let cause = ObligationCause::new(obligation.cause.span,
25052503
obligation.cause.body_id,
25062504
ObjectCastObligation(target));
2507-
let outlives = ty::OutlivesPredicate(data_a.bounds.region_bound,
2508-
data_b.bounds.region_bound);
2505+
let outlives = ty::OutlivesPredicate(data_a.region_bound,
2506+
data_b.region_bound);
25092507
nested.push(Obligation::with_depth(cause,
25102508
obligation.recursion_depth + 1,
25112509
ty::Binder(outlives).to_predicate()));
25122510
}
25132511

25142512
// T -> Trait.
25152513
(_, &ty::TyTrait(ref data)) => {
2516-
let mut object_dids = Some(data.principal_def_id()).into_iter();
2514+
let mut object_dids = Some(data.principal.def_id()).into_iter();
25172515
// FIXME(#33243)
2518-
// data.bounds.builtin_bounds.iter().flat_map(|bound| {
2516+
// data.builtin_bounds.iter().flat_map(|bound| {
25192517
// tcx.lang_items.from_builtin_kind(bound).ok()
25202518
// })
2521-
// .chain(Some(data.principal_def_id()));
2519+
// .chain(Some(data.principal.def_id()));
25222520
if let Some(did) = object_dids.find(|did| {
25232521
!tcx.is_object_safe(*did)
25242522
}) {
@@ -2535,10 +2533,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25352533
};
25362534

25372535
// Create the obligation for casting from T to Trait.
2538-
push(data.principal_trait_ref_with_self_ty(tcx, source).to_predicate());
2536+
push(data.principal.with_self_ty(tcx, source).to_predicate());
25392537

25402538
// We can only make objects from sized types.
2541-
let mut builtin_bounds = data.bounds.builtin_bounds;
2539+
let mut builtin_bounds = data.builtin_bounds;
25422540
builtin_bounds.insert(ty::BoundSized);
25432541

25442542
// Create additional obligations for all the various builtin
@@ -2554,14 +2552,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25542552
}
25552553

25562554
// Create obligations for the projection predicates.
2557-
for bound in data.projection_bounds_with_self_ty(tcx, source) {
2558-
push(bound.to_predicate());
2555+
for bound in &data.projection_bounds {
2556+
push(bound.with_self_ty(tcx, source).to_predicate());
25592557
}
25602558

25612559
// If the type is `Foo+'a`, ensures that the type
25622560
// being cast to `Foo+'a` outlives `'a`:
2563-
let outlives = ty::OutlivesPredicate(source,
2564-
data.bounds.region_bound);
2561+
let outlives = ty::OutlivesPredicate(source, data.region_bound);
25652562
push(ty::Binder(outlives).to_predicate());
25662563
}
25672564

src/librustc/ty/context.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use ty::subst::{self, Substs};
2727
use traits;
2828
use ty::{self, TraitRef, Ty, TypeAndMut};
2929
use ty::{TyS, TypeVariants};
30-
use ty::{AdtDef, ClosureSubsts, ExistentialBounds, Region};
30+
use ty::{AdtDef, ClosureSubsts, Region};
3131
use hir::FreevarMap;
32-
use ty::{BareFnTy, InferTy, ParamTy, ProjectionTy, TraitTy};
32+
use ty::{BareFnTy, InferTy, ParamTy, ProjectionTy, TraitObject};
3333
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
3434
use ty::TypeVariants::*;
3535
use ty::layout::{Layout, TargetDataLayout};
@@ -1150,12 +1150,6 @@ impl_interners!('tcx,
11501150
region: mk_region(Region, keep_local) -> Region
11511151
);
11521152

1153-
fn bound_list_is_sorted(bounds: &[ty::PolyProjectionPredicate]) -> bool {
1154-
bounds.is_empty() ||
1155-
bounds[1..].iter().enumerate().all(
1156-
|(index, bound)| bounds[index].sort_key() <= bound.sort_key())
1157-
}
1158-
11591153
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11601154
/// Create an unsafe fn ty based on a safe fn ty.
11611155
pub fn safe_to_unsafe_fn_ty(self, bare_fn: &BareFnTy<'tcx>) -> Ty<'tcx> {
@@ -1288,18 +1282,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12881282
self.mk_ty(TyFnPtr(fty))
12891283
}
12901284

1291-
pub fn mk_trait(self,
1292-
principal: ty::PolyTraitRef<'tcx>,
1293-
bounds: ExistentialBounds<'tcx>)
1294-
-> Ty<'tcx>
1295-
{
1296-
assert!(bound_list_is_sorted(&bounds.projection_bounds));
1297-
1298-
let inner = box TraitTy {
1299-
principal: principal,
1300-
bounds: bounds
1301-
};
1302-
self.mk_ty(TyTrait(inner))
1285+
pub fn mk_trait(self, mut obj: TraitObject<'tcx>) -> Ty<'tcx> {
1286+
obj.projection_bounds.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
1287+
self.mk_ty(TyTrait(box obj))
13031288
}
13041289

13051290
pub fn mk_projection(self,

src/librustc/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
243243
ty::TyFnDef(..) => format!("fn item"),
244244
ty::TyFnPtr(_) => "fn pointer".to_string(),
245245
ty::TyTrait(ref inner) => {
246-
format!("trait {}", tcx.item_path_str(inner.principal_def_id()))
246+
format!("trait {}", tcx.item_path_str(inner.principal.def_id()))
247247
}
248248
ty::TyStruct(def, _) => {
249249
format!("struct `{}`", tcx.item_path_str(def.did))

src/librustc/ty/fast_reject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
6161
ty::TyArray(..) | ty::TySlice(_) => Some(VecSimplifiedType),
6262
ty::TyRawPtr(_) => Some(PtrSimplifiedType),
6363
ty::TyTrait(ref trait_info) => {
64-
Some(TraitSimplifiedType(trait_info.principal_def_id()))
64+
Some(TraitSimplifiedType(trait_info.principal.def_id()))
6565
}
6666
ty::TyStruct(def, _) => {
6767
Some(StructSimplifiedType(def.did))

src/librustc/ty/flags.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,16 @@ impl FlagComputation {
116116
self.add_substs(substs);
117117
}
118118

119-
&ty::TyTrait(box ty::TraitTy { ref principal, ref bounds }) => {
119+
&ty::TyTrait(ref obj) => {
120120
let mut computation = FlagComputation::new();
121-
computation.add_substs(principal.0.substs);
122-
for projection_bound in &bounds.projection_bounds {
121+
computation.add_substs(obj.principal.skip_binder().substs);
122+
for projection_bound in &obj.projection_bounds {
123123
let mut proj_computation = FlagComputation::new();
124-
proj_computation.add_projection_predicate(&projection_bound.0);
124+
proj_computation.add_existential_projection(&projection_bound.0);
125125
self.add_bound_computation(&proj_computation);
126126
}
127127
self.add_bound_computation(&computation);
128-
129-
self.add_bounds(bounds);
128+
self.add_region(obj.region_bound);
130129
}
131130

132131
&ty::TyBox(tt) | &ty::TyArray(tt, _) | &ty::TySlice(tt) => {
@@ -199,9 +198,9 @@ impl FlagComputation {
199198
}
200199
}
201200

202-
fn add_projection_predicate(&mut self, projection_predicate: &ty::ProjectionPredicate) {
203-
self.add_projection_ty(&projection_predicate.projection_ty);
204-
self.add_ty(projection_predicate.ty);
201+
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection) {
202+
self.add_substs(projection.trait_ref.substs);
203+
self.add_ty(projection.ty);
205204
}
206205

207206
fn add_projection_ty(&mut self, projection_ty: &ty::ProjectionTy) {
@@ -214,8 +213,4 @@ impl FlagComputation {
214213
self.add_region(r);
215214
}
216215
}
217-
218-
fn add_bounds(&mut self, bounds: &ty::ExistentialBounds) {
219-
self.add_region(bounds.region_bound);
220-
}
221216
}

src/librustc/ty/fold.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ pub trait TypeFolder<'gcx: 'tcx, 'tcx> : Sized {
140140
t.super_fold_with(self)
141141
}
142142

143-
fn fold_trait_ref(&mut self, t: &ty::TraitRef<'tcx>) -> ty::TraitRef<'tcx> {
144-
t.super_fold_with(self)
145-
}
146-
147143
fn fold_impl_header(&mut self, imp: &ty::ImplHeader<'tcx>) -> ty::ImplHeader<'tcx> {
148144
imp.super_fold_with(self)
149145
}
@@ -177,11 +173,6 @@ pub trait TypeFolder<'gcx: 'tcx, 'tcx> : Sized {
177173
r.super_fold_with(self)
178174
}
179175

180-
fn fold_existential_bounds(&mut self, s: &ty::ExistentialBounds<'tcx>)
181-
-> ty::ExistentialBounds<'tcx> {
182-
s.super_fold_with(self)
183-
}
184-
185176
fn fold_autoref(&mut self, ar: &adjustment::AutoRef<'tcx>)
186177
-> adjustment::AutoRef<'tcx> {
187178
ar.super_fold_with(self)

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
322322
ty::TyStruct(adt_def, _) |
323323
ty::TyEnum(adt_def, _) => Some(adt_def.did),
324324

325-
ty::TyTrait(ref data) => Some(data.principal_def_id()),
325+
ty::TyTrait(ref data) => Some(data.principal.def_id()),
326326

327327
ty::TyArray(subty, _) |
328328
ty::TySlice(subty) |

src/librustc/ty/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ use hir::{ItemImpl, ItemTrait, PatKind};
5454
use hir::intravisit::Visitor;
5555

5656
pub use self::sty::{Binder, DebruijnIndex};
57-
pub use self::sty::{BuiltinBound, BuiltinBounds, ExistentialBounds};
57+
pub use self::sty::{BuiltinBound, BuiltinBounds};
5858
pub use self::sty::{BareFnTy, FnSig, PolyFnSig};
59-
pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitTy};
59+
pub use self::sty::{ClosureTy, InferTy, ParamTy, ProjectionTy, TraitObject};
6060
pub use self::sty::{ClosureSubsts, TypeAndMut};
6161
pub use self::sty::{TraitRef, TypeVariants, PolyTraitRef};
62+
pub use self::sty::{ExistentialTraitRef, PolyExistentialTraitRef};
63+
pub use self::sty::{ExistentialProjection, PolyExistentialProjection};
6264
pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
6365
pub use self::sty::Issue32330;
6466
pub use self::sty::{TyVid, IntVid, FloatVid, RegionVid, SkolemizedRegionVid};

0 commit comments

Comments
 (0)