@@ -60,7 +60,7 @@ pub trait TypeRelation<'tcx>: Sized {
6060
6161 let tcx = self . tcx ( ) ;
6262 let opt_variances = tcx. variances_of ( item_def_id) ;
63- relate_substs_with_variances ( self , item_def_id, opt_variances, a_subst, b_subst)
63+ relate_substs_with_variances ( self , item_def_id, opt_variances, a_subst, b_subst, true )
6464 }
6565
6666 /// Switch variance for the purpose of relating `a` and `b`.
@@ -151,13 +151,14 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
151151 variances : & [ ty:: Variance ] ,
152152 a_subst : SubstsRef < ' tcx > ,
153153 b_subst : SubstsRef < ' tcx > ,
154+ fetch_ty_for_diag : bool ,
154155) -> RelateResult < ' tcx , SubstsRef < ' tcx > > {
155156 let tcx = relation. tcx ( ) ;
156157
157158 let mut cached_ty = None ;
158159 let params = iter:: zip ( a_subst, b_subst) . enumerate ( ) . map ( |( i, ( a, b) ) | {
159160 let variance = variances[ i] ;
160- let variance_info = if variance == ty:: Invariant {
161+ let variance_info = if variance == ty:: Invariant && fetch_ty_for_diag {
161162 let ty =
162163 * cached_ty. get_or_insert_with ( || tcx. bound_type_of ( ty_def_id) . subst ( tcx, a_subst) ) ;
163164 ty:: VarianceDiagInfo :: Invariant { ty, param_index : i. try_into ( ) . unwrap ( ) }
@@ -170,26 +171,6 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
170171 tcx. mk_substs ( params)
171172}
172173
173- #[ instrument( level = "trace" , skip( relation) , ret) ]
174- fn relate_opaque_item_substs < ' tcx , R : TypeRelation < ' tcx > > (
175- relation : & mut R ,
176- def_id : DefId ,
177- a_subst : SubstsRef < ' tcx > ,
178- b_subst : SubstsRef < ' tcx > ,
179- ) -> RelateResult < ' tcx , SubstsRef < ' tcx > > {
180- let tcx = relation. tcx ( ) ;
181- let variances = tcx. variances_of ( def_id) ;
182- debug ! ( ?variances) ;
183-
184- let params = iter:: zip ( a_subst, b_subst) . enumerate ( ) . map ( |( i, ( a, b) ) | {
185- let variance = variances[ i] ;
186- let variance_info = ty:: VarianceDiagInfo :: default ( ) ;
187- relation. relate_with_variance ( variance, variance_info, a, b)
188- } ) ;
189-
190- tcx. mk_substs ( params)
191- }
192-
193174impl < ' tcx > Relate < ' tcx > for ty:: FnSig < ' tcx > {
194175 fn relate < R : TypeRelation < ' tcx > > (
195176 relation : & mut R ,
@@ -581,7 +562,15 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
581562 ( & ty:: Opaque ( a_def_id, a_substs) , & ty:: Opaque ( b_def_id, b_substs) )
582563 if a_def_id == b_def_id =>
583564 {
584- let substs = relate_opaque_item_substs ( relation, a_def_id, a_substs, b_substs) ?;
565+ let opt_variances = tcx. variances_of ( a_def_id) ;
566+ let substs = relate_substs_with_variances (
567+ relation,
568+ a_def_id,
569+ opt_variances,
570+ a_substs,
571+ b_substs,
572+ false , // do not fetch `type_of(a_def_id)`, as it will cause a cycle
573+ ) ?;
585574 Ok ( tcx. mk_opaque ( a_def_id, substs) )
586575 }
587576
0 commit comments