Being Ty::InferenceVar closes to chalk equivalent

This commit is contained in:
Lukas Wirth 2021-03-01 12:35:11 +01:00
parent 4e5c496199
commit 11a1f13cd1
11 changed files with 172 additions and 152 deletions

View file

@ -33,7 +33,7 @@ use hir_ty::{
traits::{FnTrait, Solution, SolutionVariables}, traits::{FnTrait, Solution, SolutionVariables},
BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment,
Ty, TyDefId, TyKind, Ty, TyDefId, TyVariableKind,
}; };
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use stdx::{format_to, impl_from}; use stdx::{format_to, impl_from};
@ -1655,7 +1655,7 @@ impl Type {
self.ty.environment.clone(), self.ty.environment.clone(),
Obligation::Projection(predicate), Obligation::Projection(predicate),
), ),
kinds: Arc::new([TyKind::General]), kinds: Arc::new([TyVariableKind::General]),
}; };
match db.trait_solve(self.krate, goal)? { match db.trait_solve(self.krate, goal)? {

View file

@ -89,8 +89,10 @@ fn deref_by_trait(
let in_env = InEnvironment { value: obligation, environment: ty.environment }; let in_env = InEnvironment { value: obligation, environment: ty.environment };
let canonical = let canonical = Canonical::new(
Canonical::new(in_env, ty.value.kinds.iter().copied().chain(Some(super::TyKind::General))); in_env,
ty.value.kinds.iter().copied().chain(Some(chalk_ir::TyVariableKind::General)),
);
let solution = db.trait_solve(krate, canonical)?; let solution = db.trait_solve(krate, canonical)?;

View file

@ -565,7 +565,7 @@ impl HirDisplay for Ty {
} }
write!(f, "{{unknown}}")?; write!(f, "{{unknown}}")?;
} }
Ty::Infer(..) => write!(f, "_")?, Ty::InferenceVar(..) => write!(f, "_")?,
} }
Ok(()) Ok(())
} }

View file

@ -36,12 +36,11 @@ use stdx::impl_from;
use syntax::SmolStr; use syntax::SmolStr;
use super::{ use super::{
primitive::{FloatTy, IntTy},
traits::{Guidance, Obligation, ProjectionPredicate, Solution}, traits::{Guidance, Obligation, ProjectionPredicate, Solution},
InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
}; };
use crate::{ use crate::{
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, Scalar, db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
}; };
pub(crate) use unify::unify; pub(crate) use unify::unify;
@ -655,30 +654,17 @@ impl<'a> InferenceContext<'a> {
/// two are used for inference of literal values (e.g. `100` could be one of /// two are used for inference of literal values (e.g. `100` could be one of
/// several integer types). /// several integer types).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum InferTy { pub struct InferenceVar {
TypeVar(unify::TypeVarId), index: u32,
IntVar(unify::TypeVarId),
FloatVar(unify::TypeVarId),
MaybeNeverTypeVar(unify::TypeVarId),
} }
impl InferTy { impl InferenceVar {
fn to_inner(self) -> unify::TypeVarId { fn to_inner(self) -> unify::TypeVarId {
match self { unify::TypeVarId(self.index)
InferTy::TypeVar(ty)
| InferTy::IntVar(ty)
| InferTy::FloatVar(ty)
| InferTy::MaybeNeverTypeVar(ty) => ty,
}
} }
fn fallback_value(self) -> Ty { fn from_inner(unify::TypeVarId(index): unify::TypeVarId) -> Self {
match self { InferenceVar { index }
InferTy::TypeVar(..) => Ty::Unknown,
InferTy::IntVar(..) => Ty::Scalar(Scalar::Int(IntTy::I32)),
InferTy::FloatVar(..) => Ty::Scalar(Scalar::Float(FloatTy::F64)),
InferTy::MaybeNeverTypeVar(..) => Ty::Never,
}
} }
} }

View file

@ -4,12 +4,13 @@
//! //!
//! See: https://doc.rust-lang.org/nomicon/coercions.html //! See: https://doc.rust-lang.org/nomicon/coercions.html
use chalk_ir::TyVariableKind;
use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; use hir_def::{lang_item::LangItemTarget, type_ref::Mutability};
use test_utils::mark; use test_utils::mark;
use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty}; use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; use super::{InEnvironment, InferenceContext};
impl<'a> InferenceContext<'a> { impl<'a> InferenceContext<'a> {
/// Unify two types, but may coerce the first one to the second one /// Unify two types, but may coerce the first one to the second one
@ -53,9 +54,8 @@ impl<'a> InferenceContext<'a> {
fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
match (&from_ty, to_ty) { match (&from_ty, to_ty) {
// Never type will make type variable to fallback to Never Type instead of Unknown. // Never type will make type variable to fallback to Never Type instead of Unknown.
(Ty::Never, Ty::Infer(InferTy::TypeVar(tv))) => { (Ty::Never, Ty::InferenceVar(tv, TyVariableKind::General)) => {
let var = self.table.new_maybe_never_type_var(); self.table.type_variable_table.set_diverging(*tv, true);
self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
return true; return true;
} }
(Ty::Never, _) => return true, (Ty::Never, _) => return true,

View file

@ -3,6 +3,7 @@
use std::iter::{repeat, repeat_with}; use std::iter::{repeat, repeat_with};
use std::{mem, sync::Arc}; use std::{mem, sync::Arc};
use chalk_ir::TyVariableKind;
use hir_def::{ use hir_def::{
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
path::{GenericArg, GenericArgs}, path::{GenericArg, GenericArgs},
@ -18,8 +19,8 @@ use crate::{
primitive::{self, UintTy}, primitive::{self, UintTy},
traits::{FnTrait, InEnvironment}, traits::{FnTrait, InEnvironment},
utils::{generics, variant_data, Generics}, utils::{generics, variant_data, Generics},
Binders, CallableDefId, FnPointer, FnSig, InferTy, Mutability, Obligation, OpaqueTyId, Rawness, Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar,
Scalar, Substs, TraitRef, Ty, Substs, TraitRef, Ty,
}; };
use super::{ use super::{
@ -527,8 +528,8 @@ impl<'a> InferenceContext<'a> {
Ty::Scalar(Scalar::Int(_)) Ty::Scalar(Scalar::Int(_))
| Ty::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Uint(_))
| Ty::Scalar(Scalar::Float(_)) | Ty::Scalar(Scalar::Float(_))
| Ty::Infer(InferTy::IntVar(..)) | Ty::InferenceVar(_, TyVariableKind::Integer)
| Ty::Infer(InferTy::FloatVar(..)) => inner_ty, | Ty::InferenceVar(_, TyVariableKind::Float) => inner_ty,
// Otherwise we resolve via the std::ops::Neg trait // Otherwise we resolve via the std::ops::Neg trait
_ => self _ => self
.resolve_associated_type(inner_ty, self.resolve_ops_neg_output()), .resolve_associated_type(inner_ty, self.resolve_ops_neg_output()),
@ -540,7 +541,7 @@ impl<'a> InferenceContext<'a> {
Ty::Scalar(Scalar::Bool) Ty::Scalar(Scalar::Bool)
| Ty::Scalar(Scalar::Int(_)) | Ty::Scalar(Scalar::Int(_))
| Ty::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Uint(_))
| Ty::Infer(InferTy::IntVar(..)) => inner_ty, | Ty::InferenceVar(_, TyVariableKind::Integer) => inner_ty,
// Otherwise we resolve via the std::ops::Not trait // Otherwise we resolve via the std::ops::Not trait
_ => self _ => self
.resolve_associated_type(inner_ty, self.resolve_ops_not_output()), .resolve_associated_type(inner_ty, self.resolve_ops_not_output()),
@ -761,7 +762,7 @@ impl<'a> InferenceContext<'a> {
// `!`). // `!`).
if self.diverges.is_always() { if self.diverges.is_always() {
// we don't even make an attempt at coercion // we don't even make an attempt at coercion
self.table.new_maybe_never_type_var() self.table.new_maybe_never_var()
} else { } else {
self.coerce(&Ty::unit(), expected.coercion_target()); self.coerce(&Ty::unit(), expected.coercion_target());
Ty::unit() Ty::unit()

View file

@ -2,14 +2,15 @@
use std::borrow::Cow; use std::borrow::Cow;
use chalk_ir::{FloatTy, IntTy, TyVariableKind};
use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
use test_utils::mark; use test_utils::mark;
use super::{InferenceContext, Obligation}; use super::{InferenceContext, Obligation};
use crate::{ use crate::{
BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Scalar, Substs, BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferenceVar, Scalar,
Ty, TyKind, TypeWalk, Substs, Ty, TypeWalk,
}; };
impl<'a> InferenceContext<'a> { impl<'a> InferenceContext<'a> {
@ -26,7 +27,7 @@ where
'a: 'b, 'a: 'b,
{ {
ctx: &'b mut InferenceContext<'a>, ctx: &'b mut InferenceContext<'a>,
free_vars: Vec<InferTy>, free_vars: Vec<(InferenceVar, TyVariableKind)>,
/// A stack of type variables that is used to detect recursive types (which /// A stack of type variables that is used to detect recursive types (which
/// are an error, but we need to protect against them to avoid stack /// are an error, but we need to protect against them to avoid stack
/// overflows). /// overflows).
@ -36,17 +37,14 @@ where
#[derive(Debug)] #[derive(Debug)]
pub(super) struct Canonicalized<T> { pub(super) struct Canonicalized<T> {
pub(super) value: Canonical<T>, pub(super) value: Canonical<T>,
free_vars: Vec<InferTy>, free_vars: Vec<(InferenceVar, TyVariableKind)>,
} }
impl<'a, 'b> Canonicalizer<'a, 'b> impl<'a, 'b> Canonicalizer<'a, 'b> {
where fn add(&mut self, free_var: InferenceVar, kind: TyVariableKind) -> usize {
'a: 'b, self.free_vars.iter().position(|&(v, _)| v == free_var).unwrap_or_else(|| {
{
fn add(&mut self, free_var: InferTy) -> usize {
self.free_vars.iter().position(|&v| v == free_var).unwrap_or_else(|| {
let next_index = self.free_vars.len(); let next_index = self.free_vars.len();
self.free_vars.push(free_var); self.free_vars.push((free_var, kind));
next_index next_index
}) })
} }
@ -54,11 +52,11 @@ where
fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T { fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T {
t.fold_binders( t.fold_binders(
&mut |ty, binders| match ty { &mut |ty, binders| match ty {
Ty::Infer(tv) => { Ty::InferenceVar(var, kind) => {
let inner = tv.to_inner(); let inner = var.to_inner();
if self.var_stack.contains(&inner) { if self.var_stack.contains(&inner) {
// recursive type // recursive type
return tv.fallback_value(); return self.ctx.table.type_variable_table.fallback_value(var, kind);
} }
if let Some(known_ty) = if let Some(known_ty) =
self.ctx.table.var_unification_table.inlined_probe_value(inner).known() self.ctx.table.var_unification_table.inlined_probe_value(inner).known()
@ -69,13 +67,7 @@ where
result result
} else { } else {
let root = self.ctx.table.var_unification_table.find(inner); let root = self.ctx.table.var_unification_table.find(inner);
let free_var = match tv { let position = self.add(InferenceVar::from_inner(root), kind);
InferTy::TypeVar(_) => InferTy::TypeVar(root),
InferTy::IntVar(_) => InferTy::IntVar(root),
InferTy::FloatVar(_) => InferTy::FloatVar(root),
InferTy::MaybeNeverTypeVar(_) => InferTy::MaybeNeverTypeVar(root),
};
let position = self.add(free_var);
Ty::Bound(BoundVar::new(binders, position)) Ty::Bound(BoundVar::new(binders, position))
} }
} }
@ -86,19 +78,7 @@ where
} }
fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> {
let kinds = self let kinds = self.free_vars.iter().map(|&(_, k)| k).collect();
.free_vars
.iter()
.map(|v| match v {
// mapping MaybeNeverTypeVar to the same kind as general ones
// should be fine, because as opposed to int or float type vars,
// they don't restrict what kind of type can go into them, they
// just affect fallback.
InferTy::TypeVar(_) | InferTy::MaybeNeverTypeVar(_) => TyKind::General,
InferTy::IntVar(_) => TyKind::Integer,
InferTy::FloatVar(_) => TyKind::Float,
})
.collect();
Canonicalized { value: Canonical { value: result, kinds }, free_vars: self.free_vars } Canonicalized { value: Canonical { value: result, kinds }, free_vars: self.free_vars }
} }
@ -132,7 +112,8 @@ impl<T> Canonicalized<T> {
&mut |ty, binders| { &mut |ty, binders| {
if let &mut Ty::Bound(bound) = ty { if let &mut Ty::Bound(bound) = ty {
if bound.debruijn >= binders { if bound.debruijn >= binders {
*ty = Ty::Infer(self.free_vars[bound.index]); let (v, k) = self.free_vars[bound.index];
*ty = Ty::InferenceVar(v, k);
} }
} }
}, },
@ -152,18 +133,18 @@ impl<T> Canonicalized<T> {
.kinds .kinds
.iter() .iter()
.map(|k| match k { .map(|k| match k {
TyKind::General => ctx.table.new_type_var(), TyVariableKind::General => ctx.table.new_type_var(),
TyKind::Integer => ctx.table.new_integer_var(), TyVariableKind::Integer => ctx.table.new_integer_var(),
TyKind::Float => ctx.table.new_float_var(), TyVariableKind::Float => ctx.table.new_float_var(),
}) })
.collect(), .collect(),
); );
for (i, ty) in solution.value.into_iter().enumerate() { for (i, ty) in solution.value.into_iter().enumerate() {
let var = self.free_vars[i]; let (v, k) = self.free_vars[i];
// eagerly replace projections in the type; we may be getting types // eagerly replace projections in the type; we may be getting types
// e.g. from where clauses where this hasn't happened yet // e.g. from where clauses where this hasn't happened yet
let ty = ctx.normalize_associated_types_in(ty.clone().subst_bound_vars(&new_vars)); let ty = ctx.normalize_associated_types_in(ty.clone().subst_bound_vars(&new_vars));
ctx.table.unify(&Ty::Infer(var), &ty); ctx.table.unify(&Ty::InferenceVar(v, k), &ty);
} }
} }
} }
@ -197,32 +178,83 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> {
) )
} }
#[derive(Clone, Debug)]
pub(super) struct TypeVariableTable {
inner: Vec<TypeVariableData>,
}
impl TypeVariableTable {
fn push(&mut self, data: TypeVariableData) {
self.inner.push(data);
}
pub(super) fn set_diverging(&mut self, iv: InferenceVar, diverging: bool) {
self.inner[iv.to_inner().0 as usize].diverging = diverging;
}
fn is_diverging(&mut self, iv: InferenceVar) -> bool {
self.inner[iv.to_inner().0 as usize].diverging
}
fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty {
match kind {
_ if self.inner[iv.to_inner().0 as usize].diverging => Ty::Never,
TyVariableKind::General => Ty::Unknown,
TyVariableKind::Integer => Ty::Scalar(Scalar::Int(IntTy::I32)),
TyVariableKind::Float => Ty::Scalar(Scalar::Float(FloatTy::F64)),
}
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct TypeVariableData {
diverging: bool,
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct InferenceTable { pub(crate) struct InferenceTable {
pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>, pub(super) var_unification_table: InPlaceUnificationTable<TypeVarId>,
pub(super) type_variable_table: TypeVariableTable,
} }
impl InferenceTable { impl InferenceTable {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
InferenceTable { var_unification_table: InPlaceUnificationTable::new() } InferenceTable {
var_unification_table: InPlaceUnificationTable::new(),
type_variable_table: TypeVariableTable { inner: Vec::new() },
}
} }
pub(crate) fn new_type_var(&mut self) -> Ty { pub(crate) fn new_type_var(&mut self) -> Ty {
Ty::Infer(InferTy::TypeVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) self.type_variable_table.push(TypeVariableData { diverging: false });
Ty::InferenceVar(
InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)),
TyVariableKind::General,
)
} }
pub(crate) fn new_integer_var(&mut self) -> Ty { pub(crate) fn new_integer_var(&mut self) -> Ty {
Ty::Infer(InferTy::IntVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) self.type_variable_table.push(TypeVariableData { diverging: false });
Ty::InferenceVar(
InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)),
TyVariableKind::Integer,
)
} }
pub(crate) fn new_float_var(&mut self) -> Ty { pub(crate) fn new_float_var(&mut self) -> Ty {
Ty::Infer(InferTy::FloatVar(self.var_unification_table.new_key(TypeVarValue::Unknown))) self.type_variable_table.push(TypeVariableData { diverging: false });
Ty::InferenceVar(
InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)),
TyVariableKind::Float,
)
} }
pub(crate) fn new_maybe_never_type_var(&mut self) -> Ty { pub(crate) fn new_maybe_never_var(&mut self) -> Ty {
Ty::Infer(InferTy::MaybeNeverTypeVar( self.type_variable_table.push(TypeVariableData { diverging: true });
self.var_unification_table.new_key(TypeVarValue::Unknown), Ty::InferenceVar(
)) InferenceVar::from_inner(self.var_unification_table.new_key(TypeVarValue::Unknown)),
TyVariableKind::General,
)
} }
pub(crate) fn resolve_ty_completely(&mut self, ty: Ty) -> Ty { pub(crate) fn resolve_ty_completely(&mut self, ty: Ty) -> Ty {
@ -283,33 +315,46 @@ impl InferenceTable {
true true
} }
(Ty::Infer(InferTy::TypeVar(tv1)), Ty::Infer(InferTy::TypeVar(tv2))) (
| (Ty::Infer(InferTy::IntVar(tv1)), Ty::Infer(InferTy::IntVar(tv2))) Ty::InferenceVar(tv1, TyVariableKind::General),
| (Ty::Infer(InferTy::FloatVar(tv1)), Ty::Infer(InferTy::FloatVar(tv2))) Ty::InferenceVar(tv2, TyVariableKind::General),
)
| ( | (
Ty::Infer(InferTy::MaybeNeverTypeVar(tv1)), Ty::InferenceVar(tv1, TyVariableKind::Integer),
Ty::Infer(InferTy::MaybeNeverTypeVar(tv2)), Ty::InferenceVar(tv2, TyVariableKind::Integer),
) => { )
| (
Ty::InferenceVar(tv1, TyVariableKind::Float),
Ty::InferenceVar(tv2, TyVariableKind::Float),
) if self.type_variable_table.is_diverging(*tv1)
== self.type_variable_table.is_diverging(*tv2) =>
{
// both type vars are unknown since we tried to resolve them // both type vars are unknown since we tried to resolve them
self.var_unification_table.union(*tv1, *tv2); self.var_unification_table.union(tv1.to_inner(), tv2.to_inner());
true true
} }
// The order of MaybeNeverTypeVar matters here. // The order of MaybeNeverTypeVar matters here.
// Unifying MaybeNeverTypeVar and TypeVar will let the latter become MaybeNeverTypeVar. // Unifying MaybeNeverTypeVar and TypeVar will let the latter become MaybeNeverTypeVar.
// Unifying MaybeNeverTypeVar and other concrete type will let the former become it. // Unifying MaybeNeverTypeVar and other concrete type will let the former become it.
(Ty::Infer(InferTy::TypeVar(tv)), other) (Ty::InferenceVar(tv, TyVariableKind::General), other)
| (other, Ty::Infer(InferTy::TypeVar(tv))) | (other, Ty::InferenceVar(tv, TyVariableKind::General))
| (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other) | (Ty::InferenceVar(tv, TyVariableKind::Integer), other @ Ty::Scalar(Scalar::Int(_)))
| (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv))) | (other @ Ty::Scalar(Scalar::Int(_)), Ty::InferenceVar(tv, TyVariableKind::Integer))
| (Ty::Infer(InferTy::IntVar(tv)), other @ Ty::Scalar(Scalar::Int(_))) | (
| (other @ Ty::Scalar(Scalar::Int(_)), Ty::Infer(InferTy::IntVar(tv))) Ty::InferenceVar(tv, TyVariableKind::Integer),
| (Ty::Infer(InferTy::IntVar(tv)), other @ Ty::Scalar(Scalar::Uint(_))) other @ Ty::Scalar(Scalar::Uint(_)),
| (other @ Ty::Scalar(Scalar::Uint(_)), Ty::Infer(InferTy::IntVar(tv))) )
| (Ty::Infer(InferTy::FloatVar(tv)), other @ Ty::Scalar(Scalar::Float(_))) | (
| (other @ Ty::Scalar(Scalar::Float(_)), Ty::Infer(InferTy::FloatVar(tv))) => { other @ Ty::Scalar(Scalar::Uint(_)),
Ty::InferenceVar(tv, TyVariableKind::Integer),
)
| (Ty::InferenceVar(tv, TyVariableKind::Float), other @ Ty::Scalar(Scalar::Float(_)))
| (other @ Ty::Scalar(Scalar::Float(_)), Ty::InferenceVar(tv, TyVariableKind::Float)) =>
{
// the type var is unknown since we tried to resolve it // the type var is unknown since we tried to resolve it
self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); self.var_unification_table
.union_value(tv.to_inner(), TypeVarValue::Known(other.clone()));
true true
} }
@ -354,7 +399,7 @@ impl InferenceTable {
mark::hit!(type_var_resolves_to_int_var); mark::hit!(type_var_resolves_to_int_var);
} }
match &*ty { match &*ty {
Ty::Infer(tv) => { Ty::InferenceVar(tv, _) => {
let inner = tv.to_inner(); let inner = tv.to_inner();
match self.var_unification_table.inlined_probe_value(inner).known() { match self.var_unification_table.inlined_probe_value(inner).known() {
Some(known_ty) => { Some(known_ty) => {
@ -377,12 +422,12 @@ impl InferenceTable {
/// known type. /// known type.
fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { fn resolve_ty_as_possible_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty {
ty.fold(&mut |ty| match ty { ty.fold(&mut |ty| match ty {
Ty::Infer(tv) => { Ty::InferenceVar(tv, kind) => {
let inner = tv.to_inner(); let inner = tv.to_inner();
if tv_stack.contains(&inner) { if tv_stack.contains(&inner) {
mark::hit!(type_var_cycles_resolve_as_possible); mark::hit!(type_var_cycles_resolve_as_possible);
// recursive type // recursive type
return tv.fallback_value(); return self.type_variable_table.fallback_value(tv, kind);
} }
if let Some(known_ty) = if let Some(known_ty) =
self.var_unification_table.inlined_probe_value(inner).known() self.var_unification_table.inlined_probe_value(inner).known()
@ -404,12 +449,12 @@ impl InferenceTable {
/// replaced by Ty::Unknown. /// replaced by Ty::Unknown.
fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty { fn resolve_ty_completely_inner(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty {
ty.fold(&mut |ty| match ty { ty.fold(&mut |ty| match ty {
Ty::Infer(tv) => { Ty::InferenceVar(tv, kind) => {
let inner = tv.to_inner(); let inner = tv.to_inner();
if tv_stack.contains(&inner) { if tv_stack.contains(&inner) {
mark::hit!(type_var_cycles_resolve_completely); mark::hit!(type_var_cycles_resolve_completely);
// recursive type // recursive type
return tv.fallback_value(); return self.type_variable_table.fallback_value(tv, kind);
} }
if let Some(known_ty) = if let Some(known_ty) =
self.var_unification_table.inlined_probe_value(inner).known() self.var_unification_table.inlined_probe_value(inner).known()
@ -420,7 +465,7 @@ impl InferenceTable {
tv_stack.pop(); tv_stack.pop();
result result
} else { } else {
tv.fallback_value() self.type_variable_table.fallback_value(tv, kind)
} }
} }
_ => ty, _ => ty,
@ -430,7 +475,7 @@ impl InferenceTable {
/// The ID of a type variable. /// The ID of a type variable.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct TypeVarId(pub(super) u32); pub(super) struct TypeVarId(pub(super) u32);
impl UnifyKey for TypeVarId { impl UnifyKey for TypeVarId {
type Value = TypeVarValue; type Value = TypeVarValue;
@ -451,7 +496,7 @@ impl UnifyKey for TypeVarId {
/// The value of a type variable: either we already know the type, or we don't /// The value of a type variable: either we already know the type, or we don't
/// know it yet. /// know it yet.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub enum TypeVarValue { pub(super) enum TypeVarValue {
Known(Ty), Known(Ty),
Unknown, Unknown,
} }

View file

@ -42,14 +42,14 @@ use crate::{
}; };
pub use autoderef::autoderef; pub use autoderef::autoderef;
pub use infer::{InferTy, InferenceResult}; pub use infer::{InferenceResult, InferenceVar};
pub use lower::{ pub use lower::{
associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
TyDefId, TyLoweringContext, ValueTyDefId, TyDefId, TyLoweringContext, ValueTyDefId,
}; };
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
pub use chalk_ir::{BoundVar, DebruijnIndex, Scalar}; pub use chalk_ir::{BoundVar, DebruijnIndex, Scalar, TyVariableKind};
#[derive(Clone, PartialEq, Eq, Debug, Hash)] #[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Lifetime { pub enum Lifetime {
@ -218,7 +218,7 @@ pub enum Ty {
Bound(BoundVar), Bound(BoundVar),
/// A type variable used during type checking. /// A type variable used during type checking.
Infer(InferTy), InferenceVar(InferenceVar, TyVariableKind),
/// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust). /// A trait object (`dyn Trait` or bare `Trait` in pre-2018 Rust).
/// ///
@ -527,22 +527,15 @@ impl TypeWalk for GenericPredicate {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Canonical<T> { pub struct Canonical<T> {
pub value: T, pub value: T,
pub kinds: Arc<[TyKind]>, pub kinds: Arc<[chalk_ir::TyVariableKind]>,
} }
impl<T> Canonical<T> { impl<T> Canonical<T> {
pub fn new(value: T, kinds: impl IntoIterator<Item = TyKind>) -> Self { pub fn new(value: T, kinds: impl IntoIterator<Item = chalk_ir::TyVariableKind>) -> Self {
Self { value, kinds: kinds.into_iter().collect() } Self { value, kinds: kinds.into_iter().collect() }
} }
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum TyKind {
General,
Integer,
Float,
}
/// A function signature as seen by type inference: Several parameter types and /// A function signature as seen by type inference: Several parameter types and
/// one return type. /// one return type.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]

View file

@ -19,7 +19,7 @@ use crate::{
primitive::{self, FloatTy, IntTy, UintTy}, primitive::{self, FloatTy, IntTy, UintTy},
utils::all_super_traits, utils::all_super_traits,
Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment,
TraitRef, Ty, TyKind, TypeWalk, TraitRef, Ty, TypeWalk,
}; };
/// This is used as a key for indexing impls. /// This is used as a key for indexing impls.
@ -667,7 +667,7 @@ pub(crate) fn inherent_impl_substs(
.build(); .build();
let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars);
let mut kinds = self_ty.kinds.to_vec(); let mut kinds = self_ty.kinds.to_vec();
kinds.extend(iter::repeat(TyKind::General).take(vars.len())); kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(vars.len()));
let tys = Canonical { kinds: kinds.into(), value: (self_ty_with_vars, self_ty.value.clone()) }; let tys = Canonical { kinds: kinds.into(), value: (self_ty_with_vars, self_ty.value.clone()) };
let substs = super::infer::unify(&tys); let substs = super::infer::unify(&tys);
// We only want the substs for the vars we added, not the ones from self_ty. // We only want the substs for the vars we added, not the ones from self_ty.
@ -759,7 +759,7 @@ fn generic_implements_goal(
.push(self_ty.value) .push(self_ty.value)
.fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len())
.build(); .build();
kinds.extend(iter::repeat(TyKind::General).take(substs.len() - 1)); kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(substs.len() - 1));
let trait_ref = TraitRef { trait_, substs }; let trait_ref = TraitRef { trait_, substs };
let obligation = super::Obligation::Trait(trait_ref); let obligation = super::Obligation::Trait(trait_ref);
Canonical { kinds: kinds.into(), value: InEnvironment::new(env, obligation) } Canonical { kinds: kinds.into(), value: InEnvironment::new(env, obligation) }

View file

@ -1,7 +1,8 @@
//! Helper functions for binary operator type inference. //! Helper functions for binary operator type inference.
use chalk_ir::TyVariableKind;
use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
use crate::{InferTy, Scalar, Ty}; use crate::{Scalar, Ty};
pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
match op { match op {
@ -11,14 +12,16 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
Ty::Scalar(Scalar::Int(_)) Ty::Scalar(Scalar::Int(_))
| Ty::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Uint(_))
| Ty::Scalar(Scalar::Float(_)) => lhs_ty, | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::InferenceVar(_, TyVariableKind::Integer)
| Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
BinaryOp::ArithOp(_) => match rhs_ty { BinaryOp::ArithOp(_) => match rhs_ty {
Ty::Scalar(Scalar::Int(_)) Ty::Scalar(Scalar::Int(_))
| Ty::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Uint(_))
| Ty::Scalar(Scalar::Float(_)) => rhs_ty, | Ty::Scalar(Scalar::Float(_)) => rhs_ty,
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, Ty::InferenceVar(_, TyVariableKind::Integer)
| Ty::InferenceVar(_, TyVariableKind::Float) => rhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
} }
@ -30,7 +33,8 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
BinaryOp::Assignment { op: None } => lhs_ty, BinaryOp::Assignment { op: None } => lhs_ty,
BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
Ty::Scalar(_) | Ty::Str => lhs_ty, Ty::Scalar(_) | Ty::Str => lhs_ty,
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::InferenceVar(_, TyVariableKind::Integer)
| Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown, BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => Ty::Unknown,
@ -40,7 +44,8 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
Ty::Scalar(Scalar::Int(_)) Ty::Scalar(Scalar::Int(_))
| Ty::Scalar(Scalar::Uint(_)) | Ty::Scalar(Scalar::Uint(_))
| Ty::Scalar(Scalar::Float(_)) => lhs_ty, | Ty::Scalar(Scalar::Float(_)) => lhs_ty,
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, Ty::InferenceVar(_, TyVariableKind::Integer)
| Ty::InferenceVar(_, TyVariableKind::Float) => lhs_ty,
_ => Ty::Unknown, _ => Ty::Unknown,
}, },
} }

View file

@ -17,7 +17,7 @@ use crate::{
primitive::UintTy, primitive::UintTy,
traits::{Canonical, Obligation}, traits::{Canonical, Obligation},
CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId,
ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty,
}; };
use super::interner::*; use super::interner::*;
@ -107,7 +107,7 @@ impl ToChalk for Ty {
.to_ty::<Interner>(&Interner) .to_ty::<Interner>(&Interner)
} }
Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), Ty::InferenceVar(..) => panic!("uncanonicalized infer ty"),
Ty::Dyn(predicates) => { Ty::Dyn(predicates) => {
let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter(
&Interner, &Interner,
@ -532,20 +532,12 @@ where
type Chalk = chalk_ir::Canonical<T::Chalk>; type Chalk = chalk_ir::Canonical<T::Chalk>;
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> {
let kinds = self let kinds = self.kinds.iter().map(|&tk| {
.kinds chalk_ir::CanonicalVarKind::new(
.iter() chalk_ir::VariableKind::Ty(tk),
.map(|k| match k { chalk_ir::UniverseIndex::ROOT,
TyKind::General => chalk_ir::TyVariableKind::General, )
TyKind::Integer => chalk_ir::TyVariableKind::Integer, });
TyKind::Float => chalk_ir::TyVariableKind::Float,
})
.map(|tk| {
chalk_ir::CanonicalVarKind::new(
chalk_ir::VariableKind::Ty(tk),
chalk_ir::UniverseIndex::ROOT,
)
});
let value = self.value.to_chalk(db); let value = self.value.to_chalk(db);
chalk_ir::Canonical { chalk_ir::Canonical {
value, value,
@ -558,17 +550,13 @@ where
.binders .binders
.iter(&Interner) .iter(&Interner)
.map(|k| match k.kind { .map(|k| match k.kind {
chalk_ir::VariableKind::Ty(tk) => match tk { chalk_ir::VariableKind::Ty(tk) => tk,
chalk_ir::TyVariableKind::General => TyKind::General,
chalk_ir::TyVariableKind::Integer => TyKind::Integer,
chalk_ir::TyVariableKind::Float => TyKind::Float,
},
// HACK: Chalk can sometimes return new lifetime variables. We // HACK: Chalk can sometimes return new lifetime variables. We
// want to just skip them, but to not mess up the indices of // want to just skip them, but to not mess up the indices of
// other variables, we'll just create a new type variable in // other variables, we'll just create a new type variable in
// their place instead. This should not matter (we never see the // their place instead. This should not matter (we never see the
// actual *uses* of the lifetime variable). // actual *uses* of the lifetime variable).
chalk_ir::VariableKind::Lifetime => TyKind::General, chalk_ir::VariableKind::Lifetime => chalk_ir::TyVariableKind::General,
chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"),
}) })
.collect(); .collect();