@@ -2055,7 +2055,14 @@ struct RegionFolder<'a, 'tcx> {
2055
2055
tcx : TyCtxt < ' tcx > ,
2056
2056
current_index : ty:: DebruijnIndex ,
2057
2057
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
+ ) ,
2059
2066
}
2060
2067
2061
2068
impl < ' a , ' tcx > ty:: TypeFolder < ' tcx > for RegionFolder < ' a , ' tcx > {
@@ -2086,7 +2093,9 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
2086
2093
fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
2087
2094
let name = & mut self . name ;
2088
2095
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
+ }
2090
2099
ty:: RePlaceholder ( ty:: PlaceholderRegion { name : kind, .. } ) => {
2091
2100
// If this is an anonymous placeholder, don't rename. Otherwise, in some
2092
2101
// async fns, we get a `for<'r> Send` bound
@@ -2095,7 +2104,10 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
2095
2104
_ => {
2096
2105
// Index doesn't matter, since this is just for naming and these never get bound
2097
2106
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) )
2099
2111
}
2100
2112
}
2101
2113
}
@@ -2234,24 +2246,63 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2234
2246
} )
2235
2247
} else {
2236
2248
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 {
2240
2259
ty:: BrAnon ( _) | ty:: BrEnv => {
2241
2260
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) )
2244
2273
}
2245
2274
ty:: BrNamed ( def_id, kw:: UnderscoreLifetime ) => {
2246
2275
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) )
2249
2288
}
2250
2289
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 )
2253
2301
}
2254
2302
} ;
2303
+
2304
+ start_or_continue ( & mut self , "for<" , ", " ) ;
2305
+ do_continue ( & mut self , name) ;
2255
2306
tcx. mk_region ( ty:: ReLateBound ( ty:: INNERMOST , ty:: BoundRegion { var : br. var , kind } ) )
2256
2307
} ;
2257
2308
let mut folder = RegionFolder {
0 commit comments