Skip to content

Commit c9dfb7f

Browse files
super-tupledaboross
authored andcommitted
Apply suggestions from code review
Refactored some of the logic for relating inference vars with types into `relate_var_ty`. This allowed us to massively simplify the code in `relate_ty_ty` that calls `relate_var_ty`. Co-authored-by: David Ross <daboross@daboross.net>
1 parent 69a3c55 commit c9dfb7f

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

chalk-solve/src/infer/unify.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,35 +158,15 @@ impl<'t, I: Interner> Unifier<'t, I> {
158158
(_, &TyKind::Alias(ref alias)) => self.relate_alias_ty(variance.invert(), alias, a),
159159
(&TyKind::Alias(ref alias), _) => self.relate_alias_ty(variance, alias, b),
160160

161-
// FIXME: needs to handle relating a var and ty; needs generalization
162-
// Relating an inference variable with a non-inference variable.
163-
(a_data @ &TyKind::InferenceVar(_, _), b_data @ _)
164-
| (a_data @ _, b_data @ &TyKind::InferenceVar(_, _)) => {
165-
// Correct variance when we flip a/b.
166-
let (new_variance, var, kind, ty_data) = match (a_data, b_data) {
167-
(&TyKind::InferenceVar(var, kind), ty_data @ _) => {
168-
(variance, var, kind, ty_data)
169-
}
170-
(ty_data @ _, &TyKind::InferenceVar(var, kind)) => {
171-
// var is grabbed from 'b', but given in as the left
172-
// parameter to relate_var_ty, so we need to flip variance.
173-
(variance.invert(), var, kind, ty_data)
174-
}
175-
_ => unreachable!(),
176-
};
177-
161+
(&TyKind::InferenceVar(var, kind), ty_data @ _) => {
178162
let ty = ty_data.clone().intern(interner);
179-
match (kind, ty.is_integer(interner), ty.is_float(interner)) {
180-
// General inference variables can unify with any type
181-
(TyVariableKind::General, _, _)
182-
// Integer inference variables can only unify with integer types
183-
| (TyVariableKind::Integer, true, _)
184-
// Float inference variables can only unify with float types
185-
| (TyVariableKind::Float, _, true) => {
186-
self.relate_var_ty(new_variance, var, &ty)
187-
},
188-
_ => Err(NoSolution),
189-
}
163+
self.relate_var_ty(variance, var, kind, &ty)
164+
}
165+
(ty_data @ _, &TyKind::InferenceVar(var, kind)) => {
166+
// We need to invert the variance if inference var is `b` because we pass it in
167+
// as `a` to relate_var_ty
168+
let ty = ty_data.clone().intern(interner);
169+
self.relate_var_ty(variance.invert(), var, kind, &ty)
190170
}
191171

192172
// This would correspond to unifying a `fn` type with a non-fn
@@ -756,8 +736,26 @@ impl<'t, I: Interner> Unifier<'t, I> {
756736
/// - `ty` does not reference anything in a lifetime that could not be named in `var`
757737
/// (the extended `OccursCheck` created to handle universes)
758738
#[instrument(level = "debug", skip(self))]
759-
fn relate_var_ty(&mut self, variance: Variance, var: InferenceVar, ty: &Ty<I>) -> Fallible<()> {
739+
fn relate_var_ty(
740+
&mut self,
741+
variance: Variance,
742+
var: InferenceVar,
743+
var_kind: TyVariableKind,
744+
ty: &Ty<I>,
745+
) -> Fallible<()> {
760746
let interner = self.interner;
747+
748+
match (var_kind, ty.is_integer(interner), ty.is_float(interner)) {
749+
// General inference variables can unify with any type
750+
(TyVariableKind::General, _, _)
751+
// Integer inference variables can only unify with integer types
752+
| (TyVariableKind::Integer, true, _)
753+
// Float inference variables can only unify with float types
754+
| (TyVariableKind::Float, _, true) => {
755+
},
756+
_ => return Err(NoSolution),
757+
}
758+
761759
let var = EnaVariable::from(var);
762760

763761
// Determine the universe index associated with this

0 commit comments

Comments
 (0)