@@ -2055,7 +2055,14 @@ struct RegionFolder<'a, 'tcx> {
20552055 tcx : TyCtxt < ' tcx > ,
20562056 current_index : ty:: DebruijnIndex ,
20572057 region_map : BTreeMap < ty:: BoundRegion , ty:: Region < ' tcx > > ,
2058- name : & ' a mut ( dyn FnMut ( ty:: BoundRegion ) -> ty:: Region < ' tcx > + ' a ) ,
2058+ name : & ' a mut (
2059+ dyn FnMut (
2060+ Option < ty:: DebruijnIndex > , // Debruijn index of the folded late-bound region
2061+ ty:: DebruijnIndex , // Index corresponding to binder level
2062+ ty:: BoundRegion ,
2063+ ) -> ty:: Region < ' tcx >
2064+ + ' a
2065+ ) ,
20592066}
20602067
20612068impl < ' a , ' tcx > ty:: TypeFolder < ' tcx > for RegionFolder < ' a , ' tcx > {
@@ -2086,7 +2093,9 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
20862093 fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
20872094 let name = & mut self . name ;
20882095 let region = match * r {
2089- ty:: ReLateBound ( _, br) => * self . region_map . entry ( br) . or_insert_with ( || name ( br) ) ,
2096+ ty:: ReLateBound ( db, br) if db >= self . current_index => {
2097+ * self . region_map . entry ( br) . or_insert_with ( || name ( Some ( db) , self . current_index , br) )
2098+ }
20902099 ty:: RePlaceholder ( ty:: PlaceholderRegion { name : kind, .. } ) => {
20912100 // If this is an anonymous placeholder, don't rename. Otherwise, in some
20922101 // async fns, we get a `for<'r> Send` bound
@@ -2095,7 +2104,10 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
20952104 _ => {
20962105 // Index doesn't matter, since this is just for naming and these never get bound
20972106 let br = ty:: BoundRegion { var : ty:: BoundVar :: from_u32 ( 0 ) , kind } ;
2098- * self . region_map . entry ( br) . or_insert_with ( || name ( br) )
2107+ * self
2108+ . region_map
2109+ . entry ( br)
2110+ . or_insert_with ( || name ( None , self . current_index , br) )
20992111 }
21002112 }
21012113 }
@@ -2234,24 +2246,63 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
22342246 } )
22352247 } else {
22362248 let tcx = self . tcx ;
2237- let mut name = |br : ty:: BoundRegion | {
2238- start_or_continue ( & mut self , "for<" , ", " ) ;
2239- let kind = match br. kind {
2249+
2250+ // Closure used in `RegionFolder` to create names for anonymous late-bound
2251+ // regions. We use two `DebruijnIndex`es (one for the currently folded
2252+ // late-bound region and the other for the binder level) to determine
2253+ // whether a name has already been created for the currently folded region,
2254+ // see issue #102392.
2255+ let mut name = |lifetime_idx : Option < ty:: DebruijnIndex > ,
2256+ binder_level_idx : ty:: DebruijnIndex ,
2257+ br : ty:: BoundRegion | {
2258+ let ( name, kind) = match br. kind {
22402259 ty:: BrAnon ( _) | ty:: BrEnv => {
22412260 let name = next_name ( & self ) ;
2242- do_continue ( & mut self , name) ;
2243- ty:: BrNamed ( CRATE_DEF_ID . to_def_id ( ) , name)
2261+
2262+ if let Some ( lt_idx) = lifetime_idx {
2263+ if lt_idx > binder_level_idx {
2264+ let kind = ty:: BrNamed ( CRATE_DEF_ID . to_def_id ( ) , name) ;
2265+ return tcx. mk_region ( ty:: ReLateBound (
2266+ ty:: INNERMOST ,
2267+ ty:: BoundRegion { var : br. var , kind } ,
2268+ ) ) ;
2269+ }
2270+ }
2271+
2272+ ( name, ty:: BrNamed ( CRATE_DEF_ID . to_def_id ( ) , name) )
22442273 }
22452274 ty:: BrNamed ( def_id, kw:: UnderscoreLifetime ) => {
22462275 let name = next_name ( & self ) ;
2247- do_continue ( & mut self , name) ;
2248- ty:: BrNamed ( def_id, name)
2276+
2277+ if let Some ( lt_idx) = lifetime_idx {
2278+ if lt_idx > binder_level_idx {
2279+ let kind = ty:: BrNamed ( def_id, name) ;
2280+ return tcx. mk_region ( ty:: ReLateBound (
2281+ ty:: INNERMOST ,
2282+ ty:: BoundRegion { var : br. var , kind } ,
2283+ ) ) ;
2284+ }
2285+ }
2286+
2287+ ( name, ty:: BrNamed ( def_id, name) )
22492288 }
22502289 ty:: BrNamed ( _, name) => {
2251- do_continue ( & mut self , name) ;
2252- br. kind
2290+ if let Some ( lt_idx) = lifetime_idx {
2291+ if lt_idx > binder_level_idx {
2292+ let kind = br. kind ;
2293+ return tcx. mk_region ( ty:: ReLateBound (
2294+ ty:: INNERMOST ,
2295+ ty:: BoundRegion { var : br. var , kind } ,
2296+ ) ) ;
2297+ }
2298+ }
2299+
2300+ ( name, br. kind )
22532301 }
22542302 } ;
2303+
2304+ start_or_continue ( & mut self , "for<" , ", " ) ;
2305+ do_continue ( & mut self , name) ;
22552306 tcx. mk_region ( ty:: ReLateBound ( ty:: INNERMOST , ty:: BoundRegion { var : br. var , kind } ) )
22562307 } ;
22572308 let mut folder = RegionFolder {
0 commit comments