Skip to content

Commit cda13d5

Browse files
bors[bot]Veykril
andauthored
Merge #7823
7823: Being Ty::InferenceVar closer to chalk equivalent r=flodiebold a=Veykril Co-authored-by: Lukas Wirth <[email protected]>
2 parents 1c7b2b8 + 4b7fc69 commit cda13d5

File tree

11 files changed

+163
-152
lines changed

11 files changed

+163
-152
lines changed

crates/hir/src/code_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use hir_ty::{
3333
traits::{FnTrait, Solution, SolutionVariables},
3434
BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
3535
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment,
36-
Ty, TyDefId, TyKind,
36+
Ty, TyDefId, TyVariableKind,
3737
};
3838
use rustc_hash::FxHashSet;
3939
use stdx::{format_to, impl_from};
@@ -1655,7 +1655,7 @@ impl Type {
16551655
self.ty.environment.clone(),
16561656
Obligation::Projection(predicate),
16571657
),
1658-
kinds: Arc::new([TyKind::General]),
1658+
kinds: Arc::new([TyVariableKind::General]),
16591659
};
16601660

16611661
match db.trait_solve(self.krate, goal)? {

crates/hir_ty/src/autoderef.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ fn deref_by_trait(
8989

9090
let in_env = InEnvironment { value: obligation, environment: ty.environment };
9191

92-
let canonical =
93-
Canonical::new(in_env, ty.value.kinds.iter().copied().chain(Some(super::TyKind::General)));
92+
let canonical = Canonical::new(
93+
in_env,
94+
ty.value.kinds.iter().copied().chain(Some(chalk_ir::TyVariableKind::General)),
95+
);
9496

9597
let solution = db.trait_solve(krate, canonical)?;
9698

crates/hir_ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl HirDisplay for Ty {
565565
}
566566
write!(f, "{{unknown}}")?;
567567
}
568-
Ty::Infer(..) => write!(f, "_")?,
568+
Ty::InferenceVar(..) => write!(f, "_")?,
569569
}
570570
Ok(())
571571
}

crates/hir_ty/src/infer.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ use stdx::impl_from;
3636
use syntax::SmolStr;
3737

3838
use super::{
39-
primitive::{FloatTy, IntTy},
4039
traits::{Guidance, Obligation, ProjectionPredicate, Solution},
4140
InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
4241
};
4342
use crate::{
44-
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, Scalar,
43+
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
4544
};
4645

4746
pub(crate) use unify::unify;
@@ -655,30 +654,17 @@ impl<'a> InferenceContext<'a> {
655654
/// two are used for inference of literal values (e.g. `100` could be one of
656655
/// several integer types).
657656
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
658-
pub enum InferTy {
659-
TypeVar(unify::TypeVarId),
660-
IntVar(unify::TypeVarId),
661-
FloatVar(unify::TypeVarId),
662-
MaybeNeverTypeVar(unify::TypeVarId),
657+
pub struct InferenceVar {
658+
index: u32,
663659
}
664660

665-
impl InferTy {
661+
impl InferenceVar {
666662
fn to_inner(self) -> unify::TypeVarId {
667-
match self {
668-
InferTy::TypeVar(ty)
669-
| InferTy::IntVar(ty)
670-
| InferTy::FloatVar(ty)
671-
| InferTy::MaybeNeverTypeVar(ty) => ty,
672-
}
663+
unify::TypeVarId(self.index)
673664
}
674665

675-
fn fallback_value(self) -> Ty {
676-
match self {
677-
InferTy::TypeVar(..) => Ty::Unknown,
678-
InferTy::IntVar(..) => Ty::Scalar(Scalar::Int(IntTy::I32)),
679-
InferTy::FloatVar(..) => Ty::Scalar(Scalar::Float(FloatTy::F64)),
680-
InferTy::MaybeNeverTypeVar(..) => Ty::Never,
681-
}
666+
fn from_inner(unify::TypeVarId(index): unify::TypeVarId) -> Self {
667+
InferenceVar { index }
682668
}
683669
}
684670

crates/hir_ty/src/infer/coerce.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
//!
55
//! See: https://doc.rust-lang.org/nomicon/coercions.html
66
7+
use chalk_ir::TyVariableKind;
78
use hir_def::{lang_item::LangItemTarget, type_ref::Mutability};
89
use test_utils::mark;
910

1011
use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
1112

12-
use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext};
13+
use super::{InEnvironment, InferenceContext};
1314

1415
impl<'a> InferenceContext<'a> {
1516
/// Unify two types, but may coerce the first one to the second one
@@ -53,9 +54,8 @@ impl<'a> InferenceContext<'a> {
5354
fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
5455
match (&from_ty, to_ty) {
5556
// Never type will make type variable to fallback to Never Type instead of Unknown.
56-
(Ty::Never, Ty::Infer(InferTy::TypeVar(tv))) => {
57-
let var = self.table.new_maybe_never_type_var();
58-
self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
57+
(Ty::Never, Ty::InferenceVar(tv, TyVariableKind::General)) => {
58+
self.table.type_variable_table.set_diverging(*tv, true);
5959
return true;
6060
}
6161
(Ty::Never, _) => return true,

crates/hir_ty/src/infer/expr.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::iter::{repeat, repeat_with};
44
use std::{mem, sync::Arc};
55

6+
use chalk_ir::TyVariableKind;
67
use hir_def::{
78
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
89
path::{GenericArg, GenericArgs},
@@ -18,8 +19,8 @@ use crate::{
1819
primitive::{self, UintTy},
1920
traits::{FnTrait, InEnvironment},
2021
utils::{generics, variant_data, Generics},
21-
Binders, CallableDefId, FnPointer, FnSig, InferTy, Mutability, Obligation, OpaqueTyId, Rawness,
22-
Scalar, Substs, TraitRef, Ty,
22+
Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar,
23+
Substs, TraitRef, Ty,
2324
};
2425

2526
use super::{
@@ -527,8 +528,8 @@ impl<'a> InferenceContext<'a> {
527528
Ty::Scalar(Scalar::Int(_))
528529
| Ty::Scalar(Scalar::Uint(_))
529530
| Ty::Scalar(Scalar::Float(_))
530-
| Ty::Infer(InferTy::IntVar(..))
531-
| Ty::Infer(InferTy::FloatVar(..)) => inner_ty,
531+
| Ty::InferenceVar(_, TyVariableKind::Integer)
532+
| Ty::InferenceVar(_, TyVariableKind::Float) => inner_ty,
532533
// Otherwise we resolve via the std::ops::Neg trait
533534
_ => self
534535
.resolve_associated_type(inner_ty, self.resolve_ops_neg_output()),
@@ -540,7 +541,7 @@ impl<'a> InferenceContext<'a> {
540541
Ty::Scalar(Scalar::Bool)
541542
| Ty::Scalar(Scalar::Int(_))
542543
| Ty::Scalar(Scalar::Uint(_))
543-
| Ty::Infer(InferTy::IntVar(..)) => inner_ty,
544+
| Ty::InferenceVar(_, TyVariableKind::Integer) => inner_ty,
544545
// Otherwise we resolve via the std::ops::Not trait
545546
_ => self
546547
.resolve_associated_type(inner_ty, self.resolve_ops_not_output()),
@@ -761,7 +762,7 @@ impl<'a> InferenceContext<'a> {
761762
// `!`).
762763
if self.diverges.is_always() {
763764
// we don't even make an attempt at coercion
764-
self.table.new_maybe_never_type_var()
765+
self.table.new_maybe_never_var()
765766
} else {
766767
self.coerce(&Ty::unit(), expected.coercion_target());
767768
Ty::unit()

0 commit comments

Comments
 (0)