Skip to content

Commit dddaf34

Browse files
Move a_is_expected out of CombineFields
1 parent 5e39032 commit dddaf34

File tree

8 files changed

+86
-82
lines changed

8 files changed

+86
-82
lines changed

src/librustc/infer/bivariate.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ use ty::TyVar;
3333
use ty::relate::{Relate, RelateResult, TypeRelation};
3434

3535
pub struct Bivariate<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
36-
fields: CombineFields<'infcx, 'gcx, 'tcx>
36+
fields: CombineFields<'infcx, 'gcx, 'tcx>,
37+
a_is_expected: bool,
3738
}
3839

3940
impl<'infcx, 'gcx, 'tcx> Bivariate<'infcx, 'gcx, 'tcx> {
40-
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Bivariate<'infcx, 'gcx, 'tcx> {
41-
Bivariate { fields: fields }
41+
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Bivariate<'infcx, 'gcx, 'tcx> {
42+
Bivariate { fields: fields, a_is_expected: a_is_expected }
4243
}
4344
}
4445

@@ -47,7 +48,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Bivariate<'infcx,
4748

4849
fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
4950

50-
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
51+
fn a_is_expected(&self) -> bool { self.a_is_expected }
5152

5253
fn relate_with_variance<T: Relate<'tcx>>(&mut self,
5354
variance: ty::Variance,
@@ -86,12 +87,12 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Bivariate<'infcx,
8687
}
8788

8889
(&ty::TyInfer(TyVar(a_id)), _) => {
89-
self.fields.instantiate(b, BiTo, a_id)?;
90+
self.fields.instantiate(b, BiTo, a_id, self.a_is_expected)?;
9091
Ok(a)
9192
}
9293

9394
(_, &ty::TyInfer(TyVar(b_id))) => {
94-
self.fields.instantiate(a, BiTo, b_id)?;
95+
self.fields.instantiate(a, BiTo, b_id, self.a_is_expected)?;
9596
Ok(a)
9697
}
9798

src/librustc/infer/combine.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use syntax_pos::Span;
5454
#[derive(Clone)]
5555
pub struct CombineFields<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
5656
pub infcx: &'infcx InferCtxt<'infcx, 'gcx, 'tcx>,
57-
pub a_is_expected: bool,
5857
pub trace: TypeTrace<'tcx>,
5958
pub cause: Option<ty::relate::Cause>,
6059
pub obligations: PredicateObligations<'tcx>,
@@ -155,37 +154,31 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
155154
self.infcx.tcx
156155
}
157156

158-
pub fn switch_expected(&self) -> CombineFields<'infcx, 'gcx, 'tcx> {
159-
CombineFields {
160-
a_is_expected: !self.a_is_expected,
161-
..(*self).clone()
162-
}
163-
}
164-
165-
pub fn equate(&self) -> Equate<'infcx, 'gcx, 'tcx> {
166-
Equate::new(self.clone())
157+
pub fn equate(&self, a_is_expected: bool) -> Equate<'infcx, 'gcx, 'tcx> {
158+
Equate::new(self.clone(), a_is_expected)
167159
}
168160

169-
pub fn bivariate(&self) -> Bivariate<'infcx, 'gcx, 'tcx> {
170-
Bivariate::new(self.clone())
161+
pub fn bivariate(&self, a_is_expected: bool) -> Bivariate<'infcx, 'gcx, 'tcx> {
162+
Bivariate::new(self.clone(), a_is_expected)
171163
}
172164

173-
pub fn sub(&self) -> Sub<'infcx, 'gcx, 'tcx> {
174-
Sub::new(self.clone())
165+
pub fn sub(&self, a_is_expected: bool) -> Sub<'infcx, 'gcx, 'tcx> {
166+
Sub::new(self.clone(), a_is_expected)
175167
}
176168

177-
pub fn lub(&self) -> Lub<'infcx, 'gcx, 'tcx> {
178-
Lub::new(self.clone())
169+
pub fn lub(&self, a_is_expected: bool) -> Lub<'infcx, 'gcx, 'tcx> {
170+
Lub::new(self.clone(), a_is_expected)
179171
}
180172

181-
pub fn glb(&self) -> Glb<'infcx, 'gcx, 'tcx> {
182-
Glb::new(self.clone())
173+
pub fn glb(&self, a_is_expected: bool) -> Glb<'infcx, 'gcx, 'tcx> {
174+
Glb::new(self.clone(), a_is_expected)
183175
}
184176

185177
pub fn instantiate(&self,
186178
a_ty: Ty<'tcx>,
187179
dir: RelationDir,
188-
b_vid: ty::TyVid)
180+
b_vid: ty::TyVid,
181+
a_is_expected: bool)
189182
-> RelateResult<'tcx, ()>
190183
{
191184
let mut stack = Vec::new();
@@ -255,10 +248,10 @@ impl<'infcx, 'gcx, 'tcx> CombineFields<'infcx, 'gcx, 'tcx> {
255248
// to associate causes/spans with each of the relations in
256249
// the stack to get this right.
257250
match dir {
258-
BiTo => self.bivariate().relate(&a_ty, &b_ty),
259-
EqTo => self.equate().relate(&a_ty, &b_ty),
260-
SubtypeOf => self.sub().relate(&a_ty, &b_ty),
261-
SupertypeOf => self.sub().relate_with_variance(ty::Contravariant, &a_ty, &b_ty),
251+
BiTo => self.bivariate(a_is_expected).relate(&a_ty, &b_ty),
252+
EqTo => self.equate(a_is_expected).relate(&a_ty, &b_ty),
253+
SubtypeOf => self.sub(a_is_expected).relate(&a_ty, &b_ty),
254+
SupertypeOf => self.sub(a_is_expected).relate_with_variance(ty::Contravariant, &a_ty, &b_ty),
262255
}?;
263256
}
264257

src/librustc/infer/equate.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ use traits::PredicateObligations;
1919

2020
/// Ensures `a` is made equal to `b`. Returns `a` on success.
2121
pub struct Equate<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
22-
fields: CombineFields<'infcx, 'gcx, 'tcx>
22+
fields: CombineFields<'infcx, 'gcx, 'tcx>,
23+
a_is_expected: bool,
2324
}
2425

2526
impl<'infcx, 'gcx, 'tcx> Equate<'infcx, 'gcx, 'tcx> {
26-
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Equate<'infcx, 'gcx, 'tcx> {
27-
Equate { fields: fields }
27+
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Equate<'infcx, 'gcx, 'tcx> {
28+
Equate { fields: fields, a_is_expected: a_is_expected }
2829
}
2930

3031
pub fn obligations(self) -> PredicateObligations<'tcx> {
@@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc
3738

3839
fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
3940

40-
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
41+
fn a_is_expected(&self) -> bool { self.a_is_expected }
4142

4243
fn relate_with_variance<T: Relate<'tcx>>(&mut self,
4344
_: ty::Variance,
@@ -63,12 +64,12 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc
6364
}
6465

6566
(&ty::TyInfer(TyVar(a_id)), _) => {
66-
self.fields.instantiate(b, EqTo, a_id)?;
67+
self.fields.instantiate(b, EqTo, a_id, self.a_is_expected)?;
6768
Ok(a)
6869
}
6970

7071
(_, &ty::TyInfer(TyVar(b_id))) => {
71-
self.fields.instantiate(a, EqTo, b_id)?;
72+
self.fields.instantiate(a, EqTo, b_id, self.a_is_expected)?;
7273
Ok(a)
7374
}
7475

@@ -93,7 +94,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gc
9394
-> RelateResult<'tcx, ty::Binder<T>>
9495
where T: Relate<'tcx>
9596
{
96-
self.fields.higher_ranked_sub(a, b)?;
97-
self.fields.higher_ranked_sub(b, a)
97+
self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
98+
self.fields.higher_ranked_sub(b, a, self.a_is_expected)
9899
}
99100
}

src/librustc/infer/glb.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ use traits::PredicateObligations;
1919

2020
/// "Greatest lower bound" (common subtype)
2121
pub struct Glb<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
22-
fields: CombineFields<'infcx, 'gcx, 'tcx>
22+
fields: CombineFields<'infcx, 'gcx, 'tcx>,
23+
a_is_expected: bool,
2324
}
2425

2526
impl<'infcx, 'gcx, 'tcx> Glb<'infcx, 'gcx, 'tcx> {
26-
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Glb<'infcx, 'gcx, 'tcx> {
27-
Glb { fields: fields }
27+
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Glb<'infcx, 'gcx, 'tcx> {
28+
Glb { fields: fields, a_is_expected: a_is_expected }
2829
}
2930

3031
pub fn obligations(self) -> PredicateObligations<'tcx> {
@@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx,
3738

3839
fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
3940

40-
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
41+
fn a_is_expected(&self) -> bool { self.a_is_expected }
4142

4243
fn relate_with_variance<T: Relate<'tcx>>(&mut self,
4344
variance: ty::Variance,
@@ -46,10 +47,10 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx,
4647
-> RelateResult<'tcx, T>
4748
{
4849
match variance {
49-
ty::Invariant => self.fields.equate().relate(a, b),
50+
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
5051
ty::Covariant => self.relate(a, b),
51-
ty::Bivariant => self.fields.bivariate().relate(a, b),
52-
ty::Contravariant => self.fields.lub().relate(a, b),
52+
ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b),
53+
ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b),
5354
}
5455
}
5556

@@ -71,7 +72,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx,
7172
-> RelateResult<'tcx, ty::Binder<T>>
7273
where T: Relate<'tcx>
7374
{
74-
self.fields.higher_ranked_glb(a, b)
75+
self.fields.higher_ranked_glb(a, b, self.a_is_expected)
7576
}
7677
}
7778

@@ -81,7 +82,7 @@ impl<'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx> for Glb<'infcx, 'gcx, 't
8182
}
8283

8384
fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
84-
let mut sub = self.fields.sub();
85+
let mut sub = self.fields.sub(self.a_is_expected);
8586
sub.relate(&v, &a)?;
8687
sub.relate(&v, &b)?;
8788
Ok(())

src/librustc/infer/higher_ranked/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct HrMatchResult<U> {
4040
}
4141

4242
impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
43-
pub fn higher_ranked_sub<T>(&self, a: &Binder<T>, b: &Binder<T>)
43+
pub fn higher_ranked_sub<T>(&self, a: &Binder<T>, b: &Binder<T>, a_is_expected: bool)
4444
-> RelateResult<'tcx, Binder<T>>
4545
where T: Relate<'tcx>
4646
{
@@ -77,11 +77,11 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
7777
debug!("b_prime={:?}", b_prime);
7878

7979
// Compare types now that bound regions have been replaced.
80-
let result = self.sub().relate(&a_prime, &b_prime)?;
80+
let result = self.sub(a_is_expected).relate(&a_prime, &b_prime)?;
8181

8282
// Presuming type comparison succeeds, we need to check
8383
// that the skolemized regions do not "leak".
84-
self.infcx.leak_check(!self.a_is_expected, span, &skol_map, snapshot)?;
84+
self.infcx.leak_check(!a_is_expected, span, &skol_map, snapshot)?;
8585

8686
// We are finished with the skolemized regions now so pop
8787
// them off.
@@ -109,7 +109,8 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
109109
pub fn higher_ranked_match<T, U>(&self,
110110
span: Span,
111111
a_pair: &Binder<(T, U)>,
112-
b_match: &T)
112+
b_match: &T,
113+
a_is_expected: bool)
113114
-> RelateResult<'tcx, HrMatchResult<U>>
114115
where T: Relate<'tcx>,
115116
U: TypeFoldable<'tcx>
@@ -129,7 +130,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
129130
debug!("higher_ranked_match: skol_map={:?}", skol_map);
130131

131132
// Equate types now that bound regions have been replaced.
132-
try!(self.equate().relate(&a_match, &b_match));
133+
try!(self.equate(a_is_expected).relate(&a_match, &b_match));
133134

134135
// Map each skolemized region to a vector of other regions that it
135136
// must be equated with. (Note that this vector may include other
@@ -221,7 +222,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
221222
});
222223
}
223224

224-
pub fn higher_ranked_lub<T>(&self, a: &Binder<T>, b: &Binder<T>)
225+
pub fn higher_ranked_lub<T>(&self, a: &Binder<T>, b: &Binder<T>, a_is_expected: bool)
225226
-> RelateResult<'tcx, Binder<T>>
226227
where T: Relate<'tcx>
227228
{
@@ -239,7 +240,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
239240

240241
// Collect constraints.
241242
let result0 =
242-
self.lub().relate(&a_with_fresh, &b_with_fresh)?;
243+
self.lub(a_is_expected).relate(&a_with_fresh, &b_with_fresh)?;
243244
let result0 =
244245
self.infcx.resolve_type_vars_if_possible(&result0);
245246
debug!("lub result0 = {:?}", result0);
@@ -311,7 +312,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
311312
}
312313
}
313314

314-
pub fn higher_ranked_glb<T>(&self, a: &Binder<T>, b: &Binder<T>)
315+
pub fn higher_ranked_glb<T>(&self, a: &Binder<T>, b: &Binder<T>, a_is_expected: bool)
315316
-> RelateResult<'tcx, Binder<T>>
316317
where T: Relate<'tcx>
317318
{
@@ -333,7 +334,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
333334

334335
// Collect constraints.
335336
let result0 =
336-
self.glb().relate(&a_with_fresh, &b_with_fresh)?;
337+
self.glb(a_is_expected).relate(&a_with_fresh, &b_with_fresh)?;
337338
let result0 =
338339
self.infcx.resolve_type_vars_if_possible(&result0);
339340
debug!("glb result0 = {:?}", result0);

src/librustc/infer/lub.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ use traits::PredicateObligations;
1919

2020
/// "Least upper bound" (common supertype)
2121
pub struct Lub<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
22-
fields: CombineFields<'infcx, 'gcx, 'tcx>
22+
fields: CombineFields<'infcx, 'gcx, 'tcx>,
23+
a_is_expected: bool,
2324
}
2425

2526
impl<'infcx, 'gcx, 'tcx> Lub<'infcx, 'gcx, 'tcx> {
26-
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Lub<'infcx, 'gcx, 'tcx> {
27-
Lub { fields: fields }
27+
pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) -> Lub<'infcx, 'gcx, 'tcx> {
28+
Lub { fields: fields, a_is_expected: a_is_expected }
2829
}
2930

3031
pub fn obligations(self) -> PredicateObligations<'tcx> {
@@ -37,7 +38,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx,
3738

3839
fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
3940

40-
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
41+
fn a_is_expected(&self) -> bool { self.a_is_expected }
4142

4243
fn relate_with_variance<T: Relate<'tcx>>(&mut self,
4344
variance: ty::Variance,
@@ -46,10 +47,10 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx,
4647
-> RelateResult<'tcx, T>
4748
{
4849
match variance {
49-
ty::Invariant => self.fields.equate().relate(a, b),
50+
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
5051
ty::Covariant => self.relate(a, b),
51-
ty::Bivariant => self.fields.bivariate().relate(a, b),
52-
ty::Contravariant => self.fields.glb().relate(a, b),
52+
ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b),
53+
ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
5354
}
5455
}
5556

@@ -71,7 +72,7 @@ impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx,
7172
-> RelateResult<'tcx, ty::Binder<T>>
7273
where T: Relate<'tcx>
7374
{
74-
self.fields.higher_ranked_lub(a, b)
75+
self.fields.higher_ranked_lub(a, b, self.a_is_expected)
7576
}
7677
}
7778

@@ -81,7 +82,7 @@ impl<'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx> for Lub<'infcx, 'gcx, 't
8182
}
8283

8384
fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
84-
let mut sub = self.fields.sub();
85+
let mut sub = self.fields.sub(self.a_is_expected);
8586
sub.relate(&a, &v)?;
8687
sub.relate(&b, &v)?;
8788
Ok(())

0 commit comments

Comments
 (0)