Skip to content

Commit f8ea24e

Browse files
committed
rustc: Avoid free functions taking &TyCtxt and &InferCtxt.
1 parent d7ee56e commit f8ea24e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+765
-1003
lines changed

src/librustc/infer/bivariate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! In particular, it might be enough to say (A,B) are bivariant for
2626
//! all (A,B).
2727
28-
use super::combine::{self, CombineFields};
28+
use super::combine::CombineFields;
2929
use super::type_variable::{BiTo};
3030

3131
use ty::{self, Ty, TyCtxt};
@@ -96,7 +96,7 @@ impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Bivariate<'a, 'tcx> {
9696
}
9797

9898
_ => {
99-
combine::super_combine_tys(self.fields.infcx, self, a, b)
99+
self.fields.infcx.super_combine_tys(self, a, b)
100100
}
101101
}
102102
}

src/librustc/infer/combine.rs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,50 +60,51 @@ pub struct CombineFields<'a, 'tcx: 'a> {
6060
pub obligations: PredicateObligations<'tcx>,
6161
}
6262

63-
pub fn super_combine_tys<'a,'tcx:'a,R>(infcx: &InferCtxt<'a, 'tcx>,
64-
relation: &mut R,
65-
a: Ty<'tcx>,
66-
b: Ty<'tcx>)
67-
-> RelateResult<'tcx, Ty<'tcx>>
63+
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
64+
pub fn super_combine_tys<R>(&self,
65+
relation: &mut R,
66+
a: Ty<'tcx>,
67+
b: Ty<'tcx>)
68+
-> RelateResult<'tcx, Ty<'tcx>>
6869
where R: TypeRelation<'a,'tcx>
6970
{
7071
let a_is_expected = relation.a_is_expected();
7172

7273
match (&a.sty, &b.sty) {
7374
// Relate integral variables to other types
7475
(&ty::TyInfer(ty::IntVar(a_id)), &ty::TyInfer(ty::IntVar(b_id))) => {
75-
infcx.int_unification_table
76-
.borrow_mut()
77-
.unify_var_var(a_id, b_id)
78-
.map_err(|e| int_unification_error(a_is_expected, e))?;
76+
self.int_unification_table
77+
.borrow_mut()
78+
.unify_var_var(a_id, b_id)
79+
.map_err(|e| int_unification_error(a_is_expected, e))?;
7980
Ok(a)
8081
}
8182
(&ty::TyInfer(ty::IntVar(v_id)), &ty::TyInt(v)) => {
82-
unify_integral_variable(infcx, a_is_expected, v_id, IntType(v))
83+
self.unify_integral_variable(a_is_expected, v_id, IntType(v))
8384
}
8485
(&ty::TyInt(v), &ty::TyInfer(ty::IntVar(v_id))) => {
85-
unify_integral_variable(infcx, !a_is_expected, v_id, IntType(v))
86+
self.unify_integral_variable(!a_is_expected, v_id, IntType(v))
8687
}
8788
(&ty::TyInfer(ty::IntVar(v_id)), &ty::TyUint(v)) => {
88-
unify_integral_variable(infcx, a_is_expected, v_id, UintType(v))
89+
self.unify_integral_variable(a_is_expected, v_id, UintType(v))
8990
}
9091
(&ty::TyUint(v), &ty::TyInfer(ty::IntVar(v_id))) => {
91-
unify_integral_variable(infcx, !a_is_expected, v_id, UintType(v))
92+
self.unify_integral_variable(!a_is_expected, v_id, UintType(v))
9293
}
9394

9495
// Relate floating-point variables to other types
9596
(&ty::TyInfer(ty::FloatVar(a_id)), &ty::TyInfer(ty::FloatVar(b_id))) => {
96-
infcx.float_unification_table
97-
.borrow_mut()
98-
.unify_var_var(a_id, b_id)
99-
.map_err(|e| float_unification_error(relation.a_is_expected(), e))?;
97+
self.float_unification_table
98+
.borrow_mut()
99+
.unify_var_var(a_id, b_id)
100+
.map_err(|e| float_unification_error(relation.a_is_expected(), e))?;
100101
Ok(a)
101102
}
102103
(&ty::TyInfer(ty::FloatVar(v_id)), &ty::TyFloat(v)) => {
103-
unify_float_variable(infcx, a_is_expected, v_id, v)
104+
self.unify_float_variable(a_is_expected, v_id, v)
104105
}
105106
(&ty::TyFloat(v), &ty::TyInfer(ty::FloatVar(v_id))) => {
106-
unify_float_variable(infcx, !a_is_expected, v_id, v)
107+
self.unify_float_variable(!a_is_expected, v_id, v)
107108
}
108109

109110
// All other cases of inference are errors
@@ -119,33 +120,34 @@ pub fn super_combine_tys<'a,'tcx:'a,R>(infcx: &InferCtxt<'a, 'tcx>,
119120
}
120121
}
121122

122-
fn unify_integral_variable<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
123-
vid_is_expected: bool,
124-
vid: ty::IntVid,
125-
val: ty::IntVarValue)
126-
-> RelateResult<'tcx, Ty<'tcx>>
123+
fn unify_integral_variable(&self,
124+
vid_is_expected: bool,
125+
vid: ty::IntVid,
126+
val: ty::IntVarValue)
127+
-> RelateResult<'tcx, Ty<'tcx>>
127128
{
128-
infcx.int_unification_table
129-
.borrow_mut()
130-
.unify_var_value(vid, val)
131-
.map_err(|e| int_unification_error(vid_is_expected, e))?;
129+
self.int_unification_table
130+
.borrow_mut()
131+
.unify_var_value(vid, val)
132+
.map_err(|e| int_unification_error(vid_is_expected, e))?;
132133
match val {
133-
IntType(v) => Ok(infcx.tcx.mk_mach_int(v)),
134-
UintType(v) => Ok(infcx.tcx.mk_mach_uint(v)),
134+
IntType(v) => Ok(self.tcx.mk_mach_int(v)),
135+
UintType(v) => Ok(self.tcx.mk_mach_uint(v)),
135136
}
136137
}
137138

138-
fn unify_float_variable<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
139-
vid_is_expected: bool,
140-
vid: ty::FloatVid,
141-
val: ast::FloatTy)
142-
-> RelateResult<'tcx, Ty<'tcx>>
139+
fn unify_float_variable(&self,
140+
vid_is_expected: bool,
141+
vid: ty::FloatVid,
142+
val: ast::FloatTy)
143+
-> RelateResult<'tcx, Ty<'tcx>>
143144
{
144-
infcx.float_unification_table
145-
.borrow_mut()
146-
.unify_var_value(vid, val)
147-
.map_err(|e| float_unification_error(vid_is_expected, e))?;
148-
Ok(infcx.tcx.mk_mach_float(val))
145+
self.float_unification_table
146+
.borrow_mut()
147+
.unify_var_value(vid, val)
148+
.map_err(|e| float_unification_error(vid_is_expected, e))?;
149+
Ok(self.tcx.mk_mach_float(val))
150+
}
149151
}
150152

151153
impl<'a, 'tcx> CombineFields<'a, 'tcx> {

src/librustc/infer/equate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::combine::{self, CombineFields};
11+
use super::combine::CombineFields;
1212
use super::higher_ranked::HigherRankedRelations;
1313
use super::{Subtype};
1414
use super::type_variable::{EqTo};
@@ -74,7 +74,7 @@ impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
7474
}
7575

7676
_ => {
77-
combine::super_combine_tys(self.fields.infcx, self, a, b)?;
77+
self.fields.infcx.super_combine_tys(self, a, b)?;
7878
Ok(a)
7979
}
8080
}

src/librustc/infer/higher_ranked/mod.rs

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ pub trait HigherRankedRelations<'a,'tcx> {
3131
where T: Relate<'a,'tcx>;
3232
}
3333

34-
trait InferCtxtExt {
35-
fn tainted_regions(&self, snapshot: &CombinedSnapshot, r: ty::Region) -> Vec<ty::Region>;
36-
37-
fn region_vars_confined_to_snapshot(&self,
38-
snapshot: &CombinedSnapshot)
39-
-> Vec<ty::RegionVid>;
40-
}
41-
4234
impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
4335
fn higher_ranked_sub<T>(&self, a: &Binder<T>, b: &Binder<T>)
4436
-> RelateResult<'tcx, Binder<T>>
@@ -79,23 +71,9 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
7971

8072
// Presuming type comparison succeeds, we need to check
8173
// that the skolemized regions do not "leak".
82-
match leak_check(self.infcx, &skol_map, snapshot) {
83-
Ok(()) => { }
84-
Err((skol_br, tainted_region)) => {
85-
if self.a_is_expected {
86-
debug!("Not as polymorphic!");
87-
return Err(TypeError::RegionsInsufficientlyPolymorphic(skol_br,
88-
tainted_region));
89-
} else {
90-
debug!("Overly polymorphic!");
91-
return Err(TypeError::RegionsOverlyPolymorphic(skol_br,
92-
tainted_region));
93-
}
94-
}
95-
}
74+
self.infcx.leak_check(!self.a_is_expected, &skol_map, snapshot)?;
9675

97-
debug!("higher_ranked_sub: OK result={:?}",
98-
result);
76+
debug!("higher_ranked_sub: OK result={:?}", result);
9977

10078
Ok(ty::Binder(result))
10179
});
@@ -371,7 +349,7 @@ fn fold_regions_in<'tcx, T, F>(tcx: &TyCtxt<'tcx>,
371349
})
372350
}
373351

374-
impl<'a,'tcx> InferCtxtExt for InferCtxt<'a,'tcx> {
352+
impl<'a,'tcx> InferCtxt<'a,'tcx> {
375353
fn tainted_regions(&self, snapshot: &CombinedSnapshot, r: ty::Region) -> Vec<ty::Region> {
376354
self.region_vars.tainted(&snapshot.region_vars_snapshot, r)
377355
}
@@ -452,12 +430,11 @@ impl<'a,'tcx> InferCtxtExt for InferCtxt<'a,'tcx> {
452430

453431
region_vars
454432
}
455-
}
456433

457-
pub fn skolemize_late_bound_regions<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
458-
binder: &ty::Binder<T>,
459-
snapshot: &CombinedSnapshot)
460-
-> (T, SkolemizationMap)
434+
pub fn skolemize_late_bound_regions<T>(&self,
435+
binder: &ty::Binder<T>,
436+
snapshot: &CombinedSnapshot)
437+
-> (T, SkolemizationMap)
461438
where T : TypeFoldable<'tcx>
462439
{
463440
/*!
@@ -468,8 +445,8 @@ pub fn skolemize_late_bound_regions<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
468445
* details.
469446
*/
470447

471-
let (result, map) = infcx.tcx.replace_late_bound_regions(binder, |br| {
472-
infcx.region_vars.new_skolemized(br, &snapshot.region_vars_snapshot)
448+
let (result, map) = self.tcx.replace_late_bound_regions(binder, |br| {
449+
self.region_vars.new_skolemized(br, &snapshot.region_vars_snapshot)
473450
});
474451

475452
debug!("skolemize_bound_regions(binder={:?}, result={:?}, map={:?})",
@@ -480,10 +457,11 @@ pub fn skolemize_late_bound_regions<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
480457
(result, map)
481458
}
482459

483-
pub fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
484-
skol_map: &SkolemizationMap,
485-
snapshot: &CombinedSnapshot)
486-
-> Result<(),(ty::BoundRegion,ty::Region)>
460+
pub fn leak_check(&self,
461+
overly_polymorphic: bool,
462+
skol_map: &SkolemizationMap,
463+
snapshot: &CombinedSnapshot)
464+
-> RelateResult<'tcx, ()>
487465
{
488466
/*!
489467
* Searches the region constriants created since `snapshot` was started
@@ -496,9 +474,9 @@ pub fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
496474
debug!("leak_check: skol_map={:?}",
497475
skol_map);
498476

499-
let new_vars = infcx.region_vars_confined_to_snapshot(snapshot);
477+
let new_vars = self.region_vars_confined_to_snapshot(snapshot);
500478
for (&skol_br, &skol) in skol_map {
501-
let tainted = infcx.tainted_regions(snapshot, skol);
479+
let tainted = self.tainted_regions(snapshot, skol);
502480
for &tainted_region in &tainted {
503481
// Each skolemized should only be relatable to itself
504482
// or new variables:
@@ -516,8 +494,15 @@ pub fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
516494
skol_br,
517495
tainted_region);
518496

519-
// A is not as polymorphic as B:
520-
return Err((skol_br, tainted_region));
497+
if overly_polymorphic {
498+
debug!("Overly polymorphic!");
499+
return Err(TypeError::RegionsOverlyPolymorphic(skol_br,
500+
tainted_region));
501+
} else {
502+
debug!("Not as polymorphic!");
503+
return Err(TypeError::RegionsInsufficientlyPolymorphic(skol_br,
504+
tainted_region));
505+
}
521506
}
522507
}
523508
Ok(())
@@ -551,14 +536,13 @@ pub fn leak_check<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>,
551536
/// replace `'0` with a late-bound region `'a`. The depth is matched
552537
/// to the depth of the predicate, in this case 1, so that the final
553538
/// predicate is `for<'a> &'a int : Clone`.
554-
pub fn plug_leaks<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
555-
skol_map: SkolemizationMap,
556-
snapshot: &CombinedSnapshot,
557-
value: &T)
558-
-> T
539+
pub fn plug_leaks<T>(&self,
540+
skol_map: SkolemizationMap,
541+
snapshot: &CombinedSnapshot,
542+
value: &T) -> T
559543
where T : TypeFoldable<'tcx>
560544
{
561-
debug_assert!(leak_check(infcx, &skol_map, snapshot).is_ok());
545+
debug_assert!(self.leak_check(false, &skol_map, snapshot).is_ok());
562546

563547
debug!("plug_leaks(skol_map={:?}, value={:?})",
564548
skol_map,
@@ -572,7 +556,7 @@ pub fn plug_leaks<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
572556
skol_map
573557
.into_iter()
574558
.flat_map(|(skol_br, skol)| {
575-
infcx.tainted_regions(snapshot, skol)
559+
self.tainted_regions(snapshot, skol)
576560
.into_iter()
577561
.map(move |tainted_region| (tainted_region, skol_br))
578562
})
@@ -583,14 +567,14 @@ pub fn plug_leaks<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
583567

584568
// Remove any instantiated type variables from `value`; those can hide
585569
// references to regions from the `fold_regions` code below.
586-
let value = infcx.resolve_type_vars_if_possible(value);
570+
let value = self.resolve_type_vars_if_possible(value);
587571

588572
// Map any skolemization byproducts back to a late-bound
589573
// region. Put that late-bound region at whatever the outermost
590574
// binder is that we encountered in `value`. The caller is
591575
// responsible for ensuring that (a) `value` contains at least one
592576
// binder and (b) that binder is the one we want to use.
593-
let result = infcx.tcx.fold_regions(&value, &mut false, |r, current_depth| {
577+
let result = self.tcx.fold_regions(&value, &mut false, |r, current_depth| {
594578
match inv_skol_map.get(&r) {
595579
None => r,
596580
Some(br) => {
@@ -612,3 +596,4 @@ pub fn plug_leaks<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>,
612596

613597
result
614598
}
599+
}

src/librustc/infer/lattice.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
//! over a `LatticeValue`, which is a value defined with respect to
3030
//! a lattice.
3131
32-
use super::combine;
3332
use super::InferCtxt;
3433

3534
use ty::TyVar;
@@ -78,7 +77,7 @@ pub fn super_lattice_tys<'a,'tcx,L:LatticeDir<'a,'tcx>>(this: &mut L,
7877
}
7978

8079
_ => {
81-
combine::super_combine_tys(this.infcx(), this, a, b)
80+
infcx.super_combine_tys(this, a, b)
8281
}
8382
}
8483
}

0 commit comments

Comments
 (0)