diff options
author | Florian Diebold <[email protected]> | 2021-03-21 19:05:38 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-03-21 19:05:38 +0000 |
commit | f7be314579db29f64ef660aef1896da33d420ad6 (patch) | |
tree | 24208e3429e80d578aed09b03ea0275820a13973 | |
parent | 1d5c4a77fb33cab7bf8f9d2edc6dd26b09ef65f3 (diff) |
Align Canonical more with Chalk's version
In particular, use chalk_ir::CanonicalVarKinds.
-rw-r--r-- | crates/hir/src/lib.rs | 41 | ||||
-rw-r--r-- | crates/hir_ty/src/autoderef.rs | 33 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/unify.rs | 44 | ||||
-rw-r--r-- | crates/hir_ty/src/lib.rs | 12 | ||||
-rw-r--r-- | crates/hir_ty/src/method_resolution.rs | 49 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 27 |
6 files changed, 125 insertions, 81 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index a325b6691..b7ab03edf 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -56,9 +56,9 @@ use hir_ty::{ | |||
56 | primitive::UintTy, | 56 | primitive::UintTy, |
57 | to_assoc_type_id, | 57 | to_assoc_type_id, |
58 | traits::{FnTrait, Solution, SolutionVariables}, | 58 | traits::{FnTrait, Solution, SolutionVariables}, |
59 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, Cast, DebruijnIndex, | 59 | AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, |
60 | InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, Ty, | 60 | DebruijnIndex, InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar, |
61 | TyDefId, TyKind, TyVariableKind, WhereClause, | 61 | Substitution, Ty, TyDefId, TyKind, TyVariableKind, WhereClause, |
62 | }; | 62 | }; |
63 | use itertools::Itertools; | 63 | use itertools::Itertools; |
64 | use rustc_hash::FxHashSet; | 64 | use rustc_hash::FxHashSet; |
@@ -1723,7 +1723,10 @@ impl Type { | |||
1723 | None => return false, | 1723 | None => return false, |
1724 | }; | 1724 | }; |
1725 | 1725 | ||
1726 | let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | 1726 | let canonical_ty = Canonical { |
1727 | value: self.ty.value.clone(), | ||
1728 | binders: CanonicalVarKinds::empty(&Interner), | ||
1729 | }; | ||
1727 | method_resolution::implements_trait( | 1730 | method_resolution::implements_trait( |
1728 | &canonical_ty, | 1731 | &canonical_ty, |
1729 | db, | 1732 | db, |
@@ -1745,7 +1748,10 @@ impl Type { | |||
1745 | None => return false, | 1748 | None => return false, |
1746 | }; | 1749 | }; |
1747 | 1750 | ||
1748 | let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | 1751 | let canonical_ty = Canonical { |
1752 | value: self.ty.value.clone(), | ||
1753 | binders: CanonicalVarKinds::empty(&Interner), | ||
1754 | }; | ||
1749 | method_resolution::implements_trait_unique( | 1755 | method_resolution::implements_trait_unique( |
1750 | &canonical_ty, | 1756 | &canonical_ty, |
1751 | db, | 1757 | db, |
@@ -1769,7 +1775,7 @@ impl Type { | |||
1769 | self.ty.environment.clone(), | 1775 | self.ty.environment.clone(), |
1770 | trait_ref.cast(&Interner), | 1776 | trait_ref.cast(&Interner), |
1771 | ), | 1777 | ), |
1772 | kinds: Arc::new([]), | 1778 | binders: CanonicalVarKinds::empty(&Interner), |
1773 | }; | 1779 | }; |
1774 | 1780 | ||
1775 | db.trait_solve(self.krate, goal).is_some() | 1781 | db.trait_solve(self.krate, goal).is_some() |
@@ -1786,8 +1792,8 @@ impl Type { | |||
1786 | .push(self.ty.value.clone()) | 1792 | .push(self.ty.value.clone()) |
1787 | .fill(args.iter().map(|t| t.ty.value.clone())) | 1793 | .fill(args.iter().map(|t| t.ty.value.clone())) |
1788 | .build(); | 1794 | .build(); |
1789 | let goal = Canonical { | 1795 | let goal = Canonical::new( |
1790 | value: InEnvironment::new( | 1796 | InEnvironment::new( |
1791 | self.ty.environment.clone(), | 1797 | self.ty.environment.clone(), |
1792 | AliasEq { | 1798 | AliasEq { |
1793 | alias: AliasTy::Projection(ProjectionTy { | 1799 | alias: AliasTy::Projection(ProjectionTy { |
@@ -1799,8 +1805,8 @@ impl Type { | |||
1799 | } | 1805 | } |
1800 | .cast(&Interner), | 1806 | .cast(&Interner), |
1801 | ), | 1807 | ), |
1802 | kinds: Arc::new([TyVariableKind::General]), | 1808 | [TyVariableKind::General].iter().copied(), |
1803 | }; | 1809 | ); |
1804 | 1810 | ||
1805 | match db.trait_solve(self.krate, goal)? { | 1811 | match db.trait_solve(self.krate, goal)? { |
1806 | Solution::Unique(SolutionVariables(subst)) => { | 1812 | Solution::Unique(SolutionVariables(subst)) => { |
@@ -1911,7 +1917,10 @@ impl Type { | |||
1911 | pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { | 1917 | pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { |
1912 | // There should be no inference vars in types passed here | 1918 | // There should be no inference vars in types passed here |
1913 | // FIXME check that? | 1919 | // FIXME check that? |
1914 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | 1920 | let canonical = Canonical { |
1921 | value: self.ty.value.clone(), | ||
1922 | binders: CanonicalVarKinds::empty(&Interner), | ||
1923 | }; | ||
1915 | let environment = self.ty.environment.clone(); | 1924 | let environment = self.ty.environment.clone(); |
1916 | let ty = InEnvironment { value: canonical, environment }; | 1925 | let ty = InEnvironment { value: canonical, environment }; |
1917 | autoderef(db, Some(self.krate), ty) | 1926 | autoderef(db, Some(self.krate), ty) |
@@ -1962,7 +1971,10 @@ impl Type { | |||
1962 | // There should be no inference vars in types passed here | 1971 | // There should be no inference vars in types passed here |
1963 | // FIXME check that? | 1972 | // FIXME check that? |
1964 | // FIXME replace Unknown by bound vars here | 1973 | // FIXME replace Unknown by bound vars here |
1965 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | 1974 | let canonical = Canonical { |
1975 | value: self.ty.value.clone(), | ||
1976 | binders: CanonicalVarKinds::empty(&Interner), | ||
1977 | }; | ||
1966 | 1978 | ||
1967 | let env = self.ty.environment.clone(); | 1979 | let env = self.ty.environment.clone(); |
1968 | let krate = krate.id; | 1980 | let krate = krate.id; |
@@ -1993,7 +2005,10 @@ impl Type { | |||
1993 | // There should be no inference vars in types passed here | 2005 | // There should be no inference vars in types passed here |
1994 | // FIXME check that? | 2006 | // FIXME check that? |
1995 | // FIXME replace Unknown by bound vars here | 2007 | // FIXME replace Unknown by bound vars here |
1996 | let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; | 2008 | let canonical = Canonical { |
2009 | value: self.ty.value.clone(), | ||
2010 | binders: CanonicalVarKinds::empty(&Interner), | ||
2011 | }; | ||
1997 | 2012 | ||
1998 | let env = self.ty.environment.clone(); | 2013 | let env = self.ty.environment.clone(); |
1999 | let krate = krate.id; | 2014 | let krate = krate.id; |
diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 23ab042c1..d6f0553b1 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs | |||
@@ -16,8 +16,8 @@ use crate::{ | |||
16 | to_assoc_type_id, to_chalk_trait_id, | 16 | to_assoc_type_id, to_chalk_trait_id, |
17 | traits::{InEnvironment, Solution}, | 17 | traits::{InEnvironment, Solution}, |
18 | utils::generics, | 18 | utils::generics, |
19 | AliasEq, AliasTy, BoundVar, Canonical, DebruijnIndex, Interner, ProjectionTy, Substitution, | 19 | AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, Interner, |
20 | TraitRef, Ty, TyKind, | 20 | ProjectionTy, Substitution, TraitRef, Ty, TyKind, |
21 | }; | 21 | }; |
22 | 22 | ||
23 | const AUTODEREF_RECURSION_LIMIT: usize = 10; | 23 | const AUTODEREF_RECURSION_LIMIT: usize = 10; |
@@ -40,7 +40,7 @@ pub(crate) fn deref( | |||
40 | ty: InEnvironment<&Canonical<Ty>>, | 40 | ty: InEnvironment<&Canonical<Ty>>, |
41 | ) -> Option<Canonical<Ty>> { | 41 | ) -> Option<Canonical<Ty>> { |
42 | if let Some(derefed) = ty.value.value.builtin_deref() { | 42 | if let Some(derefed) = ty.value.value.builtin_deref() { |
43 | Some(Canonical { value: derefed, kinds: ty.value.kinds.clone() }) | 43 | Some(Canonical { value: derefed, binders: ty.value.binders.clone() }) |
44 | } else { | 44 | } else { |
45 | deref_by_trait(db, krate, ty) | 45 | deref_by_trait(db, krate, ty) |
46 | } | 46 | } |
@@ -73,7 +73,7 @@ fn deref_by_trait( | |||
73 | let trait_ref = | 73 | let trait_ref = |
74 | TraitRef { trait_id: to_chalk_trait_id(deref_trait), substitution: parameters.clone() }; | 74 | TraitRef { trait_id: to_chalk_trait_id(deref_trait), substitution: parameters.clone() }; |
75 | let implements_goal = Canonical { | 75 | let implements_goal = Canonical { |
76 | kinds: ty.value.kinds.clone(), | 76 | binders: ty.value.binders.clone(), |
77 | value: InEnvironment { | 77 | value: InEnvironment { |
78 | value: trait_ref.cast(&Interner), | 78 | value: trait_ref.cast(&Interner), |
79 | environment: ty.environment.clone(), | 79 | environment: ty.environment.clone(), |
@@ -89,18 +89,27 @@ fn deref_by_trait( | |||
89 | associated_ty_id: to_assoc_type_id(target), | 89 | associated_ty_id: to_assoc_type_id(target), |
90 | substitution: parameters, | 90 | substitution: parameters, |
91 | }), | 91 | }), |
92 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())) | 92 | ty: TyKind::BoundVar(BoundVar::new( |
93 | .intern(&Interner), | 93 | DebruijnIndex::INNERMOST, |
94 | ty.value.binders.len(&Interner), | ||
95 | )) | ||
96 | .intern(&Interner), | ||
94 | }; | 97 | }; |
95 | 98 | ||
96 | let obligation = projection.cast(&Interner); | 99 | let obligation = projection.cast(&Interner); |
97 | 100 | ||
98 | let in_env = InEnvironment { value: obligation, environment: ty.environment }; | 101 | let in_env = InEnvironment { value: obligation, environment: ty.environment }; |
99 | 102 | ||
100 | let canonical = Canonical::new( | 103 | let canonical = Canonical { |
101 | in_env, | 104 | value: in_env, |
102 | ty.value.kinds.iter().copied().chain(Some(chalk_ir::TyVariableKind::General)), | 105 | binders: CanonicalVarKinds::from_iter( |
103 | ); | 106 | &Interner, |
107 | ty.value.binders.iter(&Interner).cloned().chain(Some(chalk_ir::WithKind::new( | ||
108 | chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General), | ||
109 | chalk_ir::UniverseIndex::ROOT, | ||
110 | ))), | ||
111 | ), | ||
112 | }; | ||
104 | 113 | ||
105 | let solution = db.trait_solve(krate, canonical)?; | 114 | let solution = db.trait_solve(krate, canonical)?; |
106 | 115 | ||
@@ -121,7 +130,7 @@ fn deref_by_trait( | |||
121 | // assumptions will be broken. We would need to properly introduce | 130 | // assumptions will be broken. We would need to properly introduce |
122 | // new variables in that case | 131 | // new variables in that case |
123 | 132 | ||
124 | for i in 1..vars.0.kinds.len() { | 133 | for i in 1..vars.0.binders.len(&Interner) { |
125 | if vars.0.value[i - 1].interned(&Interner) | 134 | if vars.0.value[i - 1].interned(&Interner) |
126 | != &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) | 135 | != &TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i - 1)) |
127 | { | 136 | { |
@@ -131,7 +140,7 @@ fn deref_by_trait( | |||
131 | } | 140 | } |
132 | Some(Canonical { | 141 | Some(Canonical { |
133 | value: vars.0.value[vars.0.value.len() - 1].clone(), | 142 | value: vars.0.value[vars.0.value.len() - 1].clone(), |
134 | kinds: vars.0.kinds.clone(), | 143 | binders: vars.0.binders.clone(), |
135 | }) | 144 | }) |
136 | } | 145 | } |
137 | Solution::Ambig(_) => { | 146 | Solution::Ambig(_) => { |
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 35b0a2059..7595b46cf 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs | |||
@@ -2,13 +2,13 @@ | |||
2 | 2 | ||
3 | use std::borrow::Cow; | 3 | use std::borrow::Cow; |
4 | 4 | ||
5 | use chalk_ir::{FloatTy, IntTy, TyVariableKind}; | 5 | use chalk_ir::{FloatTy, IntTy, TyVariableKind, UniverseIndex, VariableKind}; |
6 | use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; | 6 | use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; |
7 | 7 | ||
8 | use super::{DomainGoal, InferenceContext}; | 8 | use super::{DomainGoal, InferenceContext}; |
9 | use crate::{ | 9 | use crate::{ |
10 | AliasEq, AliasTy, BoundVar, Canonical, DebruijnIndex, FnPointer, InEnvironment, InferenceVar, | 10 | AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, |
11 | Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, | 11 | InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | impl<'a> InferenceContext<'a> { | 14 | impl<'a> InferenceContext<'a> { |
@@ -76,8 +76,17 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { | |||
76 | } | 76 | } |
77 | 77 | ||
78 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { | 78 | fn into_canonicalized<T>(self, result: T) -> Canonicalized<T> { |
79 | let kinds = self.free_vars.iter().map(|&(_, k)| k).collect(); | 79 | let kinds = self |
80 | Canonicalized { value: Canonical { value: result, kinds }, free_vars: self.free_vars } | 80 | .free_vars |
81 | .iter() | ||
82 | .map(|&(_, k)| chalk_ir::WithKind::new(VariableKind::Ty(k), UniverseIndex::ROOT)); | ||
83 | Canonicalized { | ||
84 | value: Canonical { | ||
85 | value: result, | ||
86 | binders: CanonicalVarKinds::from_iter(&Interner, kinds), | ||
87 | }, | ||
88 | free_vars: self.free_vars, | ||
89 | } | ||
81 | } | 90 | } |
82 | 91 | ||
83 | pub(crate) fn canonicalize_ty(mut self, ty: Ty) -> Canonicalized<Ty> { | 92 | pub(crate) fn canonicalize_ty(mut self, ty: Ty) -> Canonicalized<Ty> { |
@@ -125,12 +134,19 @@ impl<T> Canonicalized<T> { | |||
125 | // the solution may contain new variables, which we need to convert to new inference vars | 134 | // the solution may contain new variables, which we need to convert to new inference vars |
126 | let new_vars = Substitution( | 135 | let new_vars = Substitution( |
127 | solution | 136 | solution |
128 | .kinds | 137 | .binders |
129 | .iter() | 138 | .iter(&Interner) |
130 | .map(|k| match k { | 139 | .map(|k| match k.kind { |
131 | TyVariableKind::General => ctx.table.new_type_var(), | 140 | VariableKind::Ty(TyVariableKind::General) => ctx.table.new_type_var(), |
132 | TyVariableKind::Integer => ctx.table.new_integer_var(), | 141 | VariableKind::Ty(TyVariableKind::Integer) => ctx.table.new_integer_var(), |
133 | TyVariableKind::Float => ctx.table.new_float_var(), | 142 | VariableKind::Ty(TyVariableKind::Float) => ctx.table.new_float_var(), |
143 | // HACK: Chalk can sometimes return new lifetime variables. We | ||
144 | // want to just skip them, but to not mess up the indices of | ||
145 | // other variables, we'll just create a new type variable in | ||
146 | // their place instead. This should not matter (we never see the | ||
147 | // actual *uses* of the lifetime variable). | ||
148 | VariableKind::Lifetime => ctx.table.new_type_var(), | ||
149 | _ => panic!("const variable in solution"), | ||
134 | }) | 150 | }) |
135 | .collect(), | 151 | .collect(), |
136 | ); | 152 | ); |
@@ -147,8 +163,8 @@ impl<T> Canonicalized<T> { | |||
147 | pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { | 163 | pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { |
148 | let mut table = InferenceTable::new(); | 164 | let mut table = InferenceTable::new(); |
149 | let vars = Substitution( | 165 | let vars = Substitution( |
150 | tys.kinds | 166 | tys.binders |
151 | .iter() | 167 | .iter(&Interner) |
152 | // we always use type vars here because we want everything to | 168 | // we always use type vars here because we want everything to |
153 | // fallback to Unknown in the end (kind of hacky, as below) | 169 | // fallback to Unknown in the end (kind of hacky, as below) |
154 | .map(|_| table.new_type_var()) | 170 | .map(|_| table.new_type_var()) |
@@ -170,7 +186,7 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substitution> { | |||
170 | } | 186 | } |
171 | } | 187 | } |
172 | Some( | 188 | Some( |
173 | Substitution::builder(tys.kinds.len()) | 189 | Substitution::builder(tys.binders.len(&Interner)) |
174 | .fill(vars.iter().map(|v| table.resolve_ty_completely(v.clone()))) | 190 | .fill(vars.iter().map(|v| table.resolve_ty_completely(v.clone()))) |
175 | .build(), | 191 | .build(), |
176 | ) | 192 | ) |
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 90b5b17e2..0f49dd39b 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -61,6 +61,8 @@ pub type ClosureId = chalk_ir::ClosureId<Interner>; | |||
61 | pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; | 61 | pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; |
62 | pub type PlaceholderIndex = chalk_ir::PlaceholderIndex; | 62 | pub type PlaceholderIndex = chalk_ir::PlaceholderIndex; |
63 | 63 | ||
64 | pub type CanonicalVarKinds = chalk_ir::CanonicalVarKinds<Interner>; | ||
65 | |||
64 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; | 66 | pub type ChalkTraitId = chalk_ir::TraitId<Interner>; |
65 | 67 | ||
66 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 68 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
@@ -662,12 +664,18 @@ impl QuantifiedWhereClauses { | |||
662 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 664 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
663 | pub struct Canonical<T> { | 665 | pub struct Canonical<T> { |
664 | pub value: T, | 666 | pub value: T, |
665 | pub kinds: Arc<[TyVariableKind]>, | 667 | pub binders: CanonicalVarKinds, |
666 | } | 668 | } |
667 | 669 | ||
668 | impl<T> Canonical<T> { | 670 | impl<T> Canonical<T> { |
669 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { | 671 | pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { |
670 | Self { value, kinds: kinds.into_iter().collect() } | 672 | let kinds = kinds.into_iter().map(|tk| { |
673 | chalk_ir::CanonicalVarKind::new( | ||
674 | chalk_ir::VariableKind::Ty(tk), | ||
675 | chalk_ir::UniverseIndex::ROOT, | ||
676 | ) | ||
677 | }); | ||
678 | Self { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } | ||
671 | } | 679 | } |
672 | } | 680 | } |
673 | 681 | ||
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index da6bc2a4a..0abe8f0a3 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs | |||
@@ -6,7 +6,7 @@ use std::{iter, sync::Arc}; | |||
6 | 6 | ||
7 | use arrayvec::ArrayVec; | 7 | use arrayvec::ArrayVec; |
8 | use base_db::CrateId; | 8 | use base_db::CrateId; |
9 | use chalk_ir::{cast::Cast, Mutability}; | 9 | use chalk_ir::{cast::Cast, Mutability, UniverseIndex}; |
10 | use hir_def::{ | 10 | use hir_def::{ |
11 | lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, | 11 | lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, |
12 | ImplId, Lookup, ModuleId, TraitId, | 12 | ImplId, Lookup, ModuleId, TraitId, |
@@ -21,8 +21,9 @@ use crate::{ | |||
21 | primitive::{self, FloatTy, IntTy, UintTy}, | 21 | primitive::{self, FloatTy, IntTy, UintTy}, |
22 | to_chalk_trait_id, | 22 | to_chalk_trait_id, |
23 | utils::all_super_traits, | 23 | utils::all_super_traits, |
24 | AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, ForeignDefId, InEnvironment, Interner, | 24 | AdtId, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSig, ForeignDefId, |
25 | Scalar, Substitution, TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk, | 25 | InEnvironment, Interner, Scalar, Substitution, TraitEnvironment, TraitRef, Ty, TyKind, |
26 | TypeWalk, | ||
26 | }; | 27 | }; |
27 | 28 | ||
28 | /// This is used as a key for indexing impls. | 29 | /// This is used as a key for indexing impls. |
@@ -443,7 +444,7 @@ fn iterate_method_candidates_with_autoref( | |||
443 | return true; | 444 | return true; |
444 | } | 445 | } |
445 | let refed = Canonical { | 446 | let refed = Canonical { |
446 | kinds: deref_chain[0].kinds.clone(), | 447 | binders: deref_chain[0].binders.clone(), |
447 | value: TyKind::Ref(Mutability::Not, deref_chain[0].value.clone()).intern(&Interner), | 448 | value: TyKind::Ref(Mutability::Not, deref_chain[0].value.clone()).intern(&Interner), |
448 | }; | 449 | }; |
449 | if iterate_method_candidates_by_receiver( | 450 | if iterate_method_candidates_by_receiver( |
@@ -459,7 +460,7 @@ fn iterate_method_candidates_with_autoref( | |||
459 | return true; | 460 | return true; |
460 | } | 461 | } |
461 | let ref_muted = Canonical { | 462 | let ref_muted = Canonical { |
462 | kinds: deref_chain[0].kinds.clone(), | 463 | binders: deref_chain[0].binders.clone(), |
463 | value: TyKind::Ref(Mutability::Mut, deref_chain[0].value.clone()).intern(&Interner), | 464 | value: TyKind::Ref(Mutability::Mut, deref_chain[0].value.clone()).intern(&Interner), |
464 | }; | 465 | }; |
465 | if iterate_method_candidates_by_receiver( | 466 | if iterate_method_candidates_by_receiver( |
@@ -677,19 +678,28 @@ pub(crate) fn inherent_impl_substs( | |||
677 | // we create a var for each type parameter of the impl; we need to keep in | 678 | // we create a var for each type parameter of the impl; we need to keep in |
678 | // mind here that `self_ty` might have vars of its own | 679 | // mind here that `self_ty` might have vars of its own |
679 | let vars = Substitution::build_for_def(db, impl_id) | 680 | let vars = Substitution::build_for_def(db, impl_id) |
680 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.kinds.len()) | 681 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner)) |
681 | .build(); | 682 | .build(); |
682 | let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); | 683 | let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars); |
683 | let mut kinds = self_ty.kinds.to_vec(); | 684 | let mut kinds = self_ty.binders.interned().to_vec(); |
684 | kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(vars.len())); | 685 | kinds.extend( |
685 | let tys = Canonical { kinds: kinds.into(), value: (self_ty_with_vars, self_ty.value.clone()) }; | 686 | iter::repeat(chalk_ir::WithKind::new( |
687 | chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General), | ||
688 | UniverseIndex::ROOT, | ||
689 | )) | ||
690 | .take(vars.len()), | ||
691 | ); | ||
692 | let tys = Canonical { | ||
693 | binders: CanonicalVarKinds::from_iter(&Interner, kinds), | ||
694 | value: (self_ty_with_vars, self_ty.value.clone()), | ||
695 | }; | ||
686 | let substs = super::infer::unify(&tys); | 696 | let substs = super::infer::unify(&tys); |
687 | // We only want the substs for the vars we added, not the ones from self_ty. | 697 | // We only want the substs for the vars we added, not the ones from self_ty. |
688 | // Also, if any of the vars we added are still in there, we replace them by | 698 | // Also, if any of the vars we added are still in there, we replace them by |
689 | // Unknown. I think this can only really happen if self_ty contained | 699 | // Unknown. I think this can only really happen if self_ty contained |
690 | // Unknown, and in that case we want the result to contain Unknown in those | 700 | // Unknown, and in that case we want the result to contain Unknown in those |
691 | // places again. | 701 | // places again. |
692 | substs.map(|s| fallback_bound_vars(s.suffix(vars.len()), self_ty.kinds.len())) | 702 | substs.map(|s| fallback_bound_vars(s.suffix(vars.len()), self_ty.binders.len(&Interner))) |
693 | } | 703 | } |
694 | 704 | ||
695 | /// This replaces any 'free' Bound vars in `s` (i.e. those with indices past | 705 | /// This replaces any 'free' Bound vars in `s` (i.e. those with indices past |
@@ -768,15 +778,24 @@ fn generic_implements_goal( | |||
768 | trait_: TraitId, | 778 | trait_: TraitId, |
769 | self_ty: Canonical<Ty>, | 779 | self_ty: Canonical<Ty>, |
770 | ) -> Canonical<InEnvironment<super::DomainGoal>> { | 780 | ) -> Canonical<InEnvironment<super::DomainGoal>> { |
771 | let mut kinds = self_ty.kinds.to_vec(); | 781 | let mut kinds = self_ty.binders.interned().to_vec(); |
772 | let substs = super::Substitution::build_for_def(db, trait_) | 782 | let substs = super::Substitution::build_for_def(db, trait_) |
773 | .push(self_ty.value) | 783 | .push(self_ty.value) |
774 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) | 784 | .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) |
775 | .build(); | 785 | .build(); |
776 | kinds.extend(iter::repeat(chalk_ir::TyVariableKind::General).take(substs.len() - 1)); | 786 | kinds.extend( |
787 | iter::repeat(chalk_ir::WithKind::new( | ||
788 | chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General), | ||
789 | UniverseIndex::ROOT, | ||
790 | )) | ||
791 | .take(substs.len() - 1), | ||
792 | ); | ||
777 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs }; | 793 | let trait_ref = TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs }; |
778 | let obligation = trait_ref.cast(&Interner); | 794 | let obligation = trait_ref.cast(&Interner); |
779 | Canonical { kinds: kinds.into(), value: InEnvironment::new(env, obligation) } | 795 | Canonical { |
796 | binders: CanonicalVarKinds::from_iter(&Interner, kinds), | ||
797 | value: InEnvironment::new(env, obligation), | ||
798 | } | ||
780 | } | 799 | } |
781 | 800 | ||
782 | fn autoderef_method_receiver( | 801 | fn autoderef_method_receiver( |
@@ -789,9 +808,9 @@ fn autoderef_method_receiver( | |||
789 | if let Some(TyKind::Array(parameters)) = | 808 | if let Some(TyKind::Array(parameters)) = |
790 | deref_chain.last().map(|ty| ty.value.interned(&Interner)) | 809 | deref_chain.last().map(|ty| ty.value.interned(&Interner)) |
791 | { | 810 | { |
792 | let kinds = deref_chain.last().unwrap().kinds.clone(); | 811 | let kinds = deref_chain.last().unwrap().binders.clone(); |
793 | let unsized_ty = TyKind::Slice(parameters.clone()).intern(&Interner); | 812 | let unsized_ty = TyKind::Slice(parameters.clone()).intern(&Interner); |
794 | deref_chain.push(Canonical { value: unsized_ty, kinds }) | 813 | deref_chain.push(Canonical { value: unsized_ty, binders: kinds }) |
795 | } | 814 | } |
796 | deref_chain | 815 | deref_chain |
797 | } | 816 | } |
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 7209dd14e..58d8f2894 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -439,35 +439,12 @@ where | |||
439 | type Chalk = chalk_ir::Canonical<T::Chalk>; | 439 | type Chalk = chalk_ir::Canonical<T::Chalk>; |
440 | 440 | ||
441 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | 441 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { |
442 | let kinds = self.kinds.iter().map(|&tk| { | ||
443 | chalk_ir::CanonicalVarKind::new( | ||
444 | chalk_ir::VariableKind::Ty(tk), | ||
445 | chalk_ir::UniverseIndex::ROOT, | ||
446 | ) | ||
447 | }); | ||
448 | let value = self.value.to_chalk(db); | 442 | let value = self.value.to_chalk(db); |
449 | chalk_ir::Canonical { | 443 | chalk_ir::Canonical { value, binders: self.binders } |
450 | value, | ||
451 | binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds), | ||
452 | } | ||
453 | } | 444 | } |
454 | 445 | ||
455 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { | 446 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { |
456 | let kinds = canonical | 447 | Canonical { binders: canonical.binders, value: from_chalk(db, canonical.value) } |
457 | .binders | ||
458 | .iter(&Interner) | ||
459 | .map(|k| match k.kind { | ||
460 | chalk_ir::VariableKind::Ty(tk) => tk, | ||
461 | // HACK: Chalk can sometimes return new lifetime variables. We | ||
462 | // want to just skip them, but to not mess up the indices of | ||
463 | // other variables, we'll just create a new type variable in | ||
464 | // their place instead. This should not matter (we never see the | ||
465 | // actual *uses* of the lifetime variable). | ||
466 | chalk_ir::VariableKind::Lifetime => chalk_ir::TyVariableKind::General, | ||
467 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), | ||
468 | }) | ||
469 | .collect(); | ||
470 | Canonical { kinds, value: from_chalk(db, canonical.value) } | ||
471 | } | 448 | } |
472 | } | 449 | } |
473 | 450 | ||