@@ -61,7 +61,7 @@ pub trait TypeRelation<'tcx>: Sized {
61
61
62
62
let tcx = self . tcx ( ) ;
63
63
let opt_variances = tcx. variances_of ( item_def_id) ;
64
- relate_substs ( self , Some ( ( item_def_id, opt_variances) ) , a_subst, b_subst)
64
+ relate_substs_with_variances ( self , item_def_id, opt_variances, a_subst, b_subst)
65
65
}
66
66
67
67
/// Switch variance for the purpose of relating `a` and `b`.
@@ -135,34 +135,39 @@ pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>(
135
135
}
136
136
}
137
137
138
+ #[ inline]
138
139
pub fn relate_substs < ' tcx , R : TypeRelation < ' tcx > > (
139
140
relation : & mut R ,
140
- variances : Option < ( DefId , & [ ty:: Variance ] ) > ,
141
+ a_subst : SubstsRef < ' tcx > ,
142
+ b_subst : SubstsRef < ' tcx > ,
143
+ ) -> RelateResult < ' tcx , SubstsRef < ' tcx > > {
144
+ relation. tcx ( ) . mk_substs ( iter:: zip ( a_subst, b_subst) . map ( |( a, b) | {
145
+ relation. relate_with_variance ( ty:: Invariant , ty:: VarianceDiagInfo :: default ( ) , a, b)
146
+ } ) )
147
+ }
148
+
149
+ pub fn relate_substs_with_variances < ' tcx , R : TypeRelation < ' tcx > > (
150
+ relation : & mut R ,
151
+ ty_def_id : DefId ,
152
+ variances : & [ ty:: Variance ] ,
141
153
a_subst : SubstsRef < ' tcx > ,
142
154
b_subst : SubstsRef < ' tcx > ,
143
155
) -> RelateResult < ' tcx , SubstsRef < ' tcx > > {
144
156
let tcx = relation. tcx ( ) ;
145
157
146
- let zipped = iter:: zip ( a_subst, b_subst) ;
147
- match variances {
148
- Some ( ( ty_def_id, variances) ) => {
149
- let mut cached_ty = None ;
150
- tcx. mk_substs ( zipped. enumerate ( ) . map ( |( i, ( a, b) ) | {
151
- let variance = variances[ i] ;
152
- let variance_info = if variance == ty:: Invariant {
153
- let ty = * cached_ty
154
- . get_or_insert_with ( || tcx. type_of ( ty_def_id) . subst ( tcx, a_subst) ) ;
155
- ty:: VarianceDiagInfo :: Invariant { ty, param_index : i. try_into ( ) . unwrap ( ) }
156
- } else {
157
- ty:: VarianceDiagInfo :: default ( )
158
- } ;
159
- relation. relate_with_variance ( variance, variance_info, a, b)
160
- } ) )
161
- }
162
- None => tcx. mk_substs ( zipped. map ( |( a, b) | {
163
- relation. relate_with_variance ( ty:: Invariant , ty:: VarianceDiagInfo :: default ( ) , a, b)
164
- } ) ) ,
165
- }
158
+ let mut cached_ty = None ;
159
+ let params = iter:: zip ( a_subst, b_subst) . enumerate ( ) . map ( |( i, ( a, b) ) | {
160
+ let variance = variances[ i] ;
161
+ let variance_info = if variance == ty:: Invariant {
162
+ let ty = * cached_ty. get_or_insert_with ( || tcx. type_of ( ty_def_id) . subst ( tcx, a_subst) ) ;
163
+ ty:: VarianceDiagInfo :: Invariant { ty, param_index : i. try_into ( ) . unwrap ( ) }
164
+ } else {
165
+ ty:: VarianceDiagInfo :: default ( )
166
+ } ;
167
+ relation. relate_with_variance ( variance, variance_info, a, b)
168
+ } ) ;
169
+
170
+ tcx. mk_substs ( params)
166
171
}
167
172
168
173
impl < ' tcx > Relate < ' tcx > for ty:: FnSig < ' tcx > {
@@ -318,7 +323,7 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
318
323
if a. def_id != b. def_id {
319
324
Err ( TypeError :: Traits ( expected_found ( relation, a. def_id , b. def_id ) ) )
320
325
} else {
321
- let substs = relate_substs ( relation, None , a. substs , b. substs ) ?;
326
+ let substs = relate_substs ( relation, a. substs , b. substs ) ?;
322
327
Ok ( ty:: TraitRef { def_id : a. def_id , substs } )
323
328
}
324
329
}
@@ -334,7 +339,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
334
339
if a. def_id != b. def_id {
335
340
Err ( TypeError :: Traits ( expected_found ( relation, a. def_id , b. def_id ) ) )
336
341
} else {
337
- let substs = relate_substs ( relation, None , a. substs , b. substs ) ?;
342
+ let substs = relate_substs ( relation, a. substs , b. substs ) ?;
338
343
Ok ( ty:: ExistentialTraitRef { def_id : a. def_id , substs } )
339
344
}
340
345
}
@@ -554,7 +559,7 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
554
559
( & ty:: Opaque ( a_def_id, a_substs) , & ty:: Opaque ( b_def_id, b_substs) )
555
560
if a_def_id == b_def_id =>
556
561
{
557
- let substs = relate_substs ( relation, None , a_substs, b_substs) ?;
562
+ let substs = relate_substs ( relation, a_substs, b_substs) ?;
558
563
Ok ( tcx. mk_opaque ( a_def_id, substs) )
559
564
}
560
565
@@ -742,7 +747,7 @@ impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
742
747
a : ty:: ClosureSubsts < ' tcx > ,
743
748
b : ty:: ClosureSubsts < ' tcx > ,
744
749
) -> RelateResult < ' tcx , ty:: ClosureSubsts < ' tcx > > {
745
- let substs = relate_substs ( relation, None , a. substs , b. substs ) ?;
750
+ let substs = relate_substs ( relation, a. substs , b. substs ) ?;
746
751
Ok ( ty:: ClosureSubsts { substs } )
747
752
}
748
753
}
@@ -753,7 +758,7 @@ impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> {
753
758
a : ty:: GeneratorSubsts < ' tcx > ,
754
759
b : ty:: GeneratorSubsts < ' tcx > ,
755
760
) -> RelateResult < ' tcx , ty:: GeneratorSubsts < ' tcx > > {
756
- let substs = relate_substs ( relation, None , a. substs , b. substs ) ?;
761
+ let substs = relate_substs ( relation, a. substs , b. substs ) ?;
757
762
Ok ( ty:: GeneratorSubsts { substs } )
758
763
}
759
764
}
@@ -764,7 +769,7 @@ impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> {
764
769
a : SubstsRef < ' tcx > ,
765
770
b : SubstsRef < ' tcx > ,
766
771
) -> RelateResult < ' tcx , SubstsRef < ' tcx > > {
767
- relate_substs ( relation, None , a, b)
772
+ relate_substs ( relation, a, b)
768
773
}
769
774
}
770
775
0 commit comments