Skip to content

Commit 768ee3e

Browse files
committed
Align InferenceVar to Chalk
1 parent d280538 commit 768ee3e

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

crates/hir_ty/src/infer.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -683,25 +683,6 @@ impl<'a> InferenceContext<'a> {
683683
}
684684
}
685685

686-
/// The kinds of placeholders we need during type inference. There's separate
687-
/// values for general types, and for integer and float variables. The latter
688-
/// two are used for inference of literal values (e.g. `100` could be one of
689-
/// several integer types).
690-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
691-
pub struct InferenceVar {
692-
index: u32,
693-
}
694-
695-
impl InferenceVar {
696-
fn to_inner(self) -> unify::TypeVarId {
697-
unify::TypeVarId(self.index)
698-
}
699-
700-
fn from_inner(unify::TypeVarId(index): unify::TypeVarId) -> Self {
701-
InferenceVar { index }
702-
}
703-
}
704-
705686
/// When inferring an expression, we propagate downward whatever type hint we
706687
/// are able in the form of an `Expectation`.
707688
#[derive(Clone, PartialEq, Eq, Debug)]

crates/hir_ty/src/infer/unify.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
5151
t.fold_binders(
5252
&mut |ty, binders| match ty.kind(&Interner) {
5353
&TyKind::InferenceVar(var, kind) => {
54-
let inner = var.to_inner();
54+
let inner = from_inference_var(var);
5555
if self.var_stack.contains(&inner) {
5656
// recursive type
5757
return self.ctx.table.type_variable_table.fallback_value(var, kind);
@@ -65,7 +65,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
6565
result
6666
} else {
6767
let root = self.ctx.table.var_unification_table.find(inner);
68-
let position = self.add(InferenceVar::from_inner(root), kind);
68+
let position = self.add(to_inference_var(root), kind);
6969
TyKind::BoundVar(BoundVar::new(binders, position)).intern(&Interner)
7070
}
7171
}
@@ -207,16 +207,16 @@ impl TypeVariableTable {
207207
}
208208

209209
pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) {
210-
self.inner[iv.to_inner().0 as usize].diverging = diverging;
210+
self.inner[from_inference_var(iv).0 as usize].diverging = diverging;
211211
}
212212

213213
fn is_diverging(&mut self, iv: InferenceVar) -> bool {
214-
self.inner[iv.to_inner().0 as usize].diverging
214+
self.inner[from_inference_var(iv).0 as usize].diverging
215215
}
216216

217217
fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty {
218218
match kind {
219-
_ if self.inner[iv.to_inner().0 as usize].diverging => TyKind::Never,
219+
_ if self.inner[from_inference_var(iv).0 as usize].diverging => TyKind::Never,
220220
TyVariableKind::General => TyKind::Error,
221221
TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)),
222222
TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)),
@@ -250,7 +250,7 @@ impl InferenceTable {
250250
self.type_variable_table.push(TypeVariableData { diverging });
251251
let key = self.var_unification_table.new_key(TypeVarValue::Unknown);
252252
assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1);
253-
TyKind::InferenceVar(InferenceVar::from_inner(key), kind).intern(&Interner)
253+
TyKind::InferenceVar(to_inference_var(key), kind).intern(&Interner)
254254
}
255255

256256
pub(crate) fn new_type_var(&mut self) -> Ty {
@@ -369,8 +369,12 @@ impl InferenceTable {
369369
== self.type_variable_table.is_diverging(*tv2) =>
370370
{
371371
// both type vars are unknown since we tried to resolve them
372-
if !self.var_unification_table.unioned(tv1.to_inner(), tv2.to_inner()) {
373-
self.var_unification_table.union(tv1.to_inner(), tv2.to_inner());
372+
if !self
373+
.var_unification_table
374+
.unioned(from_inference_var(*tv1), from_inference_var(*tv2))
375+
{
376+
self.var_unification_table
377+
.union(from_inference_var(*tv1), from_inference_var(*tv2));
374378
self.revision += 1;
375379
}
376380
true
@@ -407,7 +411,7 @@ impl InferenceTable {
407411
) => {
408412
// the type var is unknown since we tried to resolve it
409413
self.var_unification_table.union_value(
410-
tv.to_inner(),
414+
from_inference_var(*tv),
411415
TypeVarValue::Known(other.clone().intern(&Interner)),
412416
);
413417
self.revision += 1;
@@ -462,7 +466,7 @@ impl InferenceTable {
462466
}
463467
match ty.kind(&Interner) {
464468
TyKind::InferenceVar(tv, _) => {
465-
let inner = tv.to_inner();
469+
let inner = from_inference_var(*tv);
466470
match self.var_unification_table.inlined_probe_value(inner).known() {
467471
Some(known_ty) => {
468472
// The known_ty can't be a type var itself
@@ -485,7 +489,7 @@ impl InferenceTable {
485489
fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty {
486490
ty.fold(&mut |ty| match ty.kind(&Interner) {
487491
&TyKind::InferenceVar(tv, kind) => {
488-
let inner = tv.to_inner();
492+
let inner = from_inference_var(tv);
489493
if tv_stack.contains(&inner) {
490494
cov_mark::hit!(type_var_cycles_resolve_as_possible);
491495
// recursive type
@@ -512,7 +516,7 @@ impl InferenceTable {
512516
fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty {
513517
ty.fold(&mut |ty| match ty.kind(&Interner) {
514518
&TyKind::InferenceVar(tv, kind) => {
515-
let inner = tv.to_inner();
519+
let inner = from_inference_var(tv);
516520
if tv_stack.contains(&inner) {
517521
cov_mark::hit!(type_var_cycles_resolve_completely);
518522
// recursive type
@@ -555,6 +559,14 @@ impl UnifyKey for TypeVarId {
555559
}
556560
}
557561

562+
fn from_inference_var(var: InferenceVar) -> TypeVarId {
563+
TypeVarId(var.index())
564+
}
565+
566+
fn to_inference_var(TypeVarId(index): TypeVarId) -> InferenceVar {
567+
index.into()
568+
}
569+
558570
/// The value of a type variable: either we already know the type, or we don't
559571
/// know it yet.
560572
#[derive(Clone, PartialEq, Eq, Debug)]

crates/hir_ty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
4242
pub use autoderef::autoderef;
4343
pub use builder::TyBuilder;
4444
pub use chalk_ext::{ProjectionTyExt, TyExt};
45-
pub use infer::{could_unify, InferenceResult, InferenceVar};
45+
pub use infer::{could_unify, InferenceResult};
4646
pub use lower::{
4747
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
4848
TyDefId, TyLoweringContext, ValueTyDefId,

crates/hir_ty/src/types.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use smallvec::SmallVec;
1111

1212
use crate::{
1313
AssocTypeId, CanonicalVarKinds, ChalkTraitId, ClosureId, Const, FnDefId, FnSig, ForeignDefId,
14-
InferenceVar, Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind,
15-
VariableKinds,
14+
Interner, Lifetime, OpaqueTyId, PlaceholderIndex, TypeWalk, VariableKind, VariableKinds,
1615
};
1716

1817
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -524,3 +523,25 @@ pub enum Guidance {
524523
/// There's no useful information to feed back to type inference
525524
Unknown,
526525
}
526+
527+
/// The kinds of placeholders we need during type inference. There's separate
528+
/// values for general types, and for integer and float variables. The latter
529+
/// two are used for inference of literal values (e.g. `100` could be one of
530+
/// several integer types).
531+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
532+
pub struct InferenceVar {
533+
index: u32,
534+
}
535+
536+
impl From<u32> for InferenceVar {
537+
fn from(index: u32) -> InferenceVar {
538+
InferenceVar { index }
539+
}
540+
}
541+
542+
impl InferenceVar {
543+
/// Gets the underlying index value.
544+
pub fn index(self) -> u32 {
545+
self.index
546+
}
547+
}

0 commit comments

Comments
 (0)