From 4b7fc693150f333cefe0e69d93271807f32741c4 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 1 Mar 2021 13:54:17 +0100 Subject: Assert index relationship between type_variable_table and var_unification_table --- crates/hir_ty/src/infer/unify.rs | 31 +++++++++++-------------------- crates/hir_ty/src/lib.rs | 4 ++-- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 1e9a94c04..b481aa1b3 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -225,36 +225,27 @@ impl InferenceTable { } } + fn new_var(&mut self, kind: TyVariableKind, diverging: bool) -> Ty { + self.type_variable_table.push(TypeVariableData { diverging }); + let key = self.var_unification_table.new_key(TypeVarValue::Unknown); + assert_eq!(key.0 as usize, self.type_variable_table.inner.len() - 1); + Ty::InferenceVar(InferenceVar::from_inner(key), kind) + } + pub(crate) fn new_type_var(&mut self) -> Ty { - self.type_variable_table.push(TypeVariableData { diverging: false }); - Ty::InferenceVar( - InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)), - TyVariableKind::General, - ) + self.new_var(TyVariableKind::General, false) } pub(crate) fn new_integer_var(&mut self) -> Ty { - self.type_variable_table.push(TypeVariableData { diverging: false }); - Ty::InferenceVar( - InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)), - TyVariableKind::Integer, - ) + self.new_var(TyVariableKind::Integer, false) } pub(crate) fn new_float_var(&mut self) -> Ty { - self.type_variable_table.push(TypeVariableData { diverging: false }); - Ty::InferenceVar( - InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)), - TyVariableKind::Float, - ) + self.new_var(TyVariableKind::Float, false) } pub(crate) fn new_maybe_never_var(&mut self) -> Ty { - self.type_variable_table.push(TypeVariableData { diverging: true }); - Ty::InferenceVar( - InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)), - TyVariableKind::General, - ) + self.new_var(TyVariableKind::General, true) } pub(crate) fn resolve_ty_completely(&mut self, ty: Ty) -> Ty { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 762437bf4..1131eaf92 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -527,11 +527,11 @@ impl TypeWalk for GenericPredicate { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Canonical { pub value: T, - pub kinds: Arc<[chalk_ir::TyVariableKind]>, + pub kinds: Arc<[TyVariableKind]>, } impl Canonical { - pub fn new(value: T, kinds: impl IntoIterator) -> Self { + pub fn new(value: T, kinds: impl IntoIterator) -> Self { Self { value, kinds: kinds.into_iter().collect() } } } -- cgit v1.2.3