Skip to content

Commit 4f34524

Browse files
committed
Move leak_check into its own method, and ensure that all higher-ranked code is in
`higher_ranked.rs`.
1 parent 1205fd8 commit 4f34524

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Helper routines for higher-ranked things. See the `doc` module at
1212
//! the end of the file for details.
1313
14-
use super::{CombinedSnapshot, cres, InferCtxt, HigherRankedType};
14+
use super::{CombinedSnapshot, cres, InferCtxt, HigherRankedType, SkolemizationMap};
1515
use super::combine::{Combine, Combineable};
1616

1717
use middle::ty::{mod, Binder};
@@ -81,7 +81,7 @@ impl<'tcx,C> HigherRankedRelations<'tcx> for C
8181

8282
// Presuming type comparison succeeds, we need to check
8383
// that the skolemized regions do not "leak".
84-
match leak_check(self.infcx(), &skol_map, snapshot) {
84+
match self.infcx().leak_check(&skol_map, snapshot) {
8585
Ok(()) => { }
8686
Err((skol_br, tainted_region)) => {
8787
if self.a_is_expected() {
@@ -455,11 +455,47 @@ impl<'a,'tcx> InferCtxtExt<'tcx> for InferCtxt<'a,'tcx> {
455455
}
456456
}
457457

458-
fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
459-
skol_map: &FnvHashMap<ty::BoundRegion,ty::Region>,
460-
snapshot: &CombinedSnapshot)
461-
-> Result<(),(ty::BoundRegion,ty::Region)>
458+
pub fn skolemize_late_bound_regions<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
459+
binder: &ty::Binder<T>,
460+
snapshot: &CombinedSnapshot)
461+
-> (T, SkolemizationMap)
462+
where T : TypeFoldable<'tcx> + Repr<'tcx>
462463
{
464+
/*!
465+
* Replace all regions bound by `binder` with skolemized regions and
466+
* return a map indicating which bound-region was replaced with what
467+
* skolemized region. This is the first step of checking subtyping
468+
* when higher-ranked things are involved. See `doc.rs` for more details.
469+
*/
470+
471+
let (result, map) = ty::replace_late_bound_regions(infcx.tcx, binder, |br, _| {
472+
infcx.region_vars.new_skolemized(br, &snapshot.region_vars_snapshot)
473+
});
474+
475+
debug!("skolemize_bound_regions(binder={}, result={}, map={})",
476+
binder.repr(infcx.tcx),
477+
result.repr(infcx.tcx),
478+
map.repr(infcx.tcx));
479+
480+
(result, map)
481+
}
482+
483+
pub fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
484+
skol_map: &SkolemizationMap,
485+
snapshot: &CombinedSnapshot)
486+
-> Result<(),(ty::BoundRegion,ty::Region)>
487+
{
488+
/*!
489+
* Searches the region constriants created since `snapshot` was started
490+
* and checks to determine whether any of the skolemized regions created
491+
* in `skol_map` would "escape" -- meaning that they are related to
492+
* other regions in some way. If so, the higher-ranked subtyping doesn't
493+
* hold. See `doc.rs` for more details.
494+
*/
495+
496+
debug!("leak_check: skol_map={}",
497+
skol_map.repr(infcx.tcx));
498+
463499
let new_vars = infcx.region_vars_confined_to_snapshot(snapshot);
464500
for (&skol_br, &skol) in skol_map.iter() {
465501
let tainted = infcx.tainted_regions(snapshot, skol);
@@ -475,6 +511,11 @@ fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
475511
}
476512
};
477513

514+
debug!("{} (which replaced {}) is tainted by {}",
515+
skol.repr(infcx.tcx),
516+
skol_br.repr(infcx.tcx),
517+
tainted_region.repr(infcx.tcx));
518+
478519
// A is not as polymorphic as B:
479520
return Err((skol_br, tainted_region));
480521
}

src/librustc/middle/infer/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub mod doc;
5252
pub mod equate;
5353
pub mod error_reporting;
5454
pub mod glb;
55-
pub mod higher_ranked;
55+
mod higher_ranked;
5656
pub mod lattice;
5757
pub mod lub;
5858
pub mod region_inference;
@@ -90,7 +90,7 @@ pub struct InferCtxt<'a, 'tcx: 'a> {
9090
RegionVarBindings<'a, 'tcx>,
9191
}
9292

93-
/// A map returned by `skolemize_bound_regions()` indicating the skolemized
93+
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
9494
/// region that each late-bound region was replaced with.
9595
pub type SkolemizationMap = FnvHashMap<ty::BoundRegion,ty::Region>;
9696

@@ -709,16 +709,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
709709
-> (T, SkolemizationMap)
710710
where T : TypeFoldable<'tcx> + Repr<'tcx>
711711
{
712-
let (result, map) = replace_late_bound_regions(self.tcx, value, |br, _| {
713-
self.region_vars.new_skolemized(br, &snapshot.region_vars_snapshot)
714-
});
712+
/*! See `higher_ranked::skolemize_late_bound_regions` */
715713

716-
debug!("skolemize_bound_regions(value={}, result={}, map={})",
717-
value.repr(self.tcx),
718-
result.repr(self.tcx),
719-
map.repr(self.tcx));
714+
higher_ranked::skolemize_late_bound_regions(self, value, snapshot)
715+
}
716+
717+
pub fn leak_check(&self,
718+
skol_map: &SkolemizationMap,
719+
snapshot: &CombinedSnapshot)
720+
-> Result<(),(ty::BoundRegion,ty::Region)>
721+
{
722+
/*! See `higher_ranked::leak_check` */
720723

721-
(result, map)
724+
higher_ranked::leak_check(self, skol_map, snapshot)
722725
}
723726

724727
pub fn next_ty_var_id(&self, diverging: bool) -> TyVid {

0 commit comments

Comments
 (0)