aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir/src/lib.rs')
-rw-r--r--crates/hir/src/lib.rs132
1 files changed, 55 insertions, 77 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index b7ab03edf..30b96d7e2 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -58,7 +58,7 @@ use hir_ty::{
58 traits::{FnTrait, Solution, SolutionVariables}, 58 traits::{FnTrait, Solution, SolutionVariables},
59 AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, 59 AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast,
60 DebruijnIndex, InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar, 60 DebruijnIndex, InEnvironment, Interner, ProjectionTy, QuantifiedWhereClause, Scalar,
61 Substitution, Ty, TyDefId, TyKind, TyVariableKind, WhereClause, 61 Substitution, TraitEnvironment, Ty, TyDefId, TyKind, TyVariableKind, WhereClause,
62}; 62};
63use itertools::Itertools; 63use itertools::Itertools;
64use rustc_hash::FxHashSet; 64use rustc_hash::FxHashSet;
@@ -851,13 +851,7 @@ impl Function {
851 .iter() 851 .iter()
852 .enumerate() 852 .enumerate()
853 .map(|(idx, type_ref)| { 853 .map(|(idx, type_ref)| {
854 let ty = Type { 854 let ty = Type { krate, env: environment.clone(), ty: ctx.lower_ty(type_ref) };
855 krate,
856 ty: InEnvironment {
857 value: ctx.lower_ty(type_ref),
858 environment: environment.clone(),
859 },
860 };
861 Param { func: self, ty, idx } 855 Param { func: self, ty, idx }
862 }) 856 })
863 .collect() 857 .collect()
@@ -1540,8 +1534,8 @@ impl Impl {
1540 inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect() 1534 inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect()
1541 } 1535 }
1542 1536
1543 pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty }: Type) -> Vec<Impl> { 1537 pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> {
1544 let def_crates = match ty.value.def_crates(db, krate) { 1538 let def_crates = match ty.def_crates(db, krate) {
1545 Some(def_crates) => def_crates, 1539 Some(def_crates) => def_crates,
1546 None => return Vec::new(), 1540 None => return Vec::new(),
1547 }; 1541 };
@@ -1549,14 +1543,14 @@ impl Impl {
1549 let filter = |impl_def: &Impl| { 1543 let filter = |impl_def: &Impl| {
1550 let target_ty = impl_def.target_ty(db); 1544 let target_ty = impl_def.target_ty(db);
1551 let rref = target_ty.remove_ref(); 1545 let rref = target_ty.remove_ref();
1552 ty.value.equals_ctor(rref.as_ref().map_or(&target_ty.ty.value, |it| &it.ty.value)) 1546 ty.equals_ctor(rref.as_ref().map_or(&target_ty.ty, |it| &it.ty))
1553 }; 1547 };
1554 1548
1555 let mut all = Vec::new(); 1549 let mut all = Vec::new();
1556 def_crates.iter().for_each(|&id| { 1550 def_crates.iter().for_each(|&id| {
1557 all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter)) 1551 all.extend(db.inherent_impls_in_crate(id).all_impls().map(Self::from).filter(filter))
1558 }); 1552 });
1559 let fp = TyFingerprint::for_impl(&ty.value); 1553 let fp = TyFingerprint::for_impl(&ty);
1560 for id in def_crates 1554 for id in def_crates
1561 .iter() 1555 .iter()
1562 .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db)) 1556 .flat_map(|&id| Crate { id }.transitive_reverse_dependencies(db))
@@ -1643,7 +1637,8 @@ impl Impl {
1643#[derive(Clone, PartialEq, Eq, Debug)] 1637#[derive(Clone, PartialEq, Eq, Debug)]
1644pub struct Type { 1638pub struct Type {
1645 krate: CrateId, 1639 krate: CrateId,
1646 ty: InEnvironment<Ty>, 1640 env: Arc<TraitEnvironment>,
1641 ty: Ty,
1647} 1642}
1648 1643
1649impl Type { 1644impl Type {
@@ -1663,14 +1658,14 @@ impl Type {
1663 ) -> Type { 1658 ) -> Type {
1664 let environment = 1659 let environment =
1665 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); 1660 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d));
1666 Type { krate, ty: InEnvironment { value: ty, environment } } 1661 Type { krate, env: environment, ty }
1667 } 1662 }
1668 1663
1669 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { 1664 fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
1670 let resolver = lexical_env.resolver(db.upcast()); 1665 let resolver = lexical_env.resolver(db.upcast());
1671 let environment = 1666 let environment =
1672 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); 1667 resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d));
1673 Type { krate, ty: InEnvironment { value: ty, environment } } 1668 Type { krate, env: environment, ty }
1674 } 1669 }
1675 1670
1676 fn from_def( 1671 fn from_def(
@@ -1684,29 +1679,29 @@ impl Type {
1684 } 1679 }
1685 1680
1686 pub fn is_unit(&self) -> bool { 1681 pub fn is_unit(&self) -> bool {
1687 matches!(self.ty.value.interned(&Interner), TyKind::Tuple(0, ..)) 1682 matches!(self.ty.interned(&Interner), TyKind::Tuple(0, ..))
1688 } 1683 }
1689 pub fn is_bool(&self) -> bool { 1684 pub fn is_bool(&self) -> bool {
1690 matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Bool)) 1685 matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Bool))
1691 } 1686 }
1692 1687
1693 pub fn is_mutable_reference(&self) -> bool { 1688 pub fn is_mutable_reference(&self) -> bool {
1694 matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) 1689 matches!(self.ty.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..))
1695 } 1690 }
1696 1691
1697 pub fn is_usize(&self) -> bool { 1692 pub fn is_usize(&self) -> bool {
1698 matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) 1693 matches!(self.ty.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
1699 } 1694 }
1700 1695
1701 pub fn remove_ref(&self) -> Option<Type> { 1696 pub fn remove_ref(&self) -> Option<Type> {
1702 match &self.ty.value.interned(&Interner) { 1697 match &self.ty.interned(&Interner) {
1703 TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), 1698 TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
1704 _ => None, 1699 _ => None,
1705 } 1700 }
1706 } 1701 }
1707 1702
1708 pub fn is_unknown(&self) -> bool { 1703 pub fn is_unknown(&self) -> bool {
1709 self.ty.value.is_unknown() 1704 self.ty.is_unknown()
1710 } 1705 }
1711 1706
1712 /// Checks that particular type `ty` implements `std::future::Future`. 1707 /// Checks that particular type `ty` implements `std::future::Future`.
@@ -1723,14 +1718,12 @@ impl Type {
1723 None => return false, 1718 None => return false,
1724 }; 1719 };
1725 1720
1726 let canonical_ty = Canonical { 1721 let canonical_ty =
1727 value: self.ty.value.clone(), 1722 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1728 binders: CanonicalVarKinds::empty(&Interner),
1729 };
1730 method_resolution::implements_trait( 1723 method_resolution::implements_trait(
1731 &canonical_ty, 1724 &canonical_ty,
1732 db, 1725 db,
1733 self.ty.environment.clone(), 1726 self.env.clone(),
1734 krate, 1727 krate,
1735 std_future_trait, 1728 std_future_trait,
1736 ) 1729 )
@@ -1748,14 +1741,12 @@ impl Type {
1748 None => return false, 1741 None => return false,
1749 }; 1742 };
1750 1743
1751 let canonical_ty = Canonical { 1744 let canonical_ty =
1752 value: self.ty.value.clone(), 1745 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1753 binders: CanonicalVarKinds::empty(&Interner),
1754 };
1755 method_resolution::implements_trait_unique( 1746 method_resolution::implements_trait_unique(
1756 &canonical_ty, 1747 &canonical_ty,
1757 db, 1748 db,
1758 self.ty.environment.clone(), 1749 self.env.clone(),
1759 krate, 1750 krate,
1760 fnonce_trait, 1751 fnonce_trait,
1761 ) 1752 )
@@ -1765,16 +1756,13 @@ impl Type {
1765 let trait_ref = hir_ty::TraitRef { 1756 let trait_ref = hir_ty::TraitRef {
1766 trait_id: hir_ty::to_chalk_trait_id(trait_.id), 1757 trait_id: hir_ty::to_chalk_trait_id(trait_.id),
1767 substitution: Substitution::build_for_def(db, trait_.id) 1758 substitution: Substitution::build_for_def(db, trait_.id)
1768 .push(self.ty.value.clone()) 1759 .push(self.ty.clone())
1769 .fill(args.iter().map(|t| t.ty.value.clone())) 1760 .fill(args.iter().map(|t| t.ty.clone()))
1770 .build(), 1761 .build(),
1771 }; 1762 };
1772 1763
1773 let goal = Canonical { 1764 let goal = Canonical {
1774 value: hir_ty::InEnvironment::new( 1765 value: hir_ty::InEnvironment::new(self.env.env.clone(), trait_ref.cast(&Interner)),
1775 self.ty.environment.clone(),
1776 trait_ref.cast(&Interner),
1777 ),
1778 binders: CanonicalVarKinds::empty(&Interner), 1766 binders: CanonicalVarKinds::empty(&Interner),
1779 }; 1767 };
1780 1768
@@ -1789,12 +1777,12 @@ impl Type {
1789 alias: TypeAlias, 1777 alias: TypeAlias,
1790 ) -> Option<Type> { 1778 ) -> Option<Type> {
1791 let subst = Substitution::build_for_def(db, trait_.id) 1779 let subst = Substitution::build_for_def(db, trait_.id)
1792 .push(self.ty.value.clone()) 1780 .push(self.ty.clone())
1793 .fill(args.iter().map(|t| t.ty.value.clone())) 1781 .fill(args.iter().map(|t| t.ty.clone()))
1794 .build(); 1782 .build();
1795 let goal = Canonical::new( 1783 let goal = Canonical::new(
1796 InEnvironment::new( 1784 InEnvironment::new(
1797 self.ty.environment.clone(), 1785 self.env.env.clone(),
1798 AliasEq { 1786 AliasEq {
1799 alias: AliasTy::Projection(ProjectionTy { 1787 alias: AliasTy::Projection(ProjectionTy {
1800 associated_ty_id: to_assoc_type_id(alias.id), 1788 associated_ty_id: to_assoc_type_id(alias.id),
@@ -1826,22 +1814,22 @@ impl Type {
1826 } 1814 }
1827 1815
1828 pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { 1816 pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
1829 let def = self.ty.value.callable_def(db); 1817 let def = self.ty.callable_def(db);
1830 1818
1831 let sig = self.ty.value.callable_sig(db)?; 1819 let sig = self.ty.callable_sig(db)?;
1832 Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) 1820 Some(Callable { ty: self.clone(), sig, def, is_bound_method: false })
1833 } 1821 }
1834 1822
1835 pub fn is_closure(&self) -> bool { 1823 pub fn is_closure(&self) -> bool {
1836 matches!(&self.ty.value.interned(&Interner), TyKind::Closure { .. }) 1824 matches!(&self.ty.interned(&Interner), TyKind::Closure { .. })
1837 } 1825 }
1838 1826
1839 pub fn is_fn(&self) -> bool { 1827 pub fn is_fn(&self) -> bool {
1840 matches!(&self.ty.value.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) 1828 matches!(&self.ty.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. })
1841 } 1829 }
1842 1830
1843 pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { 1831 pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
1844 let adt_id = match self.ty.value.interned(&Interner) { 1832 let adt_id = match self.ty.interned(&Interner) {
1845 &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, 1833 &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
1846 _ => return false, 1834 _ => return false,
1847 }; 1835 };
@@ -1854,11 +1842,11 @@ impl Type {
1854 } 1842 }
1855 1843
1856 pub fn is_raw_ptr(&self) -> bool { 1844 pub fn is_raw_ptr(&self) -> bool {
1857 matches!(&self.ty.value.interned(&Interner), TyKind::Raw(..)) 1845 matches!(&self.ty.interned(&Interner), TyKind::Raw(..))
1858 } 1846 }
1859 1847
1860 pub fn contains_unknown(&self) -> bool { 1848 pub fn contains_unknown(&self) -> bool {
1861 return go(&self.ty.value); 1849 return go(&self.ty);
1862 1850
1863 fn go(ty: &Ty) -> bool { 1851 fn go(ty: &Ty) -> bool {
1864 match ty.interned(&Interner) { 1852 match ty.interned(&Interner) {
@@ -1890,7 +1878,7 @@ impl Type {
1890 } 1878 }
1891 1879
1892 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { 1880 pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
1893 let (variant_id, substs) = match self.ty.value.interned(&Interner) { 1881 let (variant_id, substs) = match self.ty.interned(&Interner) {
1894 &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), 1882 &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),
1895 &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), 1883 &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs),
1896 _ => return Vec::new(), 1884 _ => return Vec::new(),
@@ -1907,7 +1895,7 @@ impl Type {
1907 } 1895 }
1908 1896
1909 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { 1897 pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> {
1910 if let TyKind::Tuple(_, substs) = &self.ty.value.interned(&Interner) { 1898 if let TyKind::Tuple(_, substs) = &self.ty.interned(&Interner) {
1911 substs.iter().map(|ty| self.derived(ty.clone())).collect() 1899 substs.iter().map(|ty| self.derived(ty.clone())).collect()
1912 } else { 1900 } else {
1913 Vec::new() 1901 Vec::new()
@@ -1917,12 +1905,10 @@ impl Type {
1917 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a { 1905 pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Type> + 'a {
1918 // There should be no inference vars in types passed here 1906 // There should be no inference vars in types passed here
1919 // FIXME check that? 1907 // FIXME check that?
1920 let canonical = Canonical { 1908 let canonical =
1921 value: self.ty.value.clone(), 1909 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1922 binders: CanonicalVarKinds::empty(&Interner), 1910 let environment = self.env.env.clone();
1923 }; 1911 let ty = InEnvironment { goal: canonical, environment };
1924 let environment = self.ty.environment.clone();
1925 let ty = InEnvironment { value: canonical, environment };
1926 autoderef(db, Some(self.krate), ty) 1912 autoderef(db, Some(self.krate), ty)
1927 .map(|canonical| canonical.value) 1913 .map(|canonical| canonical.value)
1928 .map(move |ty| self.derived(ty)) 1914 .map(move |ty| self.derived(ty))
@@ -1936,10 +1922,10 @@ impl Type {
1936 krate: Crate, 1922 krate: Crate,
1937 mut callback: impl FnMut(AssocItem) -> Option<T>, 1923 mut callback: impl FnMut(AssocItem) -> Option<T>,
1938 ) -> Option<T> { 1924 ) -> Option<T> {
1939 for krate in self.ty.value.def_crates(db, krate.id)? { 1925 for krate in self.ty.def_crates(db, krate.id)? {
1940 let impls = db.inherent_impls_in_crate(krate); 1926 let impls = db.inherent_impls_in_crate(krate);
1941 1927
1942 for impl_def in impls.for_self_ty(&self.ty.value) { 1928 for impl_def in impls.for_self_ty(&self.ty) {
1943 for &item in db.impl_data(*impl_def).items.iter() { 1929 for &item in db.impl_data(*impl_def).items.iter() {
1944 if let Some(result) = callback(item.into()) { 1930 if let Some(result) = callback(item.into()) {
1945 return Some(result); 1931 return Some(result);
@@ -1952,7 +1938,6 @@ impl Type {
1952 1938
1953 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { 1939 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
1954 self.ty 1940 self.ty
1955 .value
1956 .strip_references() 1941 .strip_references()
1957 .substs() 1942 .substs()
1958 .into_iter() 1943 .into_iter()
@@ -1971,12 +1956,10 @@ impl Type {
1971 // There should be no inference vars in types passed here 1956 // There should be no inference vars in types passed here
1972 // FIXME check that? 1957 // FIXME check that?
1973 // FIXME replace Unknown by bound vars here 1958 // FIXME replace Unknown by bound vars here
1974 let canonical = Canonical { 1959 let canonical =
1975 value: self.ty.value.clone(), 1960 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
1976 binders: CanonicalVarKinds::empty(&Interner),
1977 };
1978 1961
1979 let env = self.ty.environment.clone(); 1962 let env = self.env.clone();
1980 let krate = krate.id; 1963 let krate = krate.id;
1981 1964
1982 method_resolution::iterate_method_candidates( 1965 method_resolution::iterate_method_candidates(
@@ -2005,12 +1988,10 @@ impl Type {
2005 // There should be no inference vars in types passed here 1988 // There should be no inference vars in types passed here
2006 // FIXME check that? 1989 // FIXME check that?
2007 // FIXME replace Unknown by bound vars here 1990 // FIXME replace Unknown by bound vars here
2008 let canonical = Canonical { 1991 let canonical =
2009 value: self.ty.value.clone(), 1992 Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(&Interner) };
2010 binders: CanonicalVarKinds::empty(&Interner),
2011 };
2012 1993
2013 let env = self.ty.environment.clone(); 1994 let env = self.env.clone();
2014 let krate = krate.id; 1995 let krate = krate.id;
2015 1996
2016 method_resolution::iterate_method_candidates( 1997 method_resolution::iterate_method_candidates(
@@ -2026,16 +2007,16 @@ impl Type {
2026 } 2007 }
2027 2008
2028 pub fn as_adt(&self) -> Option<Adt> { 2009 pub fn as_adt(&self) -> Option<Adt> {
2029 let (adt, _subst) = self.ty.value.as_adt()?; 2010 let (adt, _subst) = self.ty.as_adt()?;
2030 Some(adt.into()) 2011 Some(adt.into())
2031 } 2012 }
2032 2013
2033 pub fn as_dyn_trait(&self) -> Option<Trait> { 2014 pub fn as_dyn_trait(&self) -> Option<Trait> {
2034 self.ty.value.dyn_trait().map(Into::into) 2015 self.ty.dyn_trait().map(Into::into)
2035 } 2016 }
2036 2017
2037 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> { 2018 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> {
2038 self.ty.value.impl_trait_bounds(db).map(|it| { 2019 self.ty.impl_trait_bounds(db).map(|it| {
2039 it.into_iter() 2020 it.into_iter()
2040 .filter_map(|pred| match pred.skip_binders() { 2021 .filter_map(|pred| match pred.skip_binders() {
2041 hir_ty::WhereClause::Implemented(trait_ref) => { 2022 hir_ty::WhereClause::Implemented(trait_ref) => {
@@ -2048,14 +2029,11 @@ impl Type {
2048 } 2029 }
2049 2030
2050 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> { 2031 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> {
2051 self.ty.value.associated_type_parent_trait(db).map(Into::into) 2032 self.ty.associated_type_parent_trait(db).map(Into::into)
2052 } 2033 }
2053 2034
2054 fn derived(&self, ty: Ty) -> Type { 2035 fn derived(&self, ty: Ty) -> Type {
2055 Type { 2036 Type { krate: self.krate, env: self.env.clone(), ty }
2056 krate: self.krate,
2057 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() },
2058 }
2059 } 2037 }
2060 2038
2061 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) { 2039 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
@@ -2094,7 +2072,7 @@ impl Type {
2094 } 2072 }
2095 2073
2096 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { 2074 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
2097 let ty = type_.ty.value.strip_references(); 2075 let ty = type_.ty.strip_references();
2098 match ty.interned(&Interner) { 2076 match ty.interned(&Interner) {
2099 TyKind::Adt(..) => { 2077 TyKind::Adt(..) => {
2100 cb(type_.derived(ty.clone())); 2078 cb(type_.derived(ty.clone()));