Skip to content

Commit 81f458e

Browse files
committed
Remove LatticeDir trait.
It's no longer necessary now that the `glb` and `lub` modules have been merged.
1 parent d1000c2 commit 81f458e

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 {
@@ -168,9 +78,70 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
16878
}
16979
}
17080

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

176147
#[instrument(skip(self), level = "trace")]
@@ -226,15 +197,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for LatticeOp<'_, '_, 'tcx> {
226197
}
227198
}
228199

229-
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for LatticeOp<'combine, 'infcx, 'tcx> {
230-
fn infcx(&self) -> &'infcx InferCtxt<'tcx> {
231-
self.fields.infcx
232-
}
233-
234-
fn cause(&self) -> &ObligationCause<'tcx> {
235-
&self.fields.trace.cause
236-
}
237-
200+
impl<'combine, 'infcx, 'tcx> LatticeOp<'combine, 'infcx, 'tcx> {
201+
// Relates the type `v` to `a` and `b` such that `v` represents
202+
// the LUB/GLB of `a` and `b` as appropriate.
203+
//
204+
// Subtle hack: ordering *may* be significant here. This method
205+
// relates `v` to `a` first, which may help us to avoid unnecessary
206+
// type variable obligations. See caller for details.
238207
fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
239208
let mut sub = self.fields.sub();
240209
match self.kind {
@@ -249,10 +218,6 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for LatticeOp<'combine, 'i
249218
}
250219
Ok(())
251220
}
252-
253-
fn define_opaque_types(&self) -> DefineOpaqueTypes {
254-
self.fields.define_opaque_types
255-
}
256221
}
257222

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

0 commit comments

Comments
 (0)