-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't repeat lifetime names from outer binder in print #102514
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, // Debruijn index of the folded late-bound region | ||
ty::DebruijnIndex, // Index corresponding to binder level | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that the issue why we're not printing the binder in the We have both a late-bound region with name I am not too confident in this code and ideally we stop printing placeholders as if they were bound regions. @jackh726 do you have an idea how this should work or any expectations for how difficult it is to remove the placeholder hack all-together There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the reason this isn't printed is that we skip the binder in the error reporting code?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, we shouldn't do that. Either |
||
} | ||
} | ||
} | ||
|
@@ -2234,24 +2246,63 @@ 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 { | ||
|
||
// Closure used in `RegionFolder` to create names for anonymous late-bound | ||
// regions. We use two `DebruijnIndex`es (one for the currently folded | ||
// late-bound region and the other for the binder level) to determine | ||
// whether a name has already been created for the currently folded region, | ||
// see issue #102392. | ||
let mut name = |lifetime_idx: Option<ty::DebruijnIndex>, | ||
binder_level_idx: 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(lt_idx) = lifetime_idx { | ||
if lt_idx > binder_level_idx { | ||
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(lt_idx) = lifetime_idx { | ||
if lt_idx > binder_level_idx { | ||
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(lt_idx) = lifetime_idx { | ||
if lt_idx > binder_level_idx { | ||
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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn g(f: for<'a> fn(fn(&str, &'a str))) -> bool { | ||
f | ||
//~^ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-102392.rs:2:5 | ||
| | ||
LL | fn g(f: for<'a> fn(fn(&str, &'a str))) -> bool { | ||
| ---- expected `bool` because of return type | ||
LL | f | ||
| ^ expected `bool`, found fn pointer | ||
| | ||
= note: expected type `bool` | ||
found fn pointer `for<'a> fn(for<'b> fn(&'b str, &'a str))` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's weird, why is this not
we shouldn't replace late-bound regions of inner binders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(ah, that's whatyou're currently doing inside of
name
. i think it's better to do this here instead)