Skip to content

Commit 46c7a11

Browse files
Privatize TraitObject.principal and add a method accessor, returning Option.
1 parent 0b399e5 commit 46c7a11

File tree

29 files changed

+142
-103
lines changed

29 files changed

+142
-103
lines changed

src/librustc/traits/coherence.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,12 @@ fn ty_is_local(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal) -> bool {
225225

226226
fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
227227
match ty.sty {
228-
ty::TyBox(..) | ty::TyRef(..) =>
229-
true,
230-
ty::TyAdt(def, _) =>
231-
def.is_fundamental(),
232-
ty::TyTrait(ref data) =>
233-
tcx.has_attr(data.principal.def_id(), "fundamental"),
234-
_ =>
235-
false
228+
ty::TyBox(..) | ty::TyRef(..) => true,
229+
ty::TyAdt(def, _) => def.is_fundamental(),
230+
ty::TyTrait(ref data) => {
231+
data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental"))
232+
}
233+
_ => false
236234
}
237235
}
238236

@@ -273,7 +271,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
273271
}
274272

275273
ty::TyTrait(ref tt) => {
276-
tt.principal.def_id().is_local()
274+
tt.principal().map_or(false, |p| p.def_id().is_local())
277275
}
278276

279277
ty::TyError => {

src/librustc/traits/select.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15281528
_ => {}
15291529
}
15301530

1531-
data.principal.with_self_ty(this.tcx(), self_ty)
1531+
match data.principal() {
1532+
Some(ref p) => p.with_self_ty(this.tcx(), self_ty),
1533+
None => return,
1534+
}
15321535
}
15331536
ty::TyInfer(ty::TyVar(_)) => {
15341537
debug!("assemble_candidates_from_object_ty: ambiguous");
@@ -1611,8 +1614,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16111614
//
16121615
// We always upcast when we can because of reason
16131616
// #2 (region bounds).
1614-
data_a.principal.def_id() == data_b.principal.def_id() &&
1615-
data_a.builtin_bounds.is_superset(&data_b.builtin_bounds)
1617+
match (data_a.principal(), data_b.principal()) {
1618+
(Some(ref a), Some(ref b)) => a.def_id() == b.def_id() &&
1619+
data_a.builtin_bounds.is_superset(&data_b.builtin_bounds),
1620+
_ => false
1621+
}
16161622
}
16171623

16181624
// T -> Trait.
@@ -2167,7 +2173,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21672173
match self_ty.sty {
21682174
ty::TyTrait(ref data) => {
21692175
// OK to skip the binder, it is reintroduced below
2170-
let input_types = data.principal.input_types();
2176+
let principal = data.principal().unwrap();
2177+
let input_types = principal.input_types();
21712178
let assoc_types = data.projection_bounds.iter()
21722179
.map(|pb| pb.skip_binder().ty);
21732180
let all_types: Vec<_> = input_types.chain(assoc_types)
@@ -2301,7 +2308,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23012308
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
23022309
let poly_trait_ref = match self_ty.sty {
23032310
ty::TyTrait(ref data) => {
2304-
data.principal.with_self_ty(self.tcx(), self_ty)
2311+
data.principal().unwrap().with_self_ty(self.tcx(), self_ty)
23052312
}
23062313
_ => {
23072314
span_bug!(obligation.cause.span,
@@ -2471,12 +2478,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24712478
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
24722479
(&ty::TyTrait(ref data_a), &ty::TyTrait(ref data_b)) => {
24732480
// See assemble_candidates_for_unsizing for more info.
2474-
let new_trait = tcx.mk_trait(ty::TraitObject {
2475-
principal: data_a.principal,
2476-
region_bound: data_b.region_bound,
2477-
builtin_bounds: data_b.builtin_bounds,
2478-
projection_bounds: data_a.projection_bounds.clone(),
2479-
});
2481+
let new_trait = tcx.mk_trait(ty::TraitObject::new(
2482+
data_a.principal(),
2483+
data_b.region_bound,
2484+
data_b.builtin_bounds,
2485+
data_a.projection_bounds.clone(),
2486+
));
2487+
let origin = TypeOrigin::Misc(obligation.cause.span);
24802488
let InferOk { obligations, .. } =
24812489
self.infcx.sub_types(false, &obligation.cause, new_trait, target)
24822490
.map_err(|_| Unimplemented)?;
@@ -2499,7 +2507,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24992507
data.builtin_bounds.iter().flat_map(|bound| {
25002508
tcx.lang_items.from_builtin_kind(bound).ok()
25012509
})
2502-
.chain(Some(data.principal.def_id()));
2510+
.chain(data.principal().map(|ref p| p.def_id()));
25032511
if let Some(did) = object_dids.find(|did| {
25042512
!tcx.is_object_safe(*did)
25052513
}) {
@@ -2516,7 +2524,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25162524
};
25172525

25182526
// Create the obligation for casting from T to Trait.
2519-
push(data.principal.with_self_ty(tcx, source).to_predicate());
2527+
push(data.principal().unwrap().with_self_ty(tcx, source).to_predicate());
25202528

25212529
// We can only make objects from sized types.
25222530
let mut builtin_bounds = data.builtin_bounds;

src/librustc/ty/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
215215
ty::TyFnDef(..) => format!("fn item"),
216216
ty::TyFnPtr(_) => "fn pointer".to_string(),
217217
ty::TyTrait(ref inner) => {
218-
format!("trait {}", tcx.item_path_str(inner.principal.def_id()))
218+
inner.principal().map_or_else(|| "trait".to_string(),
219+
|p| format!("trait {}", tcx.item_path_str(p.def_id())))
219220
}
220221
ty::TyClosure(..) => "closure".to_string(),
221222
ty::TyTuple(_) => "tuple".to_string(),

src/librustc/ty/fast_reject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
6060
ty::TyArray(..) | ty::TySlice(_) => Some(ArraySimplifiedType),
6161
ty::TyRawPtr(_) => Some(PtrSimplifiedType),
6262
ty::TyTrait(ref trait_info) => {
63-
Some(TraitSimplifiedType(trait_info.principal.def_id()))
63+
trait_info.principal().map(|p| TraitSimplifiedType(p.def_id()))
6464
}
6565
ty::TyRef(_, mt) => {
6666
// since we introduce auto-refs during method lookup, we

src/librustc/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl FlagComputation {
123123

124124
&ty::TyTrait(ref obj) => {
125125
let mut computation = FlagComputation::new();
126-
computation.add_substs(obj.principal.skip_binder().substs);
126+
computation.add_substs(obj.principal().unwrap().skip_binder().substs);
127127
for projection_bound in &obj.projection_bounds {
128128
let mut proj_computation = FlagComputation::new();
129129
proj_computation.add_existential_projection(&projection_bound.0);

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option<DefId> {
316316
match ty.sty {
317317
ty::TyAdt(adt_def, _) => Some(adt_def.did),
318318

319-
ty::TyTrait(ref data) => Some(data.principal.def_id()),
319+
ty::TyTrait(ref data) => data.principal().map(|ref p| p.def_id()),
320320

321321
ty::TyArray(subty, _) |
322322
ty::TySlice(subty) |

src/librustc/ty/relate.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,11 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
417417

418418
(&ty::TyTrait(ref a_obj), &ty::TyTrait(ref b_obj)) =>
419419
{
420-
let principal = relation.relate(&a_obj.principal, &b_obj.principal)?;
420+
let principal = match (a_obj.principal(), b_obj.principal()) {
421+
(Some(ref a_p), Some(ref b_p)) => Some(relation.relate(a_p, b_p)?),
422+
(None, None) => None,
423+
_ => return Err(TypeError::Sorts(expected_found(relation, &a, &b))),
424+
};
421425
let r =
422426
relation.with_cause(
423427
Cause::ExistentialRegionBound,
@@ -426,12 +430,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
426430
&b_obj.region_bound))?;
427431
let nb = relation.relate(&a_obj.builtin_bounds, &b_obj.builtin_bounds)?;
428432
let pb = relation.relate(&a_obj.projection_bounds, &b_obj.projection_bounds)?;
429-
Ok(tcx.mk_trait(ty::TraitObject {
430-
principal: principal,
431-
region_bound: r,
432-
builtin_bounds: nb,
433-
projection_bounds: pb
434-
}))
433+
Ok(tcx.mk_trait(ty::TraitObject::new(principal, r, nb, pb)))
435434
}
436435

437436
(&ty::TyClosure(a_id, a_substs),

src/librustc/ty/structural_impls.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,16 @@ impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
429429

430430
impl<'tcx> TypeFoldable<'tcx> for ty::TraitObject<'tcx> {
431431
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
432-
ty::TraitObject {
433-
principal: self.principal.fold_with(folder),
434-
region_bound: self.region_bound.fold_with(folder),
435-
builtin_bounds: self.builtin_bounds,
436-
projection_bounds: self.projection_bounds.fold_with(folder),
437-
}
432+
ty::TraitObject::new(
433+
self.principal().map(|p| p.fold_with(folder)),
434+
self.region_bound.fold_with(folder),
435+
self.builtin_bounds,
436+
self.projection_bounds.fold_with(folder),
437+
)
438438
}
439439

440440
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
441-
self.principal.visit_with(visitor) ||
441+
self.principal().map(|p| p.visit_with(visitor)).unwrap_or(true) ||
442442
self.region_bound.visit_with(visitor) ||
443443
self.projection_bounds.visit_with(visitor)
444444
}

src/librustc/ty/sty.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,29 @@ impl<'a, 'gcx, 'acx, 'tcx> ClosureSubsts<'tcx> {
277277

278278
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
279279
pub struct TraitObject<'tcx> {
280-
pub principal: PolyExistentialTraitRef<'tcx>,
280+
principal: Option<PolyExistentialTraitRef<'tcx>>,
281281
pub region_bound: &'tcx ty::Region,
282282
pub builtin_bounds: BuiltinBounds,
283283
pub projection_bounds: Vec<PolyExistentialProjection<'tcx>>,
284284
}
285285

286+
impl<'tcx> TraitObject<'tcx> {
287+
pub fn new(principal: Option<PolyExistentialTraitRef<'tcx>>, region_bound: &'tcx ty::Region,
288+
builtin_bounds: BuiltinBounds, projection_bounds: Vec<PolyExistentialProjection<'tcx>>)
289+
-> Self {
290+
TraitObject {
291+
principal: principal,
292+
region_bound: region_bound,
293+
builtin_bounds: builtin_bounds,
294+
projection_bounds: projection_bounds,
295+
}
296+
}
297+
298+
pub fn principal(&self) -> Option<PolyExistentialTraitRef<'tcx>> {
299+
self.principal
300+
}
301+
}
302+
286303
/// A complete reference to a trait. These take numerous guises in syntax,
287304
/// but perhaps the most recognizable form is in a where clause:
288305
///
@@ -1221,7 +1238,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12211238

12221239
pub fn ty_to_def_id(&self) -> Option<DefId> {
12231240
match self.sty {
1224-
TyTrait(ref tt) => Some(tt.principal.def_id()),
1241+
TyTrait(ref tt) => tt.principal().map(|p| p.def_id()),
12251242
TyAdt(def, _) => Some(def.did),
12261243
TyClosure(id, _) => Some(id),
12271244
_ => None
@@ -1245,7 +1262,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12451262
}
12461263
TyTrait(ref obj) => {
12471264
let mut v = vec![obj.region_bound];
1248-
v.extend(obj.principal.skip_binder().substs.regions());
1265+
v.extend(obj.principal().unwrap().skip_binder().substs.regions());
12491266
v
12501267
}
12511268
TyAdt(_, substs) | TyAnon(_, substs) => {

src/librustc/ty/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,9 @@ impl<'a, 'gcx, 'tcx, H: Hasher> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tc
532532
self.hash(f.sig.inputs().skip_binder().len());
533533
}
534534
TyTrait(ref data) => {
535-
self.def_id(data.principal.def_id());
535+
if let Some(ref p) = data.principal() {
536+
self.def_id(p.def_id());
537+
}
536538
self.hash(data.builtin_bounds);
537539
}
538540
TyTuple(tys) => {

0 commit comments

Comments
 (0)