From 46c7a117f84954ce845c55c90db62952624638de Mon Sep 17 00:00:00 2001 From: Mark-Simulacrum Date: Sat, 12 Nov 2016 15:46:16 -0700 Subject: [PATCH] Privatize TraitObject.principal and add a method accessor, returning Option. --- src/librustc/traits/coherence.rs | 16 ++++--- src/librustc/traits/select.rs | 34 +++++++++------ src/librustc/ty/error.rs | 3 +- src/librustc/ty/fast_reject.rs | 2 +- src/librustc/ty/flags.rs | 2 +- src/librustc/ty/item_path.rs | 2 +- src/librustc/ty/relate.rs | 13 +++--- src/librustc/ty/structural_impls.rs | 14 +++---- src/librustc/ty/sty.rs | 23 +++++++++-- src/librustc/ty/util.rs | 4 +- src/librustc/ty/walk.rs | 5 ++- src/librustc/ty/wf.rs | 4 +- src/librustc/util/ppaux.rs | 4 +- src/librustc_trans/base.rs | 2 +- src/librustc_trans/collector.rs | 2 +- src/librustc_trans/debuginfo/metadata.rs | 11 +++-- src/librustc_trans/debuginfo/type_names.rs | 10 +++-- src/librustc_trans/trans_item.rs | 10 +++-- src/librustc_typeck/astconv.rs | 12 +++--- src/librustc_typeck/check/cast.rs | 2 +- src/librustc_typeck/check/closure.rs | 2 +- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/method/probe.rs | 4 +- src/librustc_typeck/check/method/suggest.rs | 2 +- src/librustc_typeck/coherence/mod.rs | 2 +- src/librustc_typeck/coherence/orphan.rs | 4 +- src/librustc_typeck/coherence/overlap.rs | 4 +- src/librustc_typeck/variance/constraints.rs | 2 +- src/librustdoc/clean/mod.rs | 46 +++++++++++---------- 29 files changed, 141 insertions(+), 102 deletions(-) diff --git a/src/librustc/traits/coherence.rs b/src/librustc/traits/coherence.rs index 2e06e83f848..d4e217fb710 100644 --- a/src/librustc/traits/coherence.rs +++ b/src/librustc/traits/coherence.rs @@ -225,14 +225,12 @@ fn ty_is_local(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal) -> bool { fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool { match ty.sty { - ty::TyBox(..) | ty::TyRef(..) => - true, - ty::TyAdt(def, _) => - def.is_fundamental(), - ty::TyTrait(ref data) => - tcx.has_attr(data.principal.def_id(), "fundamental"), - _ => - false + ty::TyBox(..) | ty::TyRef(..) => true, + ty::TyAdt(def, _) => def.is_fundamental(), + ty::TyTrait(ref data) => { + data.principal().map_or(false, |p| tcx.has_attr(p.def_id(), "fundamental")) + } + _ => false } } @@ -273,7 +271,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)-> } ty::TyTrait(ref tt) => { - tt.principal.def_id().is_local() + tt.principal().map_or(false, |p| p.def_id().is_local()) } ty::TyError => { diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index c25e7ee344f..f79a212a473 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -1528,7 +1528,10 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { _ => {} } - data.principal.with_self_ty(this.tcx(), self_ty) + match data.principal() { + Some(ref p) => p.with_self_ty(this.tcx(), self_ty), + None => return, + } } ty::TyInfer(ty::TyVar(_)) => { debug!("assemble_candidates_from_object_ty: ambiguous"); @@ -1611,8 +1614,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { // // We always upcast when we can because of reason // #2 (region bounds). - data_a.principal.def_id() == data_b.principal.def_id() && - data_a.builtin_bounds.is_superset(&data_b.builtin_bounds) + match (data_a.principal(), data_b.principal()) { + (Some(ref a), Some(ref b)) => a.def_id() == b.def_id() && + data_a.builtin_bounds.is_superset(&data_b.builtin_bounds), + _ => false + } } // T -> Trait. @@ -2167,7 +2173,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { match self_ty.sty { ty::TyTrait(ref data) => { // OK to skip the binder, it is reintroduced below - let input_types = data.principal.input_types(); + let principal = data.principal().unwrap(); + let input_types = principal.input_types(); let assoc_types = data.projection_bounds.iter() .map(|pb| pb.skip_binder().ty); let all_types: Vec<_> = input_types.chain(assoc_types) @@ -2301,7 +2308,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder()); let poly_trait_ref = match self_ty.sty { ty::TyTrait(ref data) => { - data.principal.with_self_ty(self.tcx(), self_ty) + data.principal().unwrap().with_self_ty(self.tcx(), self_ty) } _ => { span_bug!(obligation.cause.span, @@ -2471,12 +2478,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { // Trait+Kx+'a -> Trait+Ky+'b (upcasts). (&ty::TyTrait(ref data_a), &ty::TyTrait(ref data_b)) => { // See assemble_candidates_for_unsizing for more info. - let new_trait = tcx.mk_trait(ty::TraitObject { - principal: data_a.principal, - region_bound: data_b.region_bound, - builtin_bounds: data_b.builtin_bounds, - projection_bounds: data_a.projection_bounds.clone(), - }); + let new_trait = tcx.mk_trait(ty::TraitObject::new( + data_a.principal(), + data_b.region_bound, + data_b.builtin_bounds, + data_a.projection_bounds.clone(), + )); + let origin = TypeOrigin::Misc(obligation.cause.span); let InferOk { obligations, .. } = self.infcx.sub_types(false, &obligation.cause, new_trait, target) .map_err(|_| Unimplemented)?; @@ -2499,7 +2507,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { data.builtin_bounds.iter().flat_map(|bound| { tcx.lang_items.from_builtin_kind(bound).ok() }) - .chain(Some(data.principal.def_id())); + .chain(data.principal().map(|ref p| p.def_id())); if let Some(did) = object_dids.find(|did| { !tcx.is_object_safe(*did) }) { @@ -2516,7 +2524,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { }; // Create the obligation for casting from T to Trait. - push(data.principal.with_self_ty(tcx, source).to_predicate()); + push(data.principal().unwrap().with_self_ty(tcx, source).to_predicate()); // We can only make objects from sized types. let mut builtin_bounds = data.builtin_bounds; diff --git a/src/librustc/ty/error.rs b/src/librustc/ty/error.rs index 9b345c2d023..8bdcc93fa21 100644 --- a/src/librustc/ty/error.rs +++ b/src/librustc/ty/error.rs @@ -215,7 +215,8 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> { ty::TyFnDef(..) => format!("fn item"), ty::TyFnPtr(_) => "fn pointer".to_string(), ty::TyTrait(ref inner) => { - format!("trait {}", tcx.item_path_str(inner.principal.def_id())) + inner.principal().map_or_else(|| "trait".to_string(), + |p| format!("trait {}", tcx.item_path_str(p.def_id()))) } ty::TyClosure(..) => "closure".to_string(), ty::TyTuple(_) => "tuple".to_string(), diff --git a/src/librustc/ty/fast_reject.rs b/src/librustc/ty/fast_reject.rs index befc9533c38..3eab06dc674 100644 --- a/src/librustc/ty/fast_reject.rs +++ b/src/librustc/ty/fast_reject.rs @@ -60,7 +60,7 @@ pub fn simplify_type<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, ty::TyArray(..) | ty::TySlice(_) => Some(ArraySimplifiedType), ty::TyRawPtr(_) => Some(PtrSimplifiedType), ty::TyTrait(ref trait_info) => { - Some(TraitSimplifiedType(trait_info.principal.def_id())) + trait_info.principal().map(|p| TraitSimplifiedType(p.def_id())) } ty::TyRef(_, mt) => { // since we introduce auto-refs during method lookup, we diff --git a/src/librustc/ty/flags.rs b/src/librustc/ty/flags.rs index 2c09b89beb2..026bf64533c 100644 --- a/src/librustc/ty/flags.rs +++ b/src/librustc/ty/flags.rs @@ -123,7 +123,7 @@ impl FlagComputation { &ty::TyTrait(ref obj) => { let mut computation = FlagComputation::new(); - computation.add_substs(obj.principal.skip_binder().substs); + computation.add_substs(obj.principal().unwrap().skip_binder().substs); for projection_bound in &obj.projection_bounds { let mut proj_computation = FlagComputation::new(); proj_computation.add_existential_projection(&projection_bound.0); diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index b93a8698f60..d1e6f5b8cd1 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -316,7 +316,7 @@ pub fn characteristic_def_id_of_type(ty: Ty) -> Option { match ty.sty { ty::TyAdt(adt_def, _) => Some(adt_def.did), - ty::TyTrait(ref data) => Some(data.principal.def_id()), + ty::TyTrait(ref data) => data.principal().map(|ref p| p.def_id()), ty::TyArray(subty, _) | ty::TySlice(subty) | diff --git a/src/librustc/ty/relate.rs b/src/librustc/ty/relate.rs index 2a01bad33c5..a70cfabac87 100644 --- a/src/librustc/ty/relate.rs +++ b/src/librustc/ty/relate.rs @@ -417,7 +417,11 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R, (&ty::TyTrait(ref a_obj), &ty::TyTrait(ref b_obj)) => { - let principal = relation.relate(&a_obj.principal, &b_obj.principal)?; + let principal = match (a_obj.principal(), b_obj.principal()) { + (Some(ref a_p), Some(ref b_p)) => Some(relation.relate(a_p, b_p)?), + (None, None) => None, + _ => return Err(TypeError::Sorts(expected_found(relation, &a, &b))), + }; let r = relation.with_cause( Cause::ExistentialRegionBound, @@ -426,12 +430,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R, &b_obj.region_bound))?; let nb = relation.relate(&a_obj.builtin_bounds, &b_obj.builtin_bounds)?; let pb = relation.relate(&a_obj.projection_bounds, &b_obj.projection_bounds)?; - Ok(tcx.mk_trait(ty::TraitObject { - principal: principal, - region_bound: r, - builtin_bounds: nb, - projection_bounds: pb - })) + Ok(tcx.mk_trait(ty::TraitObject::new(principal, r, nb, pb))) } (&ty::TyClosure(a_id, a_substs), diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index c8618cd1547..883f43258d8 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -429,16 +429,16 @@ impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder { impl<'tcx> TypeFoldable<'tcx> for ty::TraitObject<'tcx> { fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { - ty::TraitObject { - principal: self.principal.fold_with(folder), - region_bound: self.region_bound.fold_with(folder), - builtin_bounds: self.builtin_bounds, - projection_bounds: self.projection_bounds.fold_with(folder), - } + ty::TraitObject::new( + self.principal().map(|p| p.fold_with(folder)), + self.region_bound.fold_with(folder), + self.builtin_bounds, + self.projection_bounds.fold_with(folder), + ) } fn super_visit_with>(&self, visitor: &mut V) -> bool { - self.principal.visit_with(visitor) || + self.principal().map(|p| p.visit_with(visitor)).unwrap_or(true) || self.region_bound.visit_with(visitor) || self.projection_bounds.visit_with(visitor) } diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index cb3176cce10..454dedd937b 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -277,12 +277,29 @@ impl<'a, 'gcx, 'acx, 'tcx> ClosureSubsts<'tcx> { #[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] pub struct TraitObject<'tcx> { - pub principal: PolyExistentialTraitRef<'tcx>, + principal: Option>, pub region_bound: &'tcx ty::Region, pub builtin_bounds: BuiltinBounds, pub projection_bounds: Vec>, } +impl<'tcx> TraitObject<'tcx> { + pub fn new(principal: Option>, region_bound: &'tcx ty::Region, + builtin_bounds: BuiltinBounds, projection_bounds: Vec>) + -> Self { + TraitObject { + principal: principal, + region_bound: region_bound, + builtin_bounds: builtin_bounds, + projection_bounds: projection_bounds, + } + } + + pub fn principal(&self) -> Option> { + self.principal + } +} + /// A complete reference to a trait. These take numerous guises in syntax, /// but perhaps the most recognizable form is in a where clause: /// @@ -1221,7 +1238,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { pub fn ty_to_def_id(&self) -> Option { match self.sty { - TyTrait(ref tt) => Some(tt.principal.def_id()), + TyTrait(ref tt) => tt.principal().map(|p| p.def_id()), TyAdt(def, _) => Some(def.did), TyClosure(id, _) => Some(id), _ => None @@ -1245,7 +1262,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { } TyTrait(ref obj) => { let mut v = vec![obj.region_bound]; - v.extend(obj.principal.skip_binder().substs.regions()); + v.extend(obj.principal().unwrap().skip_binder().substs.regions()); v } TyAdt(_, substs) | TyAnon(_, substs) => { diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 1dde8106ec6..bd7d72d3ef3 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -532,7 +532,9 @@ impl<'a, 'gcx, 'tcx, H: Hasher> TypeVisitor<'tcx> for TypeIdHasher<'a, 'gcx, 'tc self.hash(f.sig.inputs().skip_binder().len()); } TyTrait(ref data) => { - self.def_id(data.principal.def_id()); + if let Some(ref p) = data.principal() { + self.def_id(p.def_id()); + } self.hash(data.builtin_bounds); } TyTuple(tys) => { diff --git a/src/librustc/ty/walk.rs b/src/librustc/ty/walk.rs index 2f9468dbe58..02f55c6e340 100644 --- a/src/librustc/ty/walk.rs +++ b/src/librustc/ty/walk.rs @@ -93,7 +93,10 @@ fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) { stack.extend(data.trait_ref.substs.types().rev()); } ty::TyTrait(ref obj) => { - stack.extend(obj.principal.input_types().rev()); + match obj.principal() { + Some(ref p) => stack.extend(p.input_types().rev()), + None => {} + } stack.extend(obj.projection_bounds.iter().map(|pred| { pred.0.ty }).rev()); diff --git a/src/librustc/ty/wf.rs b/src/librustc/ty/wf.rs index 74c6d7d334c..25015557ad4 100644 --- a/src/librustc/ty/wf.rs +++ b/src/librustc/ty/wf.rs @@ -394,7 +394,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> { data.builtin_bounds.iter().flat_map(|bound| { tcx.lang_items.from_builtin_kind(bound).ok() }) - .chain(Some(data.principal.def_id())); + .chain(data.principal().map(|ref p| p.def_id())); self.out.extend( component_traits.map(|did| { traits::Obligation::new( cause.clone(), @@ -492,7 +492,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> { if !data.has_escaping_regions() { let implicit_bounds = object_region_bounds(self.infcx.tcx, - data.principal, + data.principal().unwrap(), data.builtin_bounds); let explicit_bound = data.region_bound; diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index d04825d5604..c87f3ba0a59 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -339,7 +339,7 @@ impl<'tcx> fmt::Display for ty::TraitObject<'tcx> { // Use a type that can't appear in defaults of type parameters. let dummy_self = tcx.mk_infer(ty::FreshTy(0)); - let principal = tcx.lift(&self.principal) + let principal = self.principal().and_then(|ref p| tcx.lift(p)) .expect("could not lift TraitRef for printing") .with_self_ty(tcx, dummy_self).0; let projections = self.projection_bounds.iter().map(|p| { @@ -466,7 +466,7 @@ impl<'tcx> fmt::Debug for ty::TraitObject<'tcx> { }; maybe_continue(f)?; - write!(f, "{:?}", self.principal)?; + write!(f, "{:?}", self.principal())?; let region_str = format!("{:?}", self.region_bound); if !region_str.is_empty() { diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index c94c25e0b05..a79f7e1733c 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -302,7 +302,7 @@ pub fn unsized_info<'ccx, 'tcx>(ccx: &CrateContext<'ccx, 'tcx>, old_info.expect("unsized_info: missing old info for trait upcast") } (_, &ty::TyTrait(ref data)) => { - let trait_ref = data.principal.with_self_ty(ccx.tcx(), source); + let trait_ref = data.principal().unwrap().with_self_ty(ccx.tcx(), source); let trait_ref = ccx.tcx().erase_regions(&trait_ref); consts::ptrcast(meth::get_vtable(ccx, trait_ref), Type::vtable_ptr(ccx)) diff --git a/src/librustc_trans/collector.rs b/src/librustc_trans/collector.rs index 5c7b004375e..ac8793c47e7 100644 --- a/src/librustc_trans/collector.rs +++ b/src/librustc_trans/collector.rs @@ -1004,7 +1004,7 @@ fn create_trans_items_for_vtable_methods<'a, 'tcx>(scx: &SharedCrateContext<'a, assert!(!trait_ty.needs_subst() && !impl_ty.needs_subst()); if let ty::TyTrait(ref trait_ty) = trait_ty.sty { - let poly_trait_ref = trait_ty.principal.with_self_ty(scx.tcx(), impl_ty); + let poly_trait_ref = trait_ty.principal().unwrap().with_self_ty(scx.tcx(), impl_ty); let param_substs = scx.tcx().intern_substs(&[]); // Walk all methods of the trait, including those of its supertraits diff --git a/src/librustc_trans/debuginfo/metadata.rs b/src/librustc_trans/debuginfo/metadata.rs index 5d01ca892b3..5af1b0faebc 100644 --- a/src/librustc_trans/debuginfo/metadata.rs +++ b/src/librustc_trans/debuginfo/metadata.rs @@ -431,8 +431,13 @@ fn trait_pointer_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, // type is assigned the correct name, size, namespace, and source location. // But it does not describe the trait's methods. - let def_id = match trait_type.sty { - ty::TyTrait(ref data) => data.principal.def_id(), + let containing_scope = match trait_type.sty { + ty::TyTrait(ref data) => if let Some(principal) = data.principal() { + let def_id = principal.def_id(); + get_namespace_and_span_for_item(cx, def_id).0 + } else { + NO_SCOPE_METADATA + }, _ => { bug!("debuginfo: Unexpected trait-object type in \ trait_pointer_metadata(): {:?}", @@ -444,8 +449,6 @@ fn trait_pointer_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let trait_type_name = compute_debuginfo_type_name(cx, trait_object_type, false); - let (containing_scope, _) = get_namespace_and_span_for_item(cx, def_id); - let trait_llvm_type = type_of::type_of(cx, trait_object_type); let file_metadata = unknown_file_metadata(cx); diff --git a/src/librustc_trans/debuginfo/type_names.rs b/src/librustc_trans/debuginfo/type_names.rs index fce1ce56105..1831125e8eb 100644 --- a/src/librustc_trans/debuginfo/type_names.rs +++ b/src/librustc_trans/debuginfo/type_names.rs @@ -94,10 +94,12 @@ pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, output.push(']'); }, ty::TyTrait(ref trait_data) => { - let principal = cx.tcx().erase_late_bound_regions_and_normalize( - &trait_data.principal); - push_item_name(cx, principal.def_id, false, output); - push_type_params(cx, principal.substs, output); + if let Some(principal) = trait_data.principal() { + let principal = cx.tcx().erase_late_bound_regions_and_normalize( + &principal); + push_item_name(cx, principal.def_id, false, output); + push_type_params(cx, principal.substs, output); + } }, ty::TyFnDef(.., &ty::BareFnTy{ unsafety, abi, ref sig } ) | ty::TyFnPtr(&ty::BareFnTy{ unsafety, abi, ref sig } ) => { diff --git a/src/librustc_trans/trans_item.rs b/src/librustc_trans/trans_item.rs index f2d1e375793..c46c6e6f769 100644 --- a/src/librustc_trans/trans_item.rs +++ b/src/librustc_trans/trans_item.rs @@ -458,10 +458,12 @@ impl<'a, 'tcx> DefPathBasedNames<'a, 'tcx> { output.push(']'); }, ty::TyTrait(ref trait_data) => { - self.push_def_path(trait_data.principal.def_id(), output); - self.push_type_params(trait_data.principal.skip_binder().substs, - &trait_data.projection_bounds, - output); + if let Some(principal) = trait_data.principal() { + self.push_def_path(principal.def_id(), output); + self.push_type_params(principal.skip_binder().substs, + &trait_data.projection_bounds, + output); + } }, ty::TyFnDef(.., &ty::BareFnTy{ unsafety, abi, ref sig } ) | ty::TyFnPtr(&ty::BareFnTy{ unsafety, abi, ref sig } ) => { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index bb7b6253300..9cf6f555744 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1135,12 +1135,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o { .emit(); } - let ty = tcx.mk_trait(ty::TraitObject { - principal: existential_principal, - region_bound: region_bound, - builtin_bounds: builtin_bounds, - projection_bounds: existential_projections - }); + let ty = tcx.mk_trait(ty::TraitObject::new( + Some(existential_principal), + region_bound, + builtin_bounds, + existential_projections + )); debug!("trait_object_type: {:?}", ty); ty } diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index 5839c606566..3e1a3c870ca 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -78,7 +78,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { fn unsize_kind(&self, t: Ty<'tcx>) -> Option> { match t.sty { ty::TySlice(_) | ty::TyStr => Some(UnsizeKind::Length), - ty::TyTrait(ref tty) => Some(UnsizeKind::Vtable(tty.principal.def_id())), + ty::TyTrait(ref tty) => Some(UnsizeKind::Vtable(tty.principal().unwrap().def_id())), ty::TyAdt(def, substs) if def.is_struct() => { // FIXME(arielb1): do some kind of normalization match def.struct_variant().fields.last() { diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 75287d4064a..4627c166fc6 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -119,7 +119,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { self.deduce_sig_from_projection(&pb) }) .next(); - let kind = self.tcx.lang_items.fn_trait_kind(object_type.principal.def_id()); + let kind = self.tcx.lang_items.fn_trait_kind(object_type.principal().unwrap().def_id()); (sig, kind) } ty::TyInfer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid), diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index 54e93978b79..4c19ea46e5a 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -255,7 +255,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> { .autoderef(self.span, self_ty) .filter_map(|(ty, _)| { match ty.sty { - ty::TyTrait(ref data) => Some(closure(self, ty, data.principal)), + ty::TyTrait(ref data) => data.principal().map(|p| closure(self, ty, p)), _ => None, } }) diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 545d4e78852..b76d13125b4 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -296,8 +296,8 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> { match self_ty.sty { ty::TyTrait(box ref data) => { - self.assemble_inherent_candidates_from_object(self_ty, data.principal); - self.assemble_inherent_impl_candidates_for_type(data.principal.def_id()); + self.assemble_inherent_candidates_from_object(self_ty, data.principal().unwrap()); + self.assemble_inherent_impl_candidates_for_type(data.principal().unwrap().def_id()); } ty::TyAdt(def, _) => { self.assemble_inherent_impl_candidates_for_type(def.did); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 6598790355e..eb6e84c7922 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -379,7 +379,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { match ty.sty { ty::TyAdt(def, _) => def.did.is_local(), - ty::TyTrait(ref tr) => tr.principal.def_id().is_local(), + ty::TyTrait(ref tr) => tr.principal().map(|p| p.def_id().is_local()).unwrap_or(false), ty::TyParam(_) => true, diff --git a/src/librustc_typeck/coherence/mod.rs b/src/librustc_typeck/coherence/mod.rs index 2663739e36b..0b6f2f554a1 100644 --- a/src/librustc_typeck/coherence/mod.rs +++ b/src/librustc_typeck/coherence/mod.rs @@ -68,7 +68,7 @@ impl<'a, 'gcx, 'tcx> CoherenceChecker<'a, 'gcx, 'tcx> { match ty.sty { TyAdt(def, _) => Some(def.did), - TyTrait(ref t) => Some(t.principal.def_id()), + TyTrait(ref t) => t.principal().map(|p| p.def_id()), TyBox(_) => self.inference_context.tcx.lang_items.owned_box(), diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs index a507077bef7..f949a8796c7 100644 --- a/src/librustc_typeck/coherence/orphan.rs +++ b/src/librustc_typeck/coherence/orphan.rs @@ -86,8 +86,8 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> { ty::TyAdt(def, _) => { self.check_def_id(item, def.did); } - ty::TyTrait(ref data) => { - self.check_def_id(item, data.principal.def_id()); + ty::TyTrait(ref data) if data.principal().is_some() => { + self.check_def_id(item, data.principal().unwrap().def_id()); } ty::TyBox(..) => { match self.tcx.lang_items.require_owned_box() { diff --git a/src/librustc_typeck/coherence/overlap.rs b/src/librustc_typeck/coherence/overlap.rs index e5be7f63067..305b15caa69 100644 --- a/src/librustc_typeck/coherence/overlap.rs +++ b/src/librustc_typeck/coherence/overlap.rs @@ -182,14 +182,14 @@ impl<'cx, 'tcx, 'v> ItemLikeVisitor<'v> for OverlapChecker<'cx, 'tcx> { // This is something like impl Trait1 for Trait2. Illegal // if Trait1 is a supertrait of Trait2 or Trait2 is not object safe. - if !self.tcx.is_object_safe(data.principal.def_id()) { + if data.principal().is_none() || !self.tcx.is_object_safe(data.principal().unwrap().def_id()) { // This is an error, but it will be // reported by wfcheck. Ignore it // here. This is tested by // `coherence-impl-trait-for-trait-object-safe.rs`. } else { let mut supertrait_def_ids = - traits::supertrait_def_ids(self.tcx, data.principal.def_id()); + traits::supertrait_def_ids(self.tcx, data.principal().unwrap().def_id()); if supertrait_def_ids.any(|d| d == trait_def_id) { span_err!(self.tcx.sess, item.span, diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index 686d1a4a771..77ade466069 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -376,7 +376,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { let contra = self.contravariant(variance); self.add_constraints_from_region(generics, data.region_bound, contra); - let poly_trait_ref = data.principal.with_self_ty(self.tcx(), self.tcx().types.err); + let poly_trait_ref = data.principal().unwrap().with_self_ty(self.tcx(), self.tcx().types.err); self.add_constraints_from_trait_ref(generics, poly_trait_ref.0, variance); for projection in &data.projection_bounds { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e233613ee62..f32ee29c0a0 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1877,30 +1877,34 @@ impl<'tcx> Clean for ty::Ty<'tcx> { } } ty::TyTrait(ref obj) => { - let did = obj.principal.def_id(); - inline::record_extern_fqn(cx, did, TypeKind::Trait); + if let Some(principal) = obj.principal() { + let did = principal.def_id(); + inline::record_extern_fqn(cx, did, TypeKind::Trait); - let mut typarams = vec![]; - obj.region_bound.clean(cx).map(|b| typarams.push(RegionBound(b))); - for bb in &obj.builtin_bounds { - typarams.push(bb.clean(cx)); - } + let mut typarams = vec![]; + obj.region_bound.clean(cx).map(|b| typarams.push(RegionBound(b))); + for bb in &obj.builtin_bounds { + typarams.push(bb.clean(cx)); + } - let mut bindings = vec![]; - for &ty::Binder(ref pb) in &obj.projection_bounds { - bindings.push(TypeBinding { - name: pb.item_name.clean(cx), - ty: pb.ty.clean(cx) - }); - } + let mut bindings = vec![]; + for &ty::Binder(ref pb) in &obj.projection_bounds { + bindings.push(TypeBinding { + name: pb.item_name.clean(cx), + ty: pb.ty.clean(cx) + }); + } - let path = external_path(cx, &cx.tcx.item_name(did).as_str(), - Some(did), false, bindings, obj.principal.0.substs); - ResolvedPath { - path: path, - typarams: Some(typarams), - did: did, - is_generic: false, + let path = external_path(cx, &cx.tcx.item_name(did).as_str(), + Some(did), false, bindings, obj.principal.0.substs); + ResolvedPath { + path: path, + typarams: Some(typarams), + did: did, + is_generic: false, + } + } else { + Never } } ty::TyTuple(ref t) => Tuple(t.clean(cx)),