@@ -816,11 +816,14 @@ impl<'tcx> List<ty::PolyExistentialPredicate<'tcx>> {
816816pub struct TraitRef < ' tcx > {
817817 pub def_id : DefId ,
818818 pub substs : SubstsRef < ' tcx > ,
819+ /// This field exists to prevent the creation of `TraitRef` without
820+ /// calling [TyCtxt::mk_trait_ref].
821+ _use_mk_trait_ref_instead : ( ) ,
819822}
820823
821824impl < ' tcx > TraitRef < ' tcx > {
822825 pub fn new ( def_id : DefId , substs : SubstsRef < ' tcx > ) -> TraitRef < ' tcx > {
823- TraitRef { def_id, substs }
826+ TraitRef { def_id, substs, _use_mk_trait_ref_instead : ( ) }
824827 }
825828
826829 pub fn with_self_ty ( self , tcx : TyCtxt < ' tcx > , self_ty : Ty < ' tcx > ) -> Self {
@@ -836,6 +839,7 @@ impl<'tcx> TraitRef<'tcx> {
836839 ty:: Binder :: dummy ( TraitRef {
837840 def_id,
838841 substs : InternalSubsts :: identity_for_item ( tcx, def_id) ,
842+ _use_mk_trait_ref_instead : ( ) ,
839843 } )
840844 }
841845
@@ -850,7 +854,11 @@ impl<'tcx> TraitRef<'tcx> {
850854 substs : SubstsRef < ' tcx > ,
851855 ) -> ty:: TraitRef < ' tcx > {
852856 let defs = tcx. generics_of ( trait_id) ;
853- ty:: TraitRef { def_id : trait_id, substs : tcx. intern_substs ( & substs[ ..defs. params . len ( ) ] ) }
857+ ty:: TraitRef {
858+ def_id : trait_id,
859+ substs : tcx. intern_substs ( & substs[ ..defs. params . len ( ) ] ) ,
860+ _use_mk_trait_ref_instead : ( ) ,
861+ }
854862 }
855863}
856864
@@ -1194,10 +1202,7 @@ impl<'tcx> AliasTy<'tcx> {
11941202 let trait_def_id = self . trait_def_id ( tcx) ;
11951203 let trait_generics = tcx. generics_of ( trait_def_id) ;
11961204 (
1197- ty:: TraitRef {
1198- def_id : trait_def_id,
1199- substs : self . substs . truncate_to ( tcx, trait_generics) ,
1200- } ,
1205+ tcx. mk_trait_ref ( trait_def_id, self . substs . truncate_to ( tcx, trait_generics) ) ,
12011206 & self . substs [ trait_generics. count ( ) ..] ,
12021207 )
12031208 }
@@ -1211,7 +1216,7 @@ impl<'tcx> AliasTy<'tcx> {
12111216 /// as well.
12121217 pub fn trait_ref ( & self , tcx : TyCtxt < ' tcx > ) -> ty:: TraitRef < ' tcx > {
12131218 let def_id = self . trait_def_id ( tcx) ;
1214- ty :: TraitRef { def_id, substs : self . substs . truncate_to ( tcx, tcx. generics_of ( def_id) ) }
1219+ tcx . mk_trait_ref ( def_id, self . substs . truncate_to ( tcx, tcx. generics_of ( def_id) ) )
12151220 }
12161221
12171222 pub fn self_ty ( & self ) -> Ty < ' tcx > {
0 commit comments