@@ -735,6 +735,89 @@ impl<T> RegionBinder<T> {
735735 let args = RegionArgs :: erased ( self . regions . len ( ) ) ;
736736 self . apply ( args)
737737 }
738+
739+ /// Infer how the bound regions of `self` appear in `target`. The returned binder binds the
740+ /// same regions as `target`; other lifetimes may differ.
741+ pub fn infer_region_mapping ( & self , target : & Self ) -> Option < RegionBinder < RegionArgs > >
742+ where
743+ T : TyVisitable ,
744+ {
745+ struct RegionMapping {
746+ regions : IndexVec < RegionId , Option < Region > > ,
747+ depth : DeBruijnId ,
748+ }
749+
750+ impl Visitor for RegionMapping {
751+ type Break = ( ) ;
752+ }
753+
754+ impl ZipAst for RegionMapping {
755+ fn visit_region_param (
756+ & mut self ,
757+ _source : & RegionParam ,
758+ _target : & RegionParam ,
759+ ) -> ControlFlow < ( ) > {
760+ // Binder positions identify regions; names and metadata need not match.
761+ ControlFlow :: Continue ( ( ) )
762+ }
763+
764+ fn visit_region ( & mut self , source : & Region , target : & Region ) -> ControlFlow < ( ) > {
765+ if let ( Region :: Lub ( source) , Region :: Lub ( target) ) = ( source, target) {
766+ return self . visit ( source, target) ;
767+ }
768+ // We're only concerned with mapping bound variables.
769+ if let Region :: Var ( var @ DeBruijnVar :: Bound ( depth, _) ) = source
770+ && * depth <= self . depth
771+ {
772+ if let Some ( id) = var. bound_at_depth ( self . depth ) {
773+ let Some ( target) = target. clone ( ) . move_from_under_binders ( self . depth )
774+ else {
775+ return ControlFlow :: Break ( ( ) ) ;
776+ } ;
777+ let mapped = & mut self . regions [ id] ;
778+ if let Some ( previous) = mapped
779+ && * previous != target
780+ {
781+ return ControlFlow :: Break ( ( ) ) ;
782+ }
783+ * mapped = Some ( target) ;
784+ ControlFlow :: Continue ( ( ) )
785+ } else if source != target {
786+ ControlFlow :: Break ( ( ) )
787+ } else {
788+ ControlFlow :: Continue ( ( ) )
789+ }
790+ } else {
791+ ControlFlow :: Continue ( ( ) )
792+ }
793+ }
794+
795+ fn visit_region_binder < T : AstVisitable > (
796+ & mut self ,
797+ source : & RegionBinder < T > ,
798+ target : & RegionBinder < T > ,
799+ ) -> ControlFlow < ( ) > {
800+ self . depth = self . depth . incr ( ) ;
801+ let result = self . visit_inner ( source, target) ;
802+ self . depth = self . depth . decr ( ) ;
803+ result
804+ }
805+ }
806+
807+ let mut mapping = RegionMapping {
808+ regions : self . regions . map_ref_indexed ( |_, _| None ) ,
809+ depth : DeBruijnId :: zero ( ) ,
810+ } ;
811+ if ZipAst :: visit ( & mut mapping, & self . skip_binder , & target. skip_binder ) . is_break ( ) {
812+ return None ;
813+ }
814+ let regions = RegionArgs :: new ( mapping. regions . into_iter ( ) . collect :: < Option < _ > > ( ) ?) ;
815+ Some ( RegionBinder {
816+ regions : target. regions . clone ( ) ,
817+ outlives : target. outlives . clone ( ) ,
818+ skip_binder : regions,
819+ } )
820+ }
738821}
739822
740823impl < Id : TypeLevelVar > std:: ops:: Index < Id > for GenericArgs {
0 commit comments