Skip to content

Commit 08fdb83

Browse files
Remove a_is_expected from combine relations
1 parent 464b3cc commit 08fdb83

File tree

9 files changed

+74
-134
lines changed

9 files changed

+74
-134
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
120120
fn relate_opaques(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
121121
let infcx = self.type_checker.infcx;
122122
debug_assert!(!infcx.next_trait_solver());
123-
let (a, b) = if self.a_is_expected() { (a, b) } else { (b, a) };
124123
// `handle_opaque_type` cannot handle subtyping, so to support subtyping
125124
// we instead eagerly generalize here. This is a bit of a mess but will go
126125
// away once we're using the new solver.

compiler/rustc_infer/src/infer/at.rs

+29-62
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub struct At<'a, 'tcx> {
4949

5050
pub struct Trace<'a, 'tcx> {
5151
at: At<'a, 'tcx>,
52-
a_is_expected: bool,
5352
trace: TypeTrace<'tcx>,
5453
}
5554

@@ -106,23 +105,6 @@ pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
106105
}
107106

108107
impl<'a, 'tcx> At<'a, 'tcx> {
109-
/// Makes `a <: b`, where `a` may or may not be expected.
110-
///
111-
/// See [`At::trace_exp`] and [`Trace::sub`] for a version of
112-
/// this method that only requires `T: Relate<'tcx>`
113-
pub fn sub_exp<T>(
114-
self,
115-
define_opaque_types: DefineOpaqueTypes,
116-
a_is_expected: bool,
117-
a: T,
118-
b: T,
119-
) -> InferResult<'tcx, ()>
120-
where
121-
T: ToTrace<'tcx>,
122-
{
123-
self.trace_exp(a_is_expected, a, b).sub(define_opaque_types, a, b)
124-
}
125-
126108
/// Makes `actual <: expected`. For example, if type-checking a
127109
/// call like `foo(x)`, where `foo: fn(i32)`, you might have
128110
/// `sup(i32, x)`, since the "expected" type is the type that
@@ -139,7 +121,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
139121
where
140122
T: ToTrace<'tcx>,
141123
{
142-
self.sub_exp(define_opaque_types, false, actual, expected)
124+
self.trace(expected, actual).sup(define_opaque_types, expected, actual)
143125
}
144126

145127
/// Makes `expected <: actual`.
@@ -155,24 +137,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
155137
where
156138
T: ToTrace<'tcx>,
157139
{
158-
self.sub_exp(define_opaque_types, true, expected, actual)
159-
}
160-
161-
/// Makes `expected <: actual`.
162-
///
163-
/// See [`At::trace_exp`] and [`Trace::eq`] for a version of
164-
/// this method that only requires `T: Relate<'tcx>`
165-
pub fn eq_exp<T>(
166-
self,
167-
define_opaque_types: DefineOpaqueTypes,
168-
a_is_expected: bool,
169-
a: T,
170-
b: T,
171-
) -> InferResult<'tcx, ()>
172-
where
173-
T: ToTrace<'tcx>,
174-
{
175-
self.trace_exp(a_is_expected, a, b).eq(define_opaque_types, a, b)
140+
self.trace(expected, actual).sub(define_opaque_types, expected, actual)
176141
}
177142

178143
/// Makes `expected <: actual`.
@@ -261,48 +226,50 @@ impl<'a, 'tcx> At<'a, 'tcx> {
261226
where
262227
T: ToTrace<'tcx>,
263228
{
264-
self.trace_exp(true, expected, actual)
229+
let trace = ToTrace::to_trace(self.cause, true, expected, actual);
230+
Trace { at: self, trace }
265231
}
232+
}
266233

267-
/// Like `trace`, but the expected value is determined by the
268-
/// boolean argument (if true, then the first argument `a` is the
269-
/// "expected" value).
270-
pub fn trace_exp<T>(self, a_is_expected: bool, a: T, b: T) -> Trace<'a, 'tcx>
234+
impl<'a, 'tcx> Trace<'a, 'tcx> {
235+
/// Makes `a <: b`.
236+
#[instrument(skip(self), level = "debug")]
237+
pub fn sub<T>(self, define_opaque_types: DefineOpaqueTypes, a: T, b: T) -> InferResult<'tcx, ()>
271238
where
272-
T: ToTrace<'tcx>,
239+
T: Relate<'tcx>,
273240
{
274-
let trace = ToTrace::to_trace(self.cause, a_is_expected, a, b);
275-
Trace { at: self, trace, a_is_expected }
241+
let Trace { at, trace } = self;
242+
let mut fields = at.infcx.combine_fields(trace, at.param_env, define_opaque_types);
243+
fields
244+
.sub()
245+
.relate(a, b)
246+
.map(move |_| InferOk { value: (), obligations: fields.obligations })
276247
}
277-
}
278248

279-
impl<'a, 'tcx> Trace<'a, 'tcx> {
280-
/// Makes `a <: b` where `a` may or may not be expected (if
281-
/// `a_is_expected` is true, then `a` is expected).
249+
/// Makes `a :> b`.
282250
#[instrument(skip(self), level = "debug")]
283-
pub fn sub<T>(self, define_opaque_types: DefineOpaqueTypes, a: T, b: T) -> InferResult<'tcx, ()>
251+
pub fn sup<T>(self, define_opaque_types: DefineOpaqueTypes, a: T, b: T) -> InferResult<'tcx, ()>
284252
where
285253
T: Relate<'tcx>,
286254
{
287-
let Trace { at, trace, a_is_expected } = self;
255+
let Trace { at, trace } = self;
288256
let mut fields = at.infcx.combine_fields(trace, at.param_env, define_opaque_types);
289257
fields
290-
.sub(a_is_expected)
258+
.sup()
291259
.relate(a, b)
292260
.map(move |_| InferOk { value: (), obligations: fields.obligations })
293261
}
294262

295-
/// Makes `a == b`; the expectation is set by the call to
296-
/// `trace()`.
263+
/// Makes `a == b`.
297264
#[instrument(skip(self), level = "debug")]
298265
pub fn eq<T>(self, define_opaque_types: DefineOpaqueTypes, a: T, b: T) -> InferResult<'tcx, ()>
299266
where
300267
T: Relate<'tcx>,
301268
{
302-
let Trace { at, trace, a_is_expected } = self;
269+
let Trace { at, trace } = self;
303270
let mut fields = at.infcx.combine_fields(trace, at.param_env, define_opaque_types);
304271
fields
305-
.equate(StructurallyRelateAliases::No, a_is_expected)
272+
.equate(StructurallyRelateAliases::No)
306273
.relate(a, b)
307274
.map(move |_| InferOk { value: (), obligations: fields.obligations })
308275
}
@@ -314,11 +281,11 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
314281
where
315282
T: Relate<'tcx>,
316283
{
317-
let Trace { at, trace, a_is_expected } = self;
284+
let Trace { at, trace } = self;
318285
debug_assert!(at.infcx.next_trait_solver());
319286
let mut fields = at.infcx.combine_fields(trace, at.param_env, DefineOpaqueTypes::No);
320287
fields
321-
.equate(StructurallyRelateAliases::Yes, a_is_expected)
288+
.equate(StructurallyRelateAliases::Yes)
322289
.relate(a, b)
323290
.map(move |_| InferOk { value: (), obligations: fields.obligations })
324291
}
@@ -328,10 +295,10 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
328295
where
329296
T: Relate<'tcx>,
330297
{
331-
let Trace { at, trace, a_is_expected } = self;
298+
let Trace { at, trace } = self;
332299
let mut fields = at.infcx.combine_fields(trace, at.param_env, define_opaque_types);
333300
fields
334-
.lub(a_is_expected)
301+
.lub()
335302
.relate(a, b)
336303
.map(move |t| InferOk { value: t, obligations: fields.obligations })
337304
}
@@ -341,10 +308,10 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
341308
where
342309
T: Relate<'tcx>,
343310
{
344-
let Trace { at, trace, a_is_expected } = self;
311+
let Trace { at, trace } = self;
345312
let mut fields = at.infcx.combine_fields(trace, at.param_env, define_opaque_types);
346313
fields
347-
.glb(a_is_expected)
314+
.glb()
348315
.relate(a, b)
349316
.map(move |t| InferOk { value: t, obligations: fields.obligations })
350317
}

compiler/rustc_infer/src/infer/opaque_types.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,7 @@ impl<'tcx> InferCtxt<'tcx> {
522522
) -> InferResult<'tcx, ()> {
523523
let mut obligations = Vec::new();
524524

525-
self.insert_hidden_type(
526-
opaque_type_key,
527-
&cause,
528-
param_env,
529-
hidden_ty,
530-
&mut obligations,
531-
)?;
525+
self.insert_hidden_type(opaque_type_key, &cause, param_env, hidden_ty, &mut obligations)?;
532526

533527
self.add_item_bounds_for_hidden_type(
534528
opaque_type_key.def_id.to_def_id(),

compiler/rustc_infer/src/infer/relate/combine.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,24 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
321321
pub fn equate<'a>(
322322
&'a mut self,
323323
structurally_relate_aliases: StructurallyRelateAliases,
324-
a_is_expected: bool,
325324
) -> TypeRelating<'a, 'infcx, 'tcx> {
326-
TypeRelating::new(self, a_is_expected, structurally_relate_aliases, ty::Invariant)
325+
TypeRelating::new(self, structurally_relate_aliases, ty::Invariant)
327326
}
328327

329-
pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> TypeRelating<'a, 'infcx, 'tcx> {
330-
TypeRelating::new(self, a_is_expected, StructurallyRelateAliases::No, ty::Covariant)
328+
pub fn sub<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
329+
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Covariant)
331330
}
332331

333-
pub fn lub<'a>(&'a mut self, a_is_expected: bool) -> Lub<'a, 'infcx, 'tcx> {
334-
Lub::new(self, a_is_expected)
332+
pub fn sup<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
333+
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Contravariant)
335334
}
336335

337-
pub fn glb<'a>(&'a mut self, a_is_expected: bool) -> Glb<'a, 'infcx, 'tcx> {
338-
Glb::new(self, a_is_expected)
336+
pub fn lub<'a>(&'a mut self) -> Lub<'a, 'infcx, 'tcx> {
337+
Lub::new(self)
338+
}
339+
340+
pub fn glb<'a>(&'a mut self) -> Glb<'a, 'infcx, 'tcx> {
341+
Glb::new(self)
339342
}
340343

341344
pub fn register_obligations(&mut self, obligations: PredicateObligations<'tcx>) {

compiler/rustc_infer/src/infer/relate/glb.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ use crate::traits::{ObligationCause, PredicateObligations};
1313
/// "Greatest lower bound" (common subtype)
1414
pub struct Glb<'combine, 'infcx, 'tcx> {
1515
fields: &'combine mut CombineFields<'infcx, 'tcx>,
16-
a_is_expected: bool,
1716
}
1817

1918
impl<'combine, 'infcx, 'tcx> Glb<'combine, 'infcx, 'tcx> {
20-
pub fn new(
21-
fields: &'combine mut CombineFields<'infcx, 'tcx>,
22-
a_is_expected: bool,
23-
) -> Glb<'combine, 'infcx, 'tcx> {
24-
Glb { fields, a_is_expected }
19+
pub fn new(fields: &'combine mut CombineFields<'infcx, 'tcx>) -> Glb<'combine, 'infcx, 'tcx> {
20+
Glb { fields }
2521
}
2622
}
2723

@@ -35,7 +31,7 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
3531
}
3632

3733
fn a_is_expected(&self) -> bool {
38-
self.a_is_expected
34+
true
3935
}
4036

4137
fn relate_with_variance<T: Relate<'tcx>>(
@@ -46,13 +42,11 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
4642
b: T,
4743
) -> RelateResult<'tcx, T> {
4844
match variance {
49-
ty::Invariant => {
50-
self.fields.equate(StructurallyRelateAliases::No, self.a_is_expected).relate(a, b)
51-
}
45+
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
5246
ty::Covariant => self.relate(a, b),
5347
// FIXME(#41044) -- not correct, need test
5448
ty::Bivariant => Ok(a),
55-
ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b),
49+
ty::Contravariant => self.fields.lub().relate(a, b),
5650
}
5751
}
5852

@@ -126,7 +120,7 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx,
126120
}
127121

128122
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
129-
let mut sub = self.fields.sub(self.a_is_expected);
123+
let mut sub = self.fields.sub();
130124
sub.relate(v, a)?;
131125
sub.relate(v, b)?;
132126
Ok(())

compiler/rustc_infer/src/infer/relate/higher_ranked.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ impl<'a, 'tcx> CombineFields<'a, 'tcx> {
4949
debug!("b_prime={:?}", sup_prime);
5050

5151
// Compare types now that bound regions have been replaced.
52-
let result = self.sub(sub_is_expected).relate(sub_prime, sup_prime);
52+
// Reorder the inputs so that the expected is passed first.
53+
let result = if sub_is_expected {
54+
self.sub().relate(sub_prime, sup_prime)
55+
} else {
56+
self.sup().relate(sup_prime, sub_prime)
57+
};
58+
5359
if result.is_ok() {
5460
debug!("OK result={result:?}");
5561
}

compiler/rustc_infer/src/infer/relate/lub.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ use rustc_span::Span;
1313
/// "Least upper bound" (common supertype)
1414
pub struct Lub<'combine, 'infcx, 'tcx> {
1515
fields: &'combine mut CombineFields<'infcx, 'tcx>,
16-
a_is_expected: bool,
1716
}
1817

1918
impl<'combine, 'infcx, 'tcx> Lub<'combine, 'infcx, 'tcx> {
20-
pub fn new(
21-
fields: &'combine mut CombineFields<'infcx, 'tcx>,
22-
a_is_expected: bool,
23-
) -> Lub<'combine, 'infcx, 'tcx> {
24-
Lub { fields, a_is_expected }
19+
pub fn new(fields: &'combine mut CombineFields<'infcx, 'tcx>) -> Lub<'combine, 'infcx, 'tcx> {
20+
Lub { fields }
2521
}
2622
}
2723

@@ -35,7 +31,7 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
3531
}
3632

3733
fn a_is_expected(&self) -> bool {
38-
self.a_is_expected
34+
true
3935
}
4036

4137
fn relate_with_variance<T: Relate<'tcx>>(
@@ -46,13 +42,11 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
4642
b: T,
4743
) -> RelateResult<'tcx, T> {
4844
match variance {
49-
ty::Invariant => {
50-
self.fields.equate(StructurallyRelateAliases::No, self.a_is_expected).relate(a, b)
51-
}
45+
ty::Invariant => self.fields.equate(StructurallyRelateAliases::No).relate(a, b),
5246
ty::Covariant => self.relate(a, b),
5347
// FIXME(#41044) -- not correct, need test
5448
ty::Bivariant => Ok(a),
55-
ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
49+
ty::Contravariant => self.fields.glb().relate(a, b),
5650
}
5751
}
5852

@@ -126,7 +120,7 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx,
126120
}
127121

128122
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
129-
let mut sub = self.fields.sub(self.a_is_expected);
123+
let mut sub = self.fields.sub();
130124
sub.relate(a, v)?;
131125
sub.relate(b, v)?;
132126
Ok(())

0 commit comments

Comments
 (0)