diff options
author | Florian Diebold <[email protected]> | 2021-03-13 13:44:51 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-03-13 15:17:15 +0000 |
commit | 6c32bbf3ca5980fb33c1ea28dd1c5a1524ce81cb (patch) | |
tree | f81c7438f67de3c292a233887e56c7e99bcc0a01 /crates/hir | |
parent | 7accf6bc37c059a83a58c82f463f02a02ed2226f (diff) |
Separate `Ty` and `TyKind` like in Chalk
Currently `Ty` just wraps `TyKind`, but this allows us to change most
places to already use `intern` / `interned`.
Diffstat (limited to 'crates/hir')
-rw-r--r-- | crates/hir/src/lib.rs | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 58adc8fd3..7b9de11ed 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -54,8 +54,8 @@ use hir_ty::{ | |||
54 | method_resolution, | 54 | method_resolution, |
55 | traits::{FnTrait, Solution, SolutionVariables}, | 55 | traits::{FnTrait, Solution, SolutionVariables}, |
56 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, | 56 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, |
57 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, | 57 | InEnvironment, Interner, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, |
58 | Ty, TyDefId, TyVariableKind, | 58 | TraitEnvironment, Ty, TyDefId, TyKind, TyVariableKind, |
59 | }; | 59 | }; |
60 | use rustc_hash::FxHashSet; | 60 | use rustc_hash::FxHashSet; |
61 | use stdx::{format_to, impl_from}; | 61 | use stdx::{format_to, impl_from}; |
@@ -677,7 +677,7 @@ impl_from!(Struct, Union, Enum for Adt); | |||
677 | impl Adt { | 677 | impl Adt { |
678 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | 678 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { |
679 | let subst = db.generic_defaults(self.into()); | 679 | let subst = db.generic_defaults(self.into()); |
680 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | 680 | subst.iter().any(|ty| ty.value.is_unknown()) |
681 | } | 681 | } |
682 | 682 | ||
683 | /// Turns this ADT into a type. Any type parameters of the ADT will be | 683 | /// Turns this ADT into a type. Any type parameters of the ADT will be |
@@ -1012,7 +1012,7 @@ pub struct TypeAlias { | |||
1012 | impl TypeAlias { | 1012 | impl TypeAlias { |
1013 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | 1013 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { |
1014 | let subst = db.generic_defaults(self.id.into()); | 1014 | let subst = db.generic_defaults(self.id.into()); |
1015 | subst.iter().any(|ty| &ty.value == &Ty::Unknown) | 1015 | subst.iter().any(|ty| ty.value.is_unknown()) |
1016 | } | 1016 | } |
1017 | 1017 | ||
1018 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 1018 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
@@ -1384,7 +1384,7 @@ impl TypeParam { | |||
1384 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 1384 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
1385 | let resolver = self.id.parent.resolver(db.upcast()); | 1385 | let resolver = self.id.parent.resolver(db.upcast()); |
1386 | let krate = self.id.parent.module(db.upcast()).krate(); | 1386 | let krate = self.id.parent.module(db.upcast()).krate(); |
1387 | let ty = Ty::Placeholder(self.id); | 1387 | let ty = TyKind::Placeholder(self.id).intern(&Interner); |
1388 | Type::new_with_resolver_inner(db, krate, &resolver, ty) | 1388 | Type::new_with_resolver_inner(db, krate, &resolver, ty) |
1389 | } | 1389 | } |
1390 | 1390 | ||
@@ -1584,25 +1584,25 @@ impl Type { | |||
1584 | } | 1584 | } |
1585 | 1585 | ||
1586 | pub fn is_unit(&self) -> bool { | 1586 | pub fn is_unit(&self) -> bool { |
1587 | matches!(self.ty.value, Ty::Tuple(0, ..)) | 1587 | matches!(self.ty.value.interned(&Interner), TyKind::Tuple(0, ..)) |
1588 | } | 1588 | } |
1589 | pub fn is_bool(&self) -> bool { | 1589 | pub fn is_bool(&self) -> bool { |
1590 | matches!(self.ty.value, Ty::Scalar(Scalar::Bool)) | 1590 | matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Bool)) |
1591 | } | 1591 | } |
1592 | 1592 | ||
1593 | pub fn is_mutable_reference(&self) -> bool { | 1593 | pub fn is_mutable_reference(&self) -> bool { |
1594 | matches!(self.ty.value, Ty::Ref(hir_ty::Mutability::Mut, ..)) | 1594 | matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) |
1595 | } | 1595 | } |
1596 | 1596 | ||
1597 | pub fn remove_ref(&self) -> Option<Type> { | 1597 | pub fn remove_ref(&self) -> Option<Type> { |
1598 | match &self.ty.value { | 1598 | match &self.ty.value.interned(&Interner) { |
1599 | Ty::Ref(.., substs) => Some(self.derived(substs[0].clone())), | 1599 | TyKind::Ref(.., substs) => Some(self.derived(substs[0].clone())), |
1600 | _ => None, | 1600 | _ => None, |
1601 | } | 1601 | } |
1602 | } | 1602 | } |
1603 | 1603 | ||
1604 | pub fn is_unknown(&self) -> bool { | 1604 | pub fn is_unknown(&self) -> bool { |
1605 | matches!(self.ty.value, Ty::Unknown) | 1605 | self.ty.value.is_unknown() |
1606 | } | 1606 | } |
1607 | 1607 | ||
1608 | /// Checks that particular type `ty` implements `std::future::Future`. | 1608 | /// Checks that particular type `ty` implements `std::future::Future`. |
@@ -1684,7 +1684,7 @@ impl Type { | |||
1684 | .build(); | 1684 | .build(); |
1685 | let predicate = ProjectionPredicate { | 1685 | let predicate = ProjectionPredicate { |
1686 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | 1686 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, |
1687 | ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)), | 1687 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner), |
1688 | }; | 1688 | }; |
1689 | let goal = Canonical { | 1689 | let goal = Canonical { |
1690 | value: InEnvironment::new( | 1690 | value: InEnvironment::new( |
@@ -1712,8 +1712,8 @@ impl Type { | |||
1712 | } | 1712 | } |
1713 | 1713 | ||
1714 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { | 1714 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { |
1715 | let def = match self.ty.value { | 1715 | let def = match self.ty.value.interned(&Interner) { |
1716 | Ty::FnDef(def, _) => Some(def), | 1716 | &TyKind::FnDef(def, _) => Some(def), |
1717 | _ => None, | 1717 | _ => None, |
1718 | }; | 1718 | }; |
1719 | 1719 | ||
@@ -1722,16 +1722,16 @@ impl Type { | |||
1722 | } | 1722 | } |
1723 | 1723 | ||
1724 | pub fn is_closure(&self) -> bool { | 1724 | pub fn is_closure(&self) -> bool { |
1725 | matches!(&self.ty.value, Ty::Closure { .. }) | 1725 | matches!(&self.ty.value.interned(&Interner), TyKind::Closure { .. }) |
1726 | } | 1726 | } |
1727 | 1727 | ||
1728 | pub fn is_fn(&self) -> bool { | 1728 | pub fn is_fn(&self) -> bool { |
1729 | matches!(&self.ty.value, Ty::FnDef(..) | Ty::Function { .. }) | 1729 | matches!(&self.ty.value.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) |
1730 | } | 1730 | } |
1731 | 1731 | ||
1732 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { | 1732 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { |
1733 | let adt_id = match self.ty.value { | 1733 | let adt_id = match self.ty.value.interned(&Interner) { |
1734 | Ty::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, | 1734 | &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, |
1735 | _ => return false, | 1735 | _ => return false, |
1736 | }; | 1736 | }; |
1737 | 1737 | ||
@@ -1743,24 +1743,25 @@ impl Type { | |||
1743 | } | 1743 | } |
1744 | 1744 | ||
1745 | pub fn is_raw_ptr(&self) -> bool { | 1745 | pub fn is_raw_ptr(&self) -> bool { |
1746 | matches!(&self.ty.value, Ty::Raw(..)) | 1746 | matches!(&self.ty.value.interned(&Interner), TyKind::Raw(..)) |
1747 | } | 1747 | } |
1748 | 1748 | ||
1749 | pub fn contains_unknown(&self) -> bool { | 1749 | pub fn contains_unknown(&self) -> bool { |
1750 | return go(&self.ty.value); | 1750 | return go(&self.ty.value); |
1751 | 1751 | ||
1752 | fn go(ty: &Ty) -> bool { | 1752 | fn go(ty: &Ty) -> bool { |
1753 | match ty { | 1753 | if ty.is_unknown() { |
1754 | Ty::Unknown => true, | 1754 | true |
1755 | _ => ty.substs().map_or(false, |substs| substs.iter().any(go)), | 1755 | } else { |
1756 | ty.substs().map_or(false, |substs| substs.iter().any(go)) | ||
1756 | } | 1757 | } |
1757 | } | 1758 | } |
1758 | } | 1759 | } |
1759 | 1760 | ||
1760 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | 1761 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { |
1761 | let (variant_id, substs) = match self.ty.value { | 1762 | let (variant_id, substs) = match self.ty.value.interned(&Interner) { |
1762 | Ty::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), | 1763 | &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), |
1763 | Ty::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), | 1764 | &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), |
1764 | _ => return Vec::new(), | 1765 | _ => return Vec::new(), |
1765 | }; | 1766 | }; |
1766 | 1767 | ||
@@ -1775,7 +1776,7 @@ impl Type { | |||
1775 | } | 1776 | } |
1776 | 1777 | ||
1777 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { | 1778 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { |
1778 | if let Ty::Tuple(_, substs) = &self.ty.value { | 1779 | if let TyKind::Tuple(_, substs) = &self.ty.value.interned(&Interner) { |
1779 | substs.iter().map(|ty| self.derived(ty.clone())).collect() | 1780 | substs.iter().map(|ty| self.derived(ty.clone())).collect() |
1780 | } else { | 1781 | } else { |
1781 | Vec::new() | 1782 | Vec::new() |
@@ -1957,33 +1958,33 @@ impl Type { | |||
1957 | 1958 | ||
1958 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 1959 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
1959 | let ty = type_.ty.value.strip_references(); | 1960 | let ty = type_.ty.value.strip_references(); |
1960 | match ty { | 1961 | match ty.interned(&Interner) { |
1961 | Ty::Adt(..) => { | 1962 | TyKind::Adt(..) => { |
1962 | cb(type_.derived(ty.clone())); | 1963 | cb(type_.derived(ty.clone())); |
1963 | } | 1964 | } |
1964 | Ty::AssociatedType(..) => { | 1965 | TyKind::AssociatedType(..) => { |
1965 | if let Some(_) = ty.associated_type_parent_trait(db) { | 1966 | if let Some(_) = ty.associated_type_parent_trait(db) { |
1966 | cb(type_.derived(ty.clone())); | 1967 | cb(type_.derived(ty.clone())); |
1967 | } | 1968 | } |
1968 | } | 1969 | } |
1969 | Ty::OpaqueType(..) => { | 1970 | TyKind::OpaqueType(..) => { |
1970 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1971 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1971 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1972 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1972 | } | 1973 | } |
1973 | } | 1974 | } |
1974 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | 1975 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
1975 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1976 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1976 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1977 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1977 | } | 1978 | } |
1978 | 1979 | ||
1979 | walk_substs(db, type_, &opaque_ty.parameters, cb); | 1980 | walk_substs(db, type_, &opaque_ty.parameters, cb); |
1980 | } | 1981 | } |
1981 | Ty::Placeholder(_) => { | 1982 | TyKind::Placeholder(_) => { |
1982 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1983 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1983 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1984 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1984 | } | 1985 | } |
1985 | } | 1986 | } |
1986 | Ty::Dyn(bounds) => { | 1987 | TyKind::Dyn(bounds) => { |
1987 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); | 1988 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); |
1988 | } | 1989 | } |
1989 | 1990 | ||