don't repeat lifetime names from outer binder in print

This commit is contained in:
b-naber 2022-09-29 18:33:58 +02:00
parent f914b82a75
commit e1f735a5d1

View File

@ -2055,7 +2055,14 @@ struct RegionFolder<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
current_index: ty::DebruijnIndex,
region_map: BTreeMap<ty::BoundRegion, ty::Region<'tcx>>,
name: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
name: &'a mut (
dyn FnMut(
Option<ty::DebruijnIndex>,
ty::DebruijnIndex,
ty::BoundRegion,
) -> ty::Region<'tcx>
+ 'a
),
}
impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
@ -2086,7 +2093,9 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
let name = &mut self.name;
let region = match *r {
ty::ReLateBound(_, br) => *self.region_map.entry(br).or_insert_with(|| name(br)),
ty::ReLateBound(db, br) => {
*self.region_map.entry(br).or_insert_with(|| name(Some(db), self.current_index, br))
}
ty::RePlaceholder(ty::PlaceholderRegion { name: kind, .. }) => {
// If this is an anonymous placeholder, don't rename. Otherwise, in some
// async fns, we get a `for<'r> Send` bound
@ -2095,7 +2104,10 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
_ => {
// Index doesn't matter, since this is just for naming and these never get bound
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind };
*self.region_map.entry(br).or_insert_with(|| name(br))
*self
.region_map
.entry(br)
.or_insert_with(|| name(None, self.current_index, br))
}
}
}
@ -2234,24 +2246,57 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
})
} else {
let tcx = self.tcx;
let mut name = |br: ty::BoundRegion| {
start_or_continue(&mut self, "for<", ", ");
let kind = match br.kind {
let mut name = |db: Option<ty::DebruijnIndex>,
binder_level: ty::DebruijnIndex,
br: ty::BoundRegion| {
let (name, kind) = match br.kind {
ty::BrAnon(_) | ty::BrEnv => {
let name = next_name(&self);
do_continue(&mut self, name);
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
if let Some(db) = db {
if db > binder_level {
let kind = ty::BrNamed(CRATE_DEF_ID.to_def_id(), name);
return tcx.mk_region(ty::ReLateBound(
ty::INNERMOST,
ty::BoundRegion { var: br.var, kind },
));
}
}
(name, ty::BrNamed(CRATE_DEF_ID.to_def_id(), name))
}
ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
let name = next_name(&self);
do_continue(&mut self, name);
ty::BrNamed(def_id, name)
if let Some(db) = db {
if db > binder_level {
let kind = ty::BrNamed(def_id, name);
return tcx.mk_region(ty::ReLateBound(
ty::INNERMOST,
ty::BoundRegion { var: br.var, kind },
));
}
}
(name, ty::BrNamed(def_id, name))
}
ty::BrNamed(_, name) => {
do_continue(&mut self, name);
br.kind
if let Some(db) = db {
if db > binder_level {
let kind = br.kind;
return tcx.mk_region(ty::ReLateBound(
ty::INNERMOST,
ty::BoundRegion { var: br.var, kind },
));
}
}
(name, br.kind)
}
};
start_or_continue(&mut self, "for<", ", ");
do_continue(&mut self, name);
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind }))
};
let mut folder = RegionFolder {