Skip to content

Commit bb35d50

Browse files
Refactor TyTrait to contain a interned ExistentialPredicate slice.
Renames TyTrait to TyDynamic.
1 parent 64e97d9 commit bb35d50

Some content is hidden

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

59 files changed

+614
-515
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ DEPS_rustc_trans := arena flate getopts graphviz libc rustc rustc_back \
140140
DEPS_rustc_incremental := rustc syntax_pos serialize rustc_data_structures
141141
DEPS_rustc_save_analysis := rustc log syntax syntax_pos serialize
142142
DEPS_rustc_typeck := rustc syntax syntax_pos rustc_platform_intrinsics rustc_const_math \
143-
rustc_const_eval rustc_errors
143+
rustc_const_eval rustc_errors rustc_data_structures
144144

145145
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts test \
146146
rustc_lint rustc_const_eval syntax_pos rustc_data_structures

src/librustc/infer/freshen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
165165
ty::TyRef(..) |
166166
ty::TyFnDef(..) |
167167
ty::TyFnPtr(_) |
168-
ty::TyTrait(..) |
168+
ty::TyDynamic(..) |
169169
ty::TyClosure(..) |
170170
ty::TyNever |
171171
ty::TyTuple(..) |

src/librustc/traits/coherence.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
227227
match ty.sty {
228228
ty::TyBox(..) | ty::TyRef(..) => true,
229229
ty::TyAdt(def, _) => def.is_fundamental(),
230-
ty::TyTrait(ref data) => {
230+
ty::TyDynamic(ref data, ..) => {
231231
data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental"))
232232
}
233233
_ => false
@@ -270,7 +270,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
270270
krate == Some(LOCAL_CRATE)
271271
}
272272

273-
ty::TyTrait(ref tt) => {
273+
ty::TyDynamic(ref tt, ..) => {
274274
tt.principal().map_or(false, |p| p.def_id().is_local())
275275
}
276276

src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
156156
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
157157
ty::TyArray(..) | ty::TySlice(..) => Some(6),
158158
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(7),
159-
ty::TyTrait(..) => Some(8),
159+
ty::TyDynamic(..) => Some(8),
160160
ty::TyClosure(..) => Some(9),
161161
ty::TyTuple(..) => Some(10),
162162
ty::TyProjection(..) => Some(11),

src/librustc/traits/fulfill.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
231231
}
232232

233233
pub fn register_bound(&mut self,
234-
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
235-
ty: Ty<'tcx>,
236-
def_id: DefId,
237-
cause: ObligationCause<'tcx>)
234+
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
235+
ty: Ty<'tcx>,
236+
def_id: DefId,
237+
cause: ObligationCause<'tcx>)
238238
{
239239
let trait_ref = ty::TraitRef {
240240
def_id: def_id,

src/librustc/traits/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>,
366366
/// conservative towards *no impl*, which is the opposite of the
367367
/// `evaluate` methods).
368368
pub fn type_known_to_meet_bound<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
369-
ty: Ty<'tcx>,
370-
def_id: DefId,
371-
span: Span)
372-
-> bool
369+
ty: Ty<'tcx>,
370+
def_id: DefId,
371+
span: Span)
372+
-> bool
373373
{
374374
debug!("type_known_to_meet_bound(ty={:?}, bound={:?})",
375375
ty,

src/librustc/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,15 @@ fn confirm_object_candidate<'cx, 'gcx, 'tcx>(
11231123
debug!("confirm_object_candidate(object_ty={:?})",
11241124
object_ty);
11251125
let data = match object_ty.sty {
1126-
ty::TyTrait(ref data) => data,
1126+
ty::TyDynamic(ref data, ..) => data,
11271127
_ => {
11281128
span_bug!(
11291129
obligation.cause.span,
11301130
"confirm_object_candidate called with non-object: {:?}",
11311131
object_ty)
11321132
}
11331133
};
1134-
let env_predicates = data.projection_bounds.iter().map(|p| {
1134+
let env_predicates = data.projection_bounds().map(|p| {
11351135
p.with_self_ty(selcx.tcx(), object_ty).to_predicate()
11361136
}).collect();
11371137
let env_predicate = {

src/librustc/traits/select.rs

+64-80
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use std::fmt;
5050
use std::marker::PhantomData;
5151
use std::mem;
5252
use std::rc::Rc;
53-
use std::iter;
5453
use syntax::abi::Abi;
5554
use hir;
5655
use util::nodemap::FxHashMap;
@@ -1094,38 +1093,30 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
10941093
// and applicable impls. There is a certain set of precedence rules here.
10951094

10961095
let def_id = obligation.predicate.def_id();
1097-
match obligation.predicate.def_id() {
1098-
_ if self.tcx().lang_items.copy_trait() == Some(def_id) => {
1099-
debug!("obligation self ty is {:?}",
1100-
obligation.predicate.0.self_ty());
1101-
1102-
// User-defined copy impls are permitted, but only for
1103-
// structs and enums.
1104-
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1105-
1106-
// For other types, we'll use the builtin rules.
1107-
let copy_conditions = self.copy_conditions(obligation);
1108-
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates)?;
1109-
}
1110-
_ if self.tcx().lang_items.sized_trait() == Some(def_id) => {
1111-
// Sized is never implementable by end-users, it is
1112-
// always automatically computed.
1113-
let sized_conditions = self.sized_conditions(obligation);
1114-
self.assemble_builtin_bound_candidates(sized_conditions,
1115-
&mut candidates)?;
1116-
}
1117-
1118-
_ if self.tcx().lang_items.unsize_trait() == Some(def_id) => {
1119-
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
1120-
}
1121-
1122-
// For non-builtins and Send/Sync
1123-
_ => {
1124-
self.assemble_closure_candidates(obligation, &mut candidates)?;
1125-
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
1126-
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1127-
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
1128-
}
1096+
if self.tcx().lang_items.copy_trait() == Some(def_id) {
1097+
debug!("obligation self ty is {:?}",
1098+
obligation.predicate.0.self_ty());
1099+
1100+
// User-defined copy impls are permitted, but only for
1101+
// structs and enums.
1102+
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1103+
1104+
// For other types, we'll use the builtin rules.
1105+
let copy_conditions = self.copy_conditions(obligation);
1106+
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates)?;
1107+
} else if self.tcx().lang_items.sized_trait() == Some(def_id) {
1108+
// Sized is never implementable by end-users, it is
1109+
// always automatically computed.
1110+
let sized_conditions = self.sized_conditions(obligation);
1111+
self.assemble_builtin_bound_candidates(sized_conditions,
1112+
&mut candidates)?;
1113+
} else if self.tcx().lang_items.unsize_trait() == Some(def_id) {
1114+
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
1115+
} else {
1116+
self.assemble_closure_candidates(obligation, &mut candidates)?;
1117+
self.assemble_fn_pointer_candidates(obligation, &mut candidates)?;
1118+
self.assemble_candidates_from_impls(obligation, &mut candidates)?;
1119+
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
11291120
}
11301121

11311122
self.assemble_candidates_from_projected_tys(obligation, &mut candidates);
@@ -1446,7 +1437,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
14461437

14471438
if self.tcx().trait_has_default_impl(def_id) {
14481439
match self_ty.sty {
1449-
ty::TyTrait(..) => {
1440+
ty::TyDynamic(..) => {
14501441
// For object types, we don't know what the closed
14511442
// over types are. For most traits, this means we
14521443
// conservatively say nothing; a candidate may be
@@ -1516,7 +1507,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15161507
// any LBR.
15171508
let self_ty = this.tcx().erase_late_bound_regions(&obligation.self_ty());
15181509
let poly_trait_ref = match self_ty.sty {
1519-
ty::TyTrait(ref data) => {
1510+
ty::TyDynamic(ref data, ..) => {
15201511
if data.auto_traits().any(|did| did == obligation.predicate.def_id()) {
15211512
debug!("assemble_candidates_from_object_ty: matched builtin bound, \
15221513
pushing candidate");
@@ -1525,7 +1516,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15251516
}
15261517

15271518
match data.principal() {
1528-
Some(ref p) => p.with_self_ty(this.tcx(), self_ty),
1519+
Some(p) => p.with_self_ty(this.tcx(), self_ty),
15291520
None => return,
15301521
}
15311522
}
@@ -1598,7 +1589,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15981589

15991590
let may_apply = match (&source.sty, &target.sty) {
16001591
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
1601-
(&ty::TyTrait(ref data_a), &ty::TyTrait(ref data_b)) => {
1592+
(&ty::TyDynamic(ref data_a, ..), &ty::TyDynamic(ref data_b, ..)) => {
16021593
// Upcasts permit two things:
16031594
//
16041595
// 1. Dropping builtin bounds, e.g. `Foo+Send` to `Foo`
@@ -1611,7 +1602,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16111602
// We always upcast when we can because of reason
16121603
// #2 (region bounds).
16131604
match (data_a.principal(), data_b.principal()) {
1614-
(Some(ref a), Some(ref b)) => a.def_id() == b.def_id() &&
1605+
(Some(a), Some(b)) => a.def_id() == b.def_id() &&
16151606
data_b.auto_traits()
16161607
// All of a's auto traits need to be in b's auto traits.
16171608
.all(|b| data_a.auto_traits().any(|a| a == b)),
@@ -1620,7 +1611,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16201611
}
16211612

16221613
// T -> Trait.
1623-
(_, &ty::TyTrait(_)) => true,
1614+
(_, &ty::TyDynamic(..)) => true,
16241615

16251616
// Ambiguous handling is below T -> Trait, because inference
16261617
// variables can still implement Unsize<Trait> and nested
@@ -1772,7 +1763,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17721763
Where(ty::Binder(Vec::new()))
17731764
}
17741765

1775-
ty::TyStr | ty::TySlice(_) | ty::TyTrait(..) => Never,
1766+
ty::TyStr | ty::TySlice(_) | ty::TyDynamic(..) => Never,
17761767

17771768
ty::TyTuple(tys) => {
17781769
Where(ty::Binder(tys.last().into_iter().cloned().collect()))
@@ -1818,7 +1809,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
18181809
Where(ty::Binder(Vec::new()))
18191810
}
18201811

1821-
ty::TyBox(_) | ty::TyTrait(..) | ty::TyStr | ty::TySlice(..) |
1812+
ty::TyBox(_) | ty::TyDynamic(..) | ty::TyStr | ty::TySlice(..) |
18221813
ty::TyClosure(..) |
18231814
ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
18241815
Never
@@ -1883,7 +1874,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
18831874
Vec::new()
18841875
}
18851876

1886-
ty::TyTrait(..) |
1877+
ty::TyDynamic(..) |
18871878
ty::TyParam(..) |
18881879
ty::TyProjection(..) |
18891880
ty::TyAnon(..) |
@@ -2169,11 +2160,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21692160
// OK to skip binder, it is reintroduced below
21702161
let self_ty = self.infcx.shallow_resolve(obligation.predicate.skip_binder().self_ty());
21712162
match self_ty.sty {
2172-
ty::TyTrait(ref data) => {
2163+
ty::TyDynamic(ref data, ..) => {
21732164
// OK to skip the binder, it is reintroduced below
21742165
let principal = data.principal().unwrap();
21752166
let input_types = principal.input_types();
2176-
let assoc_types = data.projection_bounds.iter()
2167+
let assoc_types = data.projection_bounds()
21772168
.map(|pb| pb.skip_binder().ty);
21782169
let all_types: Vec<_> = input_types.chain(assoc_types)
21792170
.collect();
@@ -2305,7 +2296,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23052296
// case that results. -nmatsakis
23062297
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
23072298
let poly_trait_ref = match self_ty.sty {
2308-
ty::TyTrait(ref data) => {
2299+
ty::TyDynamic(ref data, ..) => {
23092300
data.principal().unwrap().with_self_ty(self.tcx(), self_ty)
23102301
}
23112302
_ => {
@@ -2474,14 +2465,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24742465
let mut nested = vec![];
24752466
match (&source.sty, &target.sty) {
24762467
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
2477-
(&ty::TyTrait(ref data_a), &ty::TyTrait(ref data_b)) => {
2468+
(&ty::TyDynamic(ref data_a, r_a), &ty::TyDynamic(ref data_b, r_b)) => {
24782469
// See assemble_candidates_for_unsizing for more info.
2479-
let new_trait = tcx.mk_trait(ty::TraitObject::new(
2480-
data_a.principal(),
2481-
data_b.region_bound,
2482-
data_b.auto_traits().collect(),
2483-
data_a.projection_bounds.clone(),
2484-
));
2470+
// Binders reintroduced below in call to mk_existential_predicates.
2471+
let principal = data_a.skip_binder().principal();
2472+
let iter = principal.into_iter().map(ty::ExistentialPredicate::Trait)
2473+
.chain(data_a.skip_binder().projection_bounds()
2474+
.map(|x| ty::ExistentialPredicate::Projection(x)))
2475+
.chain(data_b.auto_traits().map(ty::ExistentialPredicate::AutoTrait));
2476+
let new_trait = tcx.mk_dynamic(
2477+
ty::Binder(tcx.mk_existential_predicates(iter)), r_b);
24852478
let InferOk { obligations, .. } =
24862479
self.infcx.sub_types(false, &obligation.cause, new_trait, target)
24872480
.map_err(|_| Unimplemented)?;
@@ -2491,17 +2484,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24912484
let cause = ObligationCause::new(obligation.cause.span,
24922485
obligation.cause.body_id,
24932486
ObjectCastObligation(target));
2494-
let outlives = ty::OutlivesPredicate(data_a.region_bound,
2495-
data_b.region_bound);
2487+
let outlives = ty::OutlivesPredicate(r_a, r_b);
24962488
nested.push(Obligation::with_depth(cause,
24972489
obligation.recursion_depth + 1,
24982490
ty::Binder(outlives).to_predicate()));
24992491
}
25002492

25012493
// T -> Trait.
2502-
(_, &ty::TyTrait(ref data)) => {
2494+
(_, &ty::TyDynamic(ref data, r)) => {
25032495
let mut object_dids =
2504-
data.auto_traits().chain(data.principal().map(|ref p| p.def_id()));
2496+
data.auto_traits().chain(data.principal().map(|p| p.def_id()));
25052497
if let Some(did) = object_dids.find(|did| {
25062498
!tcx.is_object_safe(*did)
25072499
}) {
@@ -2517,35 +2509,27 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25172509
predicate));
25182510
};
25192511

2520-
// Create the obligation for casting from T to Trait.
2521-
push(data.principal().unwrap().with_self_ty(tcx, source).to_predicate());
2522-
2523-
// We can only make objects from sized types.
2524-
let trait_refs = data.auto_traits()
2525-
.chain(iter::once(
2526-
tcx.lang_items.require(lang_items::SizedTraitLangItem)
2527-
.unwrap_or_else(|msg| tcx.sess.fatal(&msg[..]))))
2528-
.map(|did| ty::TraitRef {
2529-
def_id: did,
2530-
substs: tcx.mk_substs_trait(source, &[]),
2531-
});
2532-
2533-
// Create additional obligations for all the various builtin
2534-
// bounds attached to the object cast. (In other words, if the
2535-
// object type is Foo+Send, this would create an obligation
2536-
// for the Send check.)
2537-
for tr in trait_refs {
2538-
push(tr.to_predicate());
2512+
// Create obligations:
2513+
// - Casting T to Trait
2514+
// - For all the various builtin bounds attached to the object cast. (In other
2515+
// words, if the object type is Foo+Send, this would create an obligation for the
2516+
// Send check.)
2517+
// - Projection predicates
2518+
for predicate in data.iter() {
2519+
push(predicate.with_self_ty(tcx, source));
25392520
}
25402521

2541-
// Create obligations for the projection predicates.
2542-
for bound in &data.projection_bounds {
2543-
push(bound.with_self_ty(tcx, source).to_predicate());
2544-
}
2522+
// We can only make objects from sized types.
2523+
let tr = ty::TraitRef {
2524+
def_id: tcx.lang_items.require(lang_items::SizedTraitLangItem)
2525+
.unwrap_or_else(|msg| tcx.sess.fatal(&msg[..])),
2526+
substs: tcx.mk_substs_trait(source, &[]),
2527+
};
2528+
push(tr.to_predicate());
25452529

25462530
// If the type is `Foo+'a`, ensures that the type
25472531
// being cast to `Foo+'a` outlives `'a`:
2548-
let outlives = ty::OutlivesPredicate(source, data.region_bound);
2532+
let outlives = ty::OutlivesPredicate(source, r);
25492533
push(ty::Binder(outlives).to_predicate());
25502534
}
25512535

src/librustc/ty/contents.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
195195
tc_ty(tcx, typ, cache).owned_pointer()
196196
}
197197

198-
ty::TyTrait(_) => {
198+
ty::TyDynamic(..) => {
199199
TC::All - TC::InteriorParam
200200
}
201201

0 commit comments

Comments
 (0)