Skip to content

Commit a4955d1

Browse files
committed
refactor NLL relate_tys to use Region internally, not RegionVid
No functional change.
1 parent 689b791 commit a4955d1

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

src/librustc_mir/borrow_check/nll/type_check/relate_tys.rs

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
use borrow_check::nll::constraints::{ConstraintCategory, OutlivesConstraint};
1212
use borrow_check::nll::type_check::{BorrowCheckContext, Locations};
13-
use borrow_check::nll::universal_regions::UniversalRegions;
14-
use borrow_check::nll::ToRegionVid;
1513
use rustc::infer::canonical::{Canonical, CanonicalVarInfos};
1614
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
1715
use rustc::traits::query::Fallible;
1816
use rustc::ty::fold::{TypeFoldable, TypeVisitor};
1917
use rustc::ty::relate::{self, Relate, RelateResult, TypeRelation};
2018
use rustc::ty::subst::Kind;
21-
use rustc::ty::{self, CanonicalTy, CanonicalVar, RegionVid, Ty, TyCtxt};
19+
use rustc::ty::{self, CanonicalTy, CanonicalVar, Ty, TyCtxt};
2220
use rustc_data_structures::fx::FxHashMap;
2321
use rustc_data_structures::indexed_vec::IndexVec;
2422

@@ -122,10 +120,10 @@ struct TypeRelating<'cx, 'bccx: 'cx, 'gcx: 'tcx, 'tcx: 'bccx> {
122120
///
123121
/// This field stores the instantiations for late-bound regions in
124122
/// the `a` type.
125-
a_scopes: Vec<BoundRegionScope>,
123+
a_scopes: Vec<BoundRegionScope<'tcx>>,
126124

127125
/// Same as `a_scopes`, but for the `b` type.
128-
b_scopes: Vec<BoundRegionScope>,
126+
b_scopes: Vec<BoundRegionScope<'tcx>>,
129127

130128
/// Where (and why) is this relation taking place?
131129
locations: Locations,
@@ -152,13 +150,13 @@ struct TypeRelating<'cx, 'bccx: 'cx, 'gcx: 'tcx, 'tcx: 'bccx> {
152150

153151
#[derive(Clone, Debug)]
154152
struct ScopesAndKind<'tcx> {
155-
scopes: Vec<BoundRegionScope>,
153+
scopes: Vec<BoundRegionScope<'tcx>>,
156154
kind: Kind<'tcx>,
157155
}
158156

159157
#[derive(Clone, Debug, Default)]
160-
struct BoundRegionScope {
161-
map: FxHashMap<ty::BoundRegion, RegionVid>,
158+
struct BoundRegionScope<'tcx> {
159+
map: FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
162160
}
163161

164162
#[derive(Copy, Clone)]
@@ -204,7 +202,7 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
204202
&mut self,
205203
value: &ty::Binder<impl TypeFoldable<'tcx>>,
206204
universally_quantified: UniversallyQuantified,
207-
) -> BoundRegionScope {
205+
) -> BoundRegionScope<'tcx> {
208206
let mut scope = BoundRegionScope::default();
209207
value.skip_binder().visit_with(&mut ScopeInstantiator {
210208
infcx: self.infcx,
@@ -227,8 +225,8 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
227225
debruijn: ty::DebruijnIndex,
228226
br: &ty::BoundRegion,
229227
first_free_index: ty::DebruijnIndex,
230-
scopes: &[BoundRegionScope],
231-
) -> RegionVid {
228+
scopes: &[BoundRegionScope<'tcx>],
229+
) -> ty::Region<'tcx> {
232230
// The debruijn index is a "reverse index" into the
233231
// scopes listing. So when we have INNERMOST (0), we
234232
// want the *last* scope pushed, and so forth.
@@ -245,28 +243,25 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
245243
/// with. Otherwise just return `r`.
246244
fn replace_bound_region(
247245
&self,
248-
universal_regions: &UniversalRegions<'tcx>,
249246
r: ty::Region<'tcx>,
250247
first_free_index: ty::DebruijnIndex,
251-
scopes: &[BoundRegionScope],
252-
) -> RegionVid {
253-
match r {
254-
ty::ReLateBound(debruijn, br) => {
255-
Self::lookup_bound_region(*debruijn, br, first_free_index, scopes)
256-
}
257-
258-
ty::ReVar(v) => *v,
259-
260-
_ => universal_regions.to_region_vid(r),
248+
scopes: &[BoundRegionScope<'tcx>],
249+
) -> ty::Region<'tcx> {
250+
if let ty::ReLateBound(debruijn, br) = r {
251+
Self::lookup_bound_region(*debruijn, br, first_free_index, scopes)
252+
} else {
253+
r
261254
}
262255
}
263256

264257
/// Push a new outlives requirement into our output set of
265258
/// constraints.
266-
fn push_outlives(&mut self, sup: RegionVid, sub: RegionVid) {
259+
fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>) {
267260
debug!("push_outlives({:?}: {:?})", sup, sub);
268261

269262
if let Some(borrowck_context) = &mut self.borrowck_context {
263+
let sub = borrowck_context.universal_regions.to_region_vid(sub);
264+
let sup = borrowck_context.universal_regions.to_region_vid(sup);
270265
borrowck_context
271266
.constraints
272267
.outlives_constraints
@@ -316,10 +311,7 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelating<'cx, 'bccx, 'gcx, 'tcx> {
316311
return result;
317312
}
318313

319-
fn generalize_value(
320-
&self,
321-
kind: Kind<'tcx>,
322-
) -> Kind<'tcx> {
314+
fn generalize_value(&self, kind: Kind<'tcx>) -> Kind<'tcx> {
323315
TypeGeneralizer {
324316
type_rel: self,
325317
first_free_index: ty::INNERMOST,
@@ -397,37 +389,30 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx>
397389
a: ty::Region<'tcx>,
398390
b: ty::Region<'tcx>,
399391
) -> RelateResult<'tcx, ty::Region<'tcx>> {
400-
if let Some(&mut BorrowCheckContext {
401-
universal_regions, ..
402-
}) = self.borrowck_context
403-
{
404-
if let ty::ReCanonical(var) = a {
405-
self.relate_var(*var, b.into())?;
406-
return Ok(a);
407-
}
392+
if let ty::ReCanonical(var) = a {
393+
self.relate_var(*var, b.into())?;
394+
return Ok(a);
395+
}
408396

409-
debug!(
410-
"regions(a={:?}, b={:?}, variance={:?})",
411-
a, b, self.ambient_variance
412-
);
397+
debug!(
398+
"regions(a={:?}, b={:?}, variance={:?})",
399+
a, b, self.ambient_variance
400+
);
413401

414-
let v_a =
415-
self.replace_bound_region(universal_regions, a, ty::INNERMOST, &self.a_scopes);
416-
let v_b =
417-
self.replace_bound_region(universal_regions, b, ty::INNERMOST, &self.b_scopes);
402+
let v_a = self.replace_bound_region(a, ty::INNERMOST, &self.a_scopes);
403+
let v_b = self.replace_bound_region(b, ty::INNERMOST, &self.b_scopes);
418404

419-
debug!("regions: v_a = {:?}", v_a);
420-
debug!("regions: v_b = {:?}", v_b);
405+
debug!("regions: v_a = {:?}", v_a);
406+
debug!("regions: v_b = {:?}", v_b);
421407

422-
if self.ambient_covariance() {
423-
// Covariance: a <= b. Hence, `b: a`.
424-
self.push_outlives(v_b, v_a);
425-
}
408+
if self.ambient_covariance() {
409+
// Covariance: a <= b. Hence, `b: a`.
410+
self.push_outlives(v_b, v_a);
411+
}
426412

427-
if self.ambient_contravariance() {
428-
// Contravariant: b <= a. Hence, `a: b`.
429-
self.push_outlives(v_a, v_b);
430-
}
413+
if self.ambient_contravariance() {
414+
// Contravariant: b <= a. Hence, `a: b`.
415+
self.push_outlives(v_a, v_b);
431416
}
432417

433418
Ok(a)
@@ -527,10 +512,8 @@ impl<'cx, 'bccx, 'gcx, 'tcx> TypeRelation<'cx, 'gcx, 'tcx>
527512

528513
// Reset ambient variance to contravariance. See the
529514
// covariant case above for an explanation.
530-
let variance = ::std::mem::replace(
531-
&mut self.ambient_variance,
532-
ty::Variance::Contravariant,
533-
);
515+
let variance =
516+
::std::mem::replace(&mut self.ambient_variance, ty::Variance::Contravariant);
534517

535518
self.relate(a.skip_binder(), b.skip_binder())?;
536519

@@ -556,7 +539,7 @@ struct ScopeInstantiator<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
556539
// The debruijn index of the scope we are instantiating.
557540
target_index: ty::DebruijnIndex,
558541
universally_quantified: UniversallyQuantified,
559-
bound_region_scope: &'cx mut BoundRegionScope,
542+
bound_region_scope: &'cx mut BoundRegionScope<'tcx>,
560543
}
561544

562545
impl<'cx, 'gcx, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'cx, 'gcx, 'tcx> {
@@ -583,7 +566,7 @@ impl<'cx, 'gcx, 'tcx> TypeVisitor<'tcx> for ScopeInstantiator<'cx, 'gcx, 'tcx> {
583566
} else {
584567
NLLRegionVariableOrigin::Existential
585568
};
586-
infcx.next_nll_region_var(origin).to_region_vid()
569+
infcx.next_nll_region_var(origin)
587570
});
588571
}
589572

0 commit comments

Comments
 (0)