Skip to content

Commit 3d3ed33

Browse files
committed
Remove LatticeDir trait.
It's no longer necessary now that the `glb` and `lub` modules have been merged.
1 parent 22f28a7 commit 3d3ed33

File tree

1 file changed

+69
-104
lines changed

1 file changed

+69
-104
lines changed

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

Lines changed: 69 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -26,96 +26,6 @@ use tracing::{debug, instrument};
2626
use super::StructurallyRelateAliases;
2727
use super::combine::{CombineFields, PredicateEmittingRelation};
2828
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
29-
use crate::traits::ObligationCause;
30-
31-
/// Trait for returning data about a lattice, and for abstracting
32-
/// over the "direction" of the lattice operation (LUB/GLB).
33-
///
34-
/// GLB moves "down" the lattice (to smaller values); LUB moves
35-
/// "up" the lattice (to bigger values).
36-
trait LatticeDir<'f, 'tcx>: PredicateEmittingRelation<InferCtxt<'tcx>> {
37-
fn infcx(&self) -> &'f InferCtxt<'tcx>;
38-
39-
fn cause(&self) -> &ObligationCause<'tcx>;
40-
41-
fn define_opaque_types(&self) -> DefineOpaqueTypes;
42-
43-
// Relates the type `v` to `a` and `b` such that `v` represents
44-
// the LUB/GLB of `a` and `b` as appropriate.
45-
//
46-
// Subtle hack: ordering *may* be significant here. This method
47-
// relates `v` to `a` first, which may help us to avoid unnecessary
48-
// type variable obligations. See caller for details.
49-
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()>;
50-
}
51-
52-
/// Relates two types using a given lattice.
53-
#[instrument(skip(this), level = "debug")]
54-
fn super_lattice_tys<'a, 'tcx: 'a, L>(
55-
this: &mut L,
56-
a: Ty<'tcx>,
57-
b: Ty<'tcx>,
58-
) -> RelateResult<'tcx, Ty<'tcx>>
59-
where
60-
L: LatticeDir<'a, 'tcx>,
61-
{
62-
if a == b {
63-
return Ok(a);
64-
}
65-
66-
let infcx = this.infcx();
67-
68-
let a = infcx.shallow_resolve(a);
69-
let b = infcx.shallow_resolve(b);
70-
71-
match (a.kind(), b.kind()) {
72-
// If one side is known to be a variable and one is not,
73-
// create a variable (`v`) to represent the LUB. Make sure to
74-
// relate `v` to the non-type-variable first (by passing it
75-
// first to `relate_bound`). Otherwise, we would produce a
76-
// subtype obligation that must then be processed.
77-
//
78-
// Example: if the LHS is a type variable, and RHS is
79-
// `Box<i32>`, then we current compare `v` to the RHS first,
80-
// which will instantiate `v` with `Box<i32>`. Then when `v`
81-
// is compared to the LHS, we instantiate LHS with `Box<i32>`.
82-
// But if we did in reverse order, we would create a `v <:
83-
// LHS` (or vice versa) constraint and then instantiate
84-
// `v`. This would require further processing to achieve same
85-
// end-result; in particular, this screws up some of the logic
86-
// in coercion, which expects LUB to figure out that the LHS
87-
// is (e.g.) `Box<i32>`. A more obvious solution might be to
88-
// iterate on the subtype obligations that are returned, but I
89-
// think this suffices. -nmatsakis
90-
(&ty::Infer(TyVar(..)), _) => {
91-
let v = infcx.next_ty_var(this.cause().span);
92-
this.relate_bound(v, b, a)?;
93-
Ok(v)
94-
}
95-
(_, &ty::Infer(TyVar(..))) => {
96-
let v = infcx.next_ty_var(this.cause().span);
97-
this.relate_bound(v, a, b)?;
98-
Ok(v)
99-
}
100-
101-
(
102-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
103-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
104-
) if a_def_id == b_def_id => infcx.super_combine_tys(this, a, b),
105-
106-
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
107-
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
108-
if this.define_opaque_types() == DefineOpaqueTypes::Yes
109-
&& def_id.is_local()
110-
&& !this.infcx().next_trait_solver() =>
111-
{
112-
this.register_goals(infcx.handle_opaque_type(a, b, this.span(), this.param_env())?);
113-
Ok(a)
114-
}
115-
116-
_ => infcx.super_combine_tys(this, a, b),
117-
}
118-
}
11929

12030
#[derive(Clone, Copy)]
12131
pub(crate) enum LatticeOpKind {
@@ -159,9 +69,70 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
15969
}
16070
}
16171

72+
/// Relates two types using a given lattice.
16273
#[instrument(skip(self), level = "trace")]
16374
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
164-
super_lattice_tys(self, a, b)
75+
if a == b {
76+
return Ok(a);
77+
}
78+
79+
let infcx = self.fields.infcx;
80+
81+
let a = infcx.shallow_resolve(a);
82+
let b = infcx.shallow_resolve(b);
83+
84+
match (a.kind(), b.kind()) {
85+
// If one side is known to be a variable and one is not,
86+
// create a variable (`v`) to represent the LUB. Make sure to
87+
// relate `v` to the non-type-variable first (by passing it
88+
// first to `relate_bound`). Otherwise, we would produce a
89+
// subtype obligation that must then be processed.
90+
//
91+
// Example: if the LHS is a type variable, and RHS is
92+
// `Box<i32>`, then we current compare `v` to the RHS first,
93+
// which will instantiate `v` with `Box<i32>`. Then when `v`
94+
// is compared to the LHS, we instantiate LHS with `Box<i32>`.
95+
// But if we did in reverse order, we would create a `v <:
96+
// LHS` (or vice versa) constraint and then instantiate
97+
// `v`. This would require further processing to achieve same
98+
// end-result; in particular, this screws up some of the logic
99+
// in coercion, which expects LUB to figure out that the LHS
100+
// is (e.g.) `Box<i32>`. A more obvious solution might be to
101+
// iterate on the subtype obligations that are returned, but I
102+
// think this suffices. -nmatsakis
103+
(&ty::Infer(TyVar(..)), _) => {
104+
let v = infcx.next_ty_var(self.fields.trace.cause.span);
105+
self.relate_bound(v, b, a)?;
106+
Ok(v)
107+
}
108+
(_, &ty::Infer(TyVar(..))) => {
109+
let v = infcx.next_ty_var(self.fields.trace.cause.span);
110+
self.relate_bound(v, a, b)?;
111+
Ok(v)
112+
}
113+
114+
(
115+
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
116+
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
117+
) if a_def_id == b_def_id => infcx.super_combine_tys(self, a, b),
118+
119+
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
120+
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
121+
if self.fields.define_opaque_types == DefineOpaqueTypes::Yes
122+
&& def_id.is_local()
123+
&& !infcx.next_trait_solver() =>
124+
{
125+
self.register_goals(infcx.handle_opaque_type(
126+
a,
127+
b,
128+
self.span(),
129+
self.param_env(),
130+
)?);
131+
Ok(a)
132+
}
133+
134+
_ => infcx.super_combine_tys(self, a, b),
135+
}
165136
}
166137

167138
#[instrument(skip(self), level = "trace")]
@@ -217,15 +188,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
217188
}
218189
}
219190

220-
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for LatticeOp<'combine, 'infcx, 'tcx> {
221-
fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
222-
self.fields.infcx
223-
}
224-
225-
fn cause(&self) -> &ObligationCause<'tcx> {
226-
&self.fields.trace.cause
227-
}
228-
191+
impl<'combine, 'infcx, 'tcx> LatticeOp<'combine, 'infcx, 'tcx> {
192+
// Relates the type `v` to `a` and `b` such that `v` represents
193+
// the LUB/GLB of `a` and `b` as appropriate.
194+
//
195+
// Subtle hack: ordering *may* be significant here. This method
196+
// relates `v` to `a` first, which may help us to avoid unnecessary
197+
// type variable obligations. See caller for details.
229198
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
230199
let mut sub = self.fields.sub();
231200
match self.kind {
@@ -240,10 +209,6 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for LatticeOp<'combine, 'i
240209
}
241210
Ok(())
242211
}
243-
244-
fn define_opaque_types(&self) -> DefineOpaqueTypes {
245-
self.fields.define_opaque_types
246-
}
247212
}
248213

249214
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {

0 commit comments

Comments
 (0)