Skip to content

Commit aec62af

Browse files
committed
Solve rather subtle bug in replace_late_bound_regions -- we were passing the debruijn index in so that callees could construct late-bound regions at the right depth, but then the result was cached. When the cached result was used, it might be at the wrong depth. So now we don't pass the result in and instead simply adjust the depth to match the current nesting level as we go.
1 parent cf136cd commit aec62af

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/librustc/middle/infer/higher_ranked/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ pub fn skolemize_late_bound_regions<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
468468
* when higher-ranked things are involved. See `doc.rs` for more details.
469469
*/
470470

471-
let (result, map) = ty::replace_late_bound_regions(infcx.tcx, binder, |br, _| {
471+
let (result, map) = ty::replace_late_bound_regions(infcx.tcx, binder, |br| {
472472
infcx.region_vars.new_skolemized(br, &snapshot.region_vars_snapshot)
473473
});
474474

src/librustc/middle/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10561056
ty::replace_late_bound_regions(
10571057
self.tcx,
10581058
value,
1059-
|br, _| self.next_region_var(LateBoundRegion(span, br, lbrct)))
1059+
|br| self.next_region_var(LateBoundRegion(span, br, lbrct)))
10601060
}
10611061

10621062
/// See `verify_generic_bound` method in `region_inference`

src/librustc/middle/ty.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6672,7 +6672,7 @@ pub fn liberate_late_bound_regions<'tcx, T>(
66726672
{
66736673
replace_late_bound_regions(
66746674
tcx, value,
6675-
|br, _| ty::ReFree(ty::FreeRegion{scope: scope, bound_region: br})).0
6675+
|br| ty::ReFree(ty::FreeRegion{scope: scope, bound_region: br})).0
66766676
}
66776677

66786678
pub fn count_late_bound_regions<'tcx, T>(
@@ -6681,7 +6681,7 @@ pub fn count_late_bound_regions<'tcx, T>(
66816681
-> uint
66826682
where T : TypeFoldable<'tcx> + Repr<'tcx>
66836683
{
6684-
let (_, skol_map) = replace_late_bound_regions(tcx, value, |_, _| ty::ReStatic);
6684+
let (_, skol_map) = replace_late_bound_regions(tcx, value, |_| ty::ReStatic);
66856685
skol_map.len()
66866686
}
66876687

@@ -6712,7 +6712,7 @@ pub fn erase_late_bound_regions<'tcx, T>(
67126712
-> T
67136713
where T : TypeFoldable<'tcx> + Repr<'tcx>
67146714
{
6715-
replace_late_bound_regions(tcx, value, |_, _| ty::ReStatic).0
6715+
replace_late_bound_regions(tcx, value, |_| ty::ReStatic).0
67166716
}
67176717

67186718
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
@@ -6730,9 +6730,9 @@ pub fn anonymize_late_bound_regions<'tcx, T>(
67306730
where T : TypeFoldable<'tcx> + Repr<'tcx>,
67316731
{
67326732
let mut counter = 0;
6733-
ty::Binder(replace_late_bound_regions(tcx, sig, |_, db| {
6733+
ty::Binder(replace_late_bound_regions(tcx, sig, |_| {
67346734
counter += 1;
6735-
ReLateBound(db, BrAnon(counter))
6735+
ReLateBound(ty::DebruijnIndex::new(1), BrAnon(counter))
67366736
}).0)
67376737
}
67386738

@@ -6743,7 +6743,7 @@ pub fn replace_late_bound_regions<'tcx, T, F>(
67436743
mut mapf: F)
67446744
-> (T, FnvHashMap<ty::BoundRegion,ty::Region>)
67456745
where T : TypeFoldable<'tcx> + Repr<'tcx>,
6746-
F : FnMut(BoundRegion, DebruijnIndex) -> ty::Region,
6746+
F : FnMut(BoundRegion) -> ty::Region,
67476747
{
67486748
debug!("replace_late_bound_regions({})", binder.repr(tcx));
67496749

@@ -6755,8 +6755,19 @@ pub fn replace_late_bound_regions<'tcx, T, F>(
67556755
debug!("region={}", region.repr(tcx));
67566756
match region {
67576757
ty::ReLateBound(debruijn, br) if debruijn.depth == current_depth => {
6758-
* map.entry(br).get().unwrap_or_else(
6759-
|vacant_entry| vacant_entry.insert(mapf(br, debruijn)))
6758+
let region =
6759+
* map.entry(br).get().unwrap_or_else(
6760+
|vacant_entry| vacant_entry.insert(mapf(br)));
6761+
6762+
if let ty::ReLateBound(debruijn1, br) = region {
6763+
// If the callback returns a late-bound region,
6764+
// that region should always use depth 1. Then we
6765+
// adjust it to the correct depth.
6766+
assert_eq!(debruijn1.depth, 1);
6767+
ty::ReLateBound(debruijn, br)
6768+
} else {
6769+
region
6770+
}
67606771
}
67616772
_ => {
67626773
region

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,8 @@ impl<'tcx, T> UserString<'tcx> for ty::Binder<T>
11821182
// the output. We'll probably want to tweak this over time to
11831183
// decide just how much information to give.
11841184
let mut names = Vec::new();
1185-
let (unbound_value, _) = ty::replace_late_bound_regions(tcx, self, |br, debruijn| {
1186-
ty::ReLateBound(debruijn, match br {
1185+
let (unbound_value, _) = ty::replace_late_bound_regions(tcx, self, |br| {
1186+
ty::ReLateBound(ty::DebruijnIndex::new(1), match br {
11871187
ty::BrNamed(_, name) => {
11881188
names.push(token::get_name(name));
11891189
br

0 commit comments

Comments
 (0)