aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r--crates/hir_ty/src/chalk_ext.rs35
-rw-r--r--crates/hir_ty/src/infer/expr.rs5
-rw-r--r--crates/hir_ty/src/infer/pat.rs6
-rw-r--r--crates/hir_ty/src/infer/unify.rs3
-rw-r--r--crates/hir_ty/src/lib.rs60
-rw-r--r--crates/hir_ty/src/walk.rs26
6 files changed, 61 insertions, 74 deletions
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs
index 8e8a1aa48..6a353423a 100644
--- a/crates/hir_ty/src/chalk_ext.rs
+++ b/crates/hir_ty/src/chalk_ext.rs
@@ -8,7 +8,7 @@ use hir_def::{
8use crate::{ 8use crate::{
9 db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, 9 db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
10 from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId, 10 from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId,
11 CallableSig, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, 11 CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause,
12 Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause, 12 Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause,
13}; 13};
14 14
@@ -34,6 +34,9 @@ pub trait TyExt {
34 34
35 fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>; 35 fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>;
36 fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>; 36 fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>;
37
38 /// FIXME: Get rid of this, it's not a good abstraction
39 fn equals_ctor(&self, other: &Ty) -> bool;
37} 40}
38 41
39impl TyExt for Ty { 42impl TyExt for Ty {
@@ -238,6 +241,36 @@ impl TyExt for Ty {
238 _ => None, 241 _ => None,
239 } 242 }
240 } 243 }
244
245 fn equals_ctor(&self, other: &Ty) -> bool {
246 match (self.kind(&Interner), other.kind(&Interner)) {
247 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
248 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => {
249 true
250 }
251 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
252 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
253 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
254 ty_id == ty_id2
255 }
256 (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2,
257 (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2,
258 (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..))
259 | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => {
260 mutability == mutability2
261 }
262 (
263 TyKind::Function(FnPointer { num_binders, sig, .. }),
264 TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
265 ) => num_binders == num_binders2 && sig == sig2,
266 (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
267 cardinality == cardinality2
268 }
269 (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
270 (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
271 _ => false,
272 }
273 }
241} 274}
242 275
243pub trait ProjectionTyExt { 276pub trait ProjectionTyExt {
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 9ab0fa212..9841988c5 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -412,7 +412,10 @@ impl<'a> InferenceContext<'a> {
412 412
413 self.unify(&ty, &expected.ty); 413 self.unify(&ty, &expected.ty);
414 414
415 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 415 let substs = ty
416 .as_adt()
417 .map(|(_, s)| s.clone())
418 .unwrap_or_else(|| Substitution::empty(&Interner));
416 let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); 419 let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default();
417 let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); 420 let variant_data = def_id.map(|it| it.variant_data(self.db.upcast()));
418 for field in fields.iter() { 421 for field in fields.iter() {
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index e4813c87c..f88d5c5d3 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -33,7 +33,8 @@ impl<'a> InferenceContext<'a> {
33 } 33 }
34 self.unify(&ty, expected); 34 self.unify(&ty, expected);
35 35
36 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 36 let substs =
37 ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
37 38
38 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); 39 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
39 let (pre, post) = match ellipsis { 40 let (pre, post) = match ellipsis {
@@ -74,7 +75,8 @@ impl<'a> InferenceContext<'a> {
74 75
75 self.unify(&ty, expected); 76 self.unify(&ty, expected);
76 77
77 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 78 let substs =
79 ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
78 80
79 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); 81 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
80 for subpat in subpats { 82 for subpat in subpats {
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index d717e3375..2ea9dd920 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -8,7 +8,8 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
8use super::{DomainGoal, InferenceContext}; 8use super::{DomainGoal, InferenceContext};
9use crate::{ 9use crate::{
10 AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, 10 AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst,
11 InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, 11 InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, TypeWalk,
12 WhereClause,
12}; 13};
13 14
14impl<'a> InferenceContext<'a> { 15impl<'a> InferenceContext<'a> {
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index ae3987752..2e851d3e0 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -168,65 +168,7 @@ impl CallableSig {
168 } 168 }
169} 169}
170 170
171impl Ty { 171impl Ty {}
172 pub fn equals_ctor(&self, other: &Ty) -> bool {
173 match (self.kind(&Interner), other.kind(&Interner)) {
174 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
175 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => {
176 true
177 }
178 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
179 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
180 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
181 ty_id == ty_id2
182 }
183 (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2,
184 (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2,
185 (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..))
186 | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => {
187 mutability == mutability2
188 }
189 (
190 TyKind::Function(FnPointer { num_binders, sig, .. }),
191 TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
192 ) => num_binders == num_binders2 && sig == sig2,
193 (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
194 cardinality == cardinality2
195 }
196 (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
197 (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
198 _ => false,
199 }
200 }
201
202 /// Returns the type parameters of this type if it has some (i.e. is an ADT
203 /// or function); so if `self` is `Option<u32>`, this returns the `u32`.
204 pub fn substs(&self) -> Option<&Substitution> {
205 match self.kind(&Interner) {
206 TyKind::Adt(_, substs)
207 | TyKind::FnDef(_, substs)
208 | TyKind::Tuple(_, substs)
209 | TyKind::OpaqueType(_, substs)
210 | TyKind::AssociatedType(_, substs)
211 | TyKind::Closure(.., substs) => Some(substs),
212 TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&substs.0),
213 _ => None,
214 }
215 }
216
217 fn substs_mut(&mut self) -> Option<&mut Substitution> {
218 match self.interned_mut() {
219 TyKind::Adt(_, substs)
220 | TyKind::FnDef(_, substs)
221 | TyKind::Tuple(_, substs)
222 | TyKind::OpaqueType(_, substs)
223 | TyKind::AssociatedType(_, substs)
224 | TyKind::Closure(.., substs) => Some(substs),
225 TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&mut substs.0),
226 _ => None,
227 }
228 }
229}
230 172
231#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] 173#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
232pub enum ImplTraitId { 174pub enum ImplTraitId {
diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs
index 41ebf6137..91116dcda 100644
--- a/crates/hir_ty/src/walk.rs
+++ b/crates/hir_ty/src/walk.rs
@@ -162,13 +162,15 @@ impl TypeWalk for Ty {
162 TyKind::Function(fn_pointer) => { 162 TyKind::Function(fn_pointer) => {
163 fn_pointer.substitution.0.walk(f); 163 fn_pointer.substitution.0.walk(f);
164 } 164 }
165 _ => { 165 TyKind::Adt(_, substs)
166 if let Some(substs) = self.substs() { 166 | TyKind::FnDef(_, substs)
167 for t in substs.iter(&Interner) { 167 | TyKind::Tuple(_, substs)
168 t.walk(f); 168 | TyKind::OpaqueType(_, substs)
169 } 169 | TyKind::AssociatedType(_, substs)
170 } 170 | TyKind::Closure(.., substs) => {
171 substs.walk(f);
171 } 172 }
173 _ => {}
172 } 174 }
173 f(self); 175 f(self);
174 } 176 }
@@ -199,11 +201,15 @@ impl TypeWalk for Ty {
199 TyKind::Function(fn_pointer) => { 201 TyKind::Function(fn_pointer) => {
200 fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); 202 fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in());
201 } 203 }
202 _ => { 204 TyKind::Adt(_, substs)
203 if let Some(substs) = self.substs_mut() { 205 | TyKind::FnDef(_, substs)
204 substs.walk_mut_binders(f, binders); 206 | TyKind::Tuple(_, substs)
205 } 207 | TyKind::OpaqueType(_, substs)
208 | TyKind::AssociatedType(_, substs)
209 | TyKind::Closure(.., substs) => {
210 substs.walk_mut_binders(f, binders);
206 } 211 }
212 _ => {}
207 } 213 }
208 f(self, binders); 214 f(self, binders);
209 } 215 }