@@ -259,11 +259,11 @@ impl<'tcx> InferCtxt<'tcx> {
259259 structurally_relate_aliases,
260260 root_vid,
261261 for_universe,
262- ambient_variance,
263262 root_term : source_term. into ( ) ,
263+ ambient_variance,
264264 in_alias : false ,
265- has_unconstrained_ty_var : false ,
266265 cache : Default :: default ( ) ,
266+ has_unconstrained_ty_var : false ,
267267 } ;
268268
269269 let value_may_be_infer = generalizer. relate ( source_term, source_term) ?;
@@ -304,14 +304,12 @@ struct Generalizer<'me, 'tcx> {
304304 /// we reject the relation.
305305 for_universe : ty:: UniverseIndex ,
306306
307- /// After we generalize this type, we are going to relate it to
308- /// some other type. What will be the variance at this point?
309- ambient_variance : ty:: Variance ,
310-
311307 /// The root term (const or type) we're generalizing. Used for cycle errors.
312308 root_term : Term < ' tcx > ,
313309
314- cache : SsoHashMap < Ty < ' tcx > , Ty < ' tcx > > ,
310+ /// After we generalize this type, we are going to relate it to
311+ /// some other type. What will be the variance at this point?
312+ ambient_variance : ty:: Variance ,
315313
316314 /// This is set once we're generalizing the arguments of an alias.
317315 ///
@@ -320,6 +318,8 @@ struct Generalizer<'me, 'tcx> {
320318 /// hold by either normalizing the outer or the inner associated type.
321319 in_alias : bool ,
322320
321+ cache : SsoHashMap < ( Ty < ' tcx > , ty:: Variance , bool ) , Ty < ' tcx > > ,
322+
323323 /// See the field `has_unconstrained_ty_var` in `Generalization`.
324324 has_unconstrained_ty_var : bool ,
325325}
@@ -451,7 +451,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
451451 fn tys ( & mut self , t : Ty < ' tcx > , t2 : Ty < ' tcx > ) -> RelateResult < ' tcx , Ty < ' tcx > > {
452452 assert_eq ! ( t, t2) ; // we are misusing TypeRelation here; both LHS and RHS ought to be ==
453453
454- if let Some ( & result) = self . cache . get ( & t ) {
454+ if let Some ( & result) = self . cache . get ( & ( t , self . ambient_variance , self . in_alias ) ) {
455455 return Ok ( result) ;
456456 }
457457
@@ -557,7 +557,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
557557 _ => relate:: structurally_relate_tys ( self , t, t) ,
558558 } ?;
559559
560- self . cache . insert ( t , g) ;
560+ self . cache . insert ( ( t , self . ambient_variance , self . in_alias ) , g) ;
561561 Ok ( g)
562562 }
563563
0 commit comments