Skip to content

Commit e1f735a

Browse files
committed
don't repeat lifetime names from outer binder in print
1 parent f914b82 commit e1f735a

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+57-12
Original file line numberDiff line numberDiff line change
@@ -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>,
2061+
ty::DebruijnIndex,
2062+
ty::BoundRegion,
2063+
) -> ty::Region<'tcx>
2064+
+ 'a
2065+
),
20592066
}
20602067

20612068
impl<'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) => {
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,57 @@ 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+
let mut name = |db: Option<ty::DebruijnIndex>,
2250+
binder_level: ty::DebruijnIndex,
2251+
br: ty::BoundRegion| {
2252+
let (name, kind) = match br.kind {
22402253
ty::BrAnon(_) | ty::BrEnv => {
22412254
let name = next_name(&self);
2242-
do_continue(&mut self, name);
2243-
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
2255+
2256+
if let Some(db) = db {
2257+
if db > binder_level {
2258+
let kind = ty::BrNamed(CRATE_DEF_ID.to_def_id(), name);
2259+
return tcx.mk_region(ty::ReLateBound(
2260+
ty::INNERMOST,
2261+
ty::BoundRegion { var: br.var, kind },
2262+
));
2263+
}
2264+
}
2265+
2266+
(name, ty::BrNamed(CRATE_DEF_ID.to_def_id(), name))
22442267
}
22452268
ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
22462269
let name = next_name(&self);
2247-
do_continue(&mut self, name);
2248-
ty::BrNamed(def_id, name)
2270+
2271+
if let Some(db) = db {
2272+
if db > binder_level {
2273+
let kind = ty::BrNamed(def_id, name);
2274+
return tcx.mk_region(ty::ReLateBound(
2275+
ty::INNERMOST,
2276+
ty::BoundRegion { var: br.var, kind },
2277+
));
2278+
}
2279+
}
2280+
2281+
(name, ty::BrNamed(def_id, name))
22492282
}
22502283
ty::BrNamed(_, name) => {
2251-
do_continue(&mut self, name);
2252-
br.kind
2284+
if let Some(db) = db {
2285+
if db > binder_level {
2286+
let kind = br.kind;
2287+
return tcx.mk_region(ty::ReLateBound(
2288+
ty::INNERMOST,
2289+
ty::BoundRegion { var: br.var, kind },
2290+
));
2291+
}
2292+
}
2293+
2294+
(name, br.kind)
22532295
}
22542296
};
2297+
2298+
start_or_continue(&mut self, "for<", ", ");
2299+
do_continue(&mut self, name);
22552300
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind }))
22562301
};
22572302
let mut folder = RegionFolder {

0 commit comments

Comments
 (0)