@@ -357,7 +357,10 @@ where
357357{
358358 match args {
359359 None => x. instantiate_identity ( ) . skip_normalization ( ) ,
360- Some ( args) => normalize ( tcx, typing_env, x. instantiate ( tcx, args) ) ,
360+ Some ( args) => {
361+ let x = x. instantiate ( tcx, args) . skip_normalization ( ) ;
362+ normalize ( tcx, typing_env, ty:: set_aliases_to_non_rigid ( tcx, x) )
363+ }
361364 }
362365}
363366
@@ -388,10 +391,13 @@ pub fn identity_args_with_inferred<'tcx>(
388391 . type_of ( def_id)
389392 . instantiate_identity ( )
390393 . skip_normalization ( ) ;
391- match * type_of. kind ( ) {
394+ let args = match * type_of. kind ( ) {
392395 ty:: Closure ( _, args) | ty:: Coroutine ( _, args) => args,
393396 _ => unreachable ! ( ) ,
394- }
397+ } ;
398+ // `type_of` erases too much; use the parent args as a base.
399+ let parent = tcx. generics_of ( def_id) . parent . unwrap ( ) ;
400+ args. rebase_onto ( tcx, parent, ty:: GenericArgs :: identity_for_item ( tcx, parent) )
395401 }
396402 RDefKind :: AnonConst
397403 if tcx. anon_const_kind ( def_id) == ty:: AnonConstKind :: NonTypeSystemInline =>
@@ -441,6 +447,116 @@ where
441447 ( val, extra_regions)
442448}
443449
450+ /// Takes two values that differ only in their regions, where `template` has placeholder regions
451+ /// used each once in the places that they differ. Returns a mapping from those placeholders to the
452+ /// corresponding regions in `actual`.
453+ pub fn match_region_args < ' tcx , T : Relate < ty:: TyCtxt < ' tcx > > > (
454+ tcx : ty:: TyCtxt < ' tcx > ,
455+ template : T ,
456+ actual : T ,
457+ ) -> Result < HashMap < ty:: PlaceholderRegion < ' tcx > , ty:: Region < ' tcx > > , ty:: error:: TypeError < ' tcx > > {
458+ struct RegionMapping < ' tcx > {
459+ tcx : ty:: TyCtxt < ' tcx > ,
460+ args : HashMap < ty:: PlaceholderRegion < ' tcx > , ty:: Region < ' tcx > > ,
461+ depth : u32 ,
462+ }
463+
464+ impl < ' tcx > TypeRelation < ty:: TyCtxt < ' tcx > > for RegionMapping < ' tcx > {
465+ fn cx ( & self ) -> ty:: TyCtxt < ' tcx > {
466+ self . tcx
467+ }
468+
469+ fn regions (
470+ & mut self ,
471+ source : ty:: Region < ' tcx > ,
472+ target : ty:: Region < ' tcx > ,
473+ ) -> RelateResult < ' tcx , ty:: Region < ' tcx > > {
474+ if let ty:: RePlaceholder ( placeholder) = source. kind ( ) {
475+ let target =
476+ if let ty:: ReBound ( ty:: BoundVarIndexKind :: Bound ( db) , br) = target. kind ( ) {
477+ if db. as_u32 ( ) < self . depth {
478+ return Err ( ty:: error:: TypeError :: RegionsPlaceholderMismatch ) ;
479+ }
480+ ty:: Region :: new_bound ( self . tcx , db. shifted_out ( self . depth ) , br)
481+ } else {
482+ target
483+ } ;
484+ let slot = self
485+ . args
486+ . entry ( placeholder)
487+ . or_insert ( self . tcx . lifetimes . re_erased ) ;
488+ if slot. is_erased ( ) || * slot == target {
489+ * slot = target;
490+ } else {
491+ return Err ( ty:: error:: TypeError :: RegionsPlaceholderMismatch ) ;
492+ }
493+ }
494+ Ok ( source)
495+ }
496+
497+ fn relate_with_variance < T : Relate < ty:: TyCtxt < ' tcx > > > (
498+ & mut self ,
499+ _: ty:: Variance ,
500+ _: ty:: VarianceDiagInfo < ty:: TyCtxt < ' tcx > > ,
501+ a : T ,
502+ b : T ,
503+ ) -> RelateResult < ' tcx , T > {
504+ self . relate ( a, b)
505+ }
506+
507+ fn relate_ty_args (
508+ & mut self ,
509+ a_ty : ty:: Ty < ' tcx > ,
510+ _: ty:: Ty < ' tcx > ,
511+ _: rustc_span:: def_id:: DefId ,
512+ a_args : ty:: GenericArgsRef < ' tcx > ,
513+ b_args : ty:: GenericArgsRef < ' tcx > ,
514+ _: impl FnOnce ( ty:: GenericArgsRef < ' tcx > ) -> ty:: Ty < ' tcx > ,
515+ ) -> RelateResult < ' tcx , ty:: Ty < ' tcx > > {
516+ self . relate ( a_args, b_args) ?;
517+ Ok ( a_ty)
518+ }
519+
520+ fn tys ( & mut self , a : ty:: Ty < ' tcx > , b : ty:: Ty < ' tcx > ) -> RelateResult < ' tcx , ty:: Ty < ' tcx > > {
521+ structurally_relate_tys ( self , a, b)
522+ }
523+
524+ fn consts (
525+ & mut self ,
526+ a : ty:: Const < ' tcx > ,
527+ b : ty:: Const < ' tcx > ,
528+ ) -> RelateResult < ' tcx , ty:: Const < ' tcx > > {
529+ structurally_relate_consts ( self , a, b)
530+ }
531+
532+ fn binders < T > (
533+ & mut self ,
534+ a : ty:: Binder < ' tcx , T > ,
535+ b : ty:: Binder < ' tcx , T > ,
536+ ) -> RelateResult < ' tcx , ty:: Binder < ' tcx , T > >
537+ where
538+ T : Relate < ty:: TyCtxt < ' tcx > > ,
539+ {
540+ if a. bound_vars ( ) . len ( ) != b. bound_vars ( ) . len ( ) {
541+ return Err ( ty:: error:: TypeError :: Mismatch ) ;
542+ }
543+ self . depth += 1 ;
544+ let result = self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) ;
545+ self . depth -= 1 ;
546+ result?;
547+ Ok ( a)
548+ }
549+ }
550+
551+ let mut mapping = RegionMapping {
552+ tcx,
553+ args : HashMap :: default ( ) ,
554+ depth : 0 ,
555+ } ;
556+ mapping. relate ( template, actual) ?;
557+ Ok ( mapping. args )
558+ }
559+
444560#[ extension_traits:: extension( pub trait SubstBinder ) ]
445561impl < ' tcx , T : ty:: TypeFoldable < ty:: TyCtxt < ' tcx > > > ty:: Binder < ' tcx , T > {
446562 fn subst (
0 commit comments