@@ -343,7 +343,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
343343 for_vid : ty:: TyVid ,
344344 dir : RelationDir ,
345345 ) -> RelateResult < ' tcx , Generalization < ' tcx > > {
346- debug ! ( "generalize(ty={:?}, for_vid={:?}, dir={:?}" , ty, for_vid, dir) ;
346+ debug ! ( "generalize(ty={:?}, for_vid={:?}, dir={:?}) " , ty, for_vid, dir) ;
347347 // Determine the ambient variance within which `ty` appears.
348348 // The surrounding equation is:
349349 //
@@ -649,13 +649,17 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
649649 ) -> RelateResult < ' tcx , & ' tcx ty:: Const < ' tcx > > {
650650 assert_eq ! ( c, c2) ; // we are abusing TypeRelation here; both LHS and RHS ought to be ==
651651
652+ debug ! ( "generalize: consts c={:?}" , c) ;
652653 match c. val {
653654 ty:: ConstKind :: Infer ( InferConst :: Var ( vid) ) => {
654655 let mut inner = self . infcx . inner . borrow_mut ( ) ;
655656 let variable_table = & mut inner. const_unification_table ( ) ;
656657 let var_value = variable_table. probe_value ( vid) ;
657658 match var_value. val {
658- ConstVariableValue :: Known { value : u } => self . relate ( & u, & u) ,
659+ ConstVariableValue :: Known { value : u } => {
660+ drop ( inner) ;
661+ self . relate ( & u, & u)
662+ }
659663 ConstVariableValue :: Unknown { universe } => {
660664 if self . for_universe . can_name ( universe) {
661665 Ok ( c)
@@ -669,7 +673,22 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
669673 }
670674 }
671675 }
672- ty:: ConstKind :: Unevaluated ( ..) if self . tcx ( ) . lazy_normalization ( ) => Ok ( c) ,
676+ ty:: ConstKind :: Unevaluated ( did, substs, promoted)
677+ if self . tcx ( ) . lazy_normalization ( ) =>
678+ {
679+ // We have to generalize inference variables used in the generic substitutions,
680+ // as unevaluated consts may otherwise contain invalid inference variables.
681+ let new_substs =
682+ self . relate_with_variance ( ty:: Variance :: Invariant , & substs, & substs) ?;
683+ if new_substs != substs {
684+ Ok ( self . tcx ( ) . mk_const ( ty:: Const {
685+ ty : c. ty ,
686+ val : ty:: ConstKind :: Unevaluated ( did, new_substs, promoted) ,
687+ } ) )
688+ } else {
689+ Ok ( c)
690+ }
691+ }
673692 _ => relate:: super_relate_consts ( self , c, c) ,
674693 }
675694 }
0 commit comments