@@ -654,3 +654,49 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
654654 false
655655 }
656656}
657+
658+ ///////////////////////////////////////////////////////////////////////////
659+ // ReScope Anonymizer
660+
661+ impl < ' a , ' gcx , ' tcx > TyCtxt < ' a , ' gcx , ' tcx > {
662+
663+ /// Transforms RegionKind::ReScope values into equivalent
664+ /// RegionKind::ReScopeAnon values. This should be done before exporting to
665+ /// crate metadata (as long as we can't use erase_regions() on everything
666+ /// that is exported).
667+ pub fn anonymize_scope_regions < T > ( self , value : & T ) -> T
668+ where T : TypeFoldable < ' tcx >
669+ {
670+ use rustc_data_structures:: stable_hasher:: { StableHasher , HashStable } ;
671+ use ich:: { StableHashingContext , Fingerprint } ;
672+
673+ let hcx = StableHashingContext :: new ( self ) ;
674+
675+ return value. fold_with ( & mut AnonReScopes {
676+ tcx : self ,
677+ hcx,
678+ } ) ;
679+
680+ struct AnonReScopes < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
681+ tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
682+ hcx : StableHashingContext < ' a , ' gcx , ' tcx > ,
683+ }
684+
685+ impl < ' a , ' gcx , ' tcx > TypeFolder < ' gcx , ' tcx > for AnonReScopes < ' a , ' gcx , ' tcx > {
686+
687+ fn tcx < ' b > ( & ' b self ) -> TyCtxt < ' b , ' gcx , ' tcx > { self . tcx }
688+
689+ fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
690+ match * r {
691+ ty:: ReScope ( code_extent) => {
692+ let mut hasher = StableHasher :: new ( ) ;
693+ code_extent. hash_stable ( & mut self . hcx , & mut hasher) ;
694+ let code_extent_fingerprint: Fingerprint = hasher. finish ( ) ;
695+ self . tcx . mk_region ( ty:: ReScopeAnon ( code_extent_fingerprint) )
696+ } ,
697+ _ => r,
698+ }
699+ }
700+ }
701+ }
702+ }
0 commit comments