diff options
Diffstat (limited to 'crates/hir/src/lib.rs')
-rw-r--r-- | crates/hir/src/lib.rs | 89 |
1 files changed, 46 insertions, 43 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 58adc8fd3..10b8171be 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -51,11 +51,11 @@ use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | |||
51 | use hir_ty::{ | 51 | use hir_ty::{ |
52 | autoderef, | 52 | autoderef, |
53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | 53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, |
54 | method_resolution, | 54 | method_resolution, to_assoc_type_id, |
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, Ty, |
58 | Ty, TyDefId, TyVariableKind, | 58 | 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 |
@@ -802,7 +802,7 @@ impl Function { | |||
802 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); | 802 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); |
803 | let ret_type = &db.function_data(self.id).ret_type; | 803 | let ret_type = &db.function_data(self.id).ret_type; |
804 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | 804 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); |
805 | let ty = Ty::from_hir_ext(&ctx, ret_type).0; | 805 | let ty = ctx.lower_ty(ret_type); |
806 | Type::new_with_resolver_inner(db, krate, &resolver, ty) | 806 | Type::new_with_resolver_inner(db, krate, &resolver, ty) |
807 | } | 807 | } |
808 | 808 | ||
@@ -817,7 +817,7 @@ impl Function { | |||
817 | let resolver = self.id.resolver(db.upcast()); | 817 | let resolver = self.id.resolver(db.upcast()); |
818 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); | 818 | let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate(); |
819 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | 819 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); |
820 | let environment = TraitEnvironment::lower(db, &resolver); | 820 | let environment = db.trait_environment(self.id.into()); |
821 | db.function_data(self.id) | 821 | db.function_data(self.id) |
822 | .params | 822 | .params |
823 | .iter() | 823 | .iter() |
@@ -825,7 +825,7 @@ impl Function { | |||
825 | let ty = Type { | 825 | let ty = Type { |
826 | krate, | 826 | krate, |
827 | ty: InEnvironment { | 827 | ty: InEnvironment { |
828 | value: Ty::from_hir_ext(&ctx, type_ref).0, | 828 | value: ctx.lower_ty(type_ref), |
829 | environment: environment.clone(), | 829 | environment: environment.clone(), |
830 | }, | 830 | }, |
831 | }; | 831 | }; |
@@ -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(hir_ty::to_placeholder_idx(db, 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 | ||
@@ -1499,7 +1499,7 @@ impl Impl { | |||
1499 | let resolver = self.id.resolver(db.upcast()); | 1499 | let resolver = self.id.resolver(db.upcast()); |
1500 | let krate = self.id.lookup(db.upcast()).container.krate(); | 1500 | let krate = self.id.lookup(db.upcast()).container.krate(); |
1501 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | 1501 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); |
1502 | let ty = Ty::from_hir(&ctx, &impl_data.target_type); | 1502 | let ty = ctx.lower_ty(&impl_data.target_type); |
1503 | Type::new_with_resolver_inner(db, krate, &resolver, ty) | 1503 | Type::new_with_resolver_inner(db, krate, &resolver, ty) |
1504 | } | 1504 | } |
1505 | 1505 | ||
@@ -1563,13 +1563,15 @@ impl Type { | |||
1563 | resolver: &Resolver, | 1563 | resolver: &Resolver, |
1564 | ty: Ty, | 1564 | ty: Ty, |
1565 | ) -> Type { | 1565 | ) -> Type { |
1566 | let environment = TraitEnvironment::lower(db, &resolver); | 1566 | let environment = |
1567 | resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); | ||
1567 | Type { krate, ty: InEnvironment { value: ty, environment } } | 1568 | Type { krate, ty: InEnvironment { value: ty, environment } } |
1568 | } | 1569 | } |
1569 | 1570 | ||
1570 | fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { | 1571 | fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { |
1571 | let resolver = lexical_env.resolver(db.upcast()); | 1572 | let resolver = lexical_env.resolver(db.upcast()); |
1572 | let environment = TraitEnvironment::lower(db, &resolver); | 1573 | let environment = |
1574 | resolver.generic_def().map_or_else(Default::default, |d| db.trait_environment(d)); | ||
1573 | Type { krate, ty: InEnvironment { value: ty, environment } } | 1575 | Type { krate, ty: InEnvironment { value: ty, environment } } |
1574 | } | 1576 | } |
1575 | 1577 | ||
@@ -1584,25 +1586,25 @@ impl Type { | |||
1584 | } | 1586 | } |
1585 | 1587 | ||
1586 | pub fn is_unit(&self) -> bool { | 1588 | pub fn is_unit(&self) -> bool { |
1587 | matches!(self.ty.value, Ty::Tuple(0, ..)) | 1589 | matches!(self.ty.value.interned(&Interner), TyKind::Tuple(0, ..)) |
1588 | } | 1590 | } |
1589 | pub fn is_bool(&self) -> bool { | 1591 | pub fn is_bool(&self) -> bool { |
1590 | matches!(self.ty.value, Ty::Scalar(Scalar::Bool)) | 1592 | matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Bool)) |
1591 | } | 1593 | } |
1592 | 1594 | ||
1593 | pub fn is_mutable_reference(&self) -> bool { | 1595 | pub fn is_mutable_reference(&self) -> bool { |
1594 | matches!(self.ty.value, Ty::Ref(hir_ty::Mutability::Mut, ..)) | 1596 | matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) |
1595 | } | 1597 | } |
1596 | 1598 | ||
1597 | pub fn remove_ref(&self) -> Option<Type> { | 1599 | pub fn remove_ref(&self) -> Option<Type> { |
1598 | match &self.ty.value { | 1600 | match &self.ty.value.interned(&Interner) { |
1599 | Ty::Ref(.., substs) => Some(self.derived(substs[0].clone())), | 1601 | TyKind::Ref(.., substs) => Some(self.derived(substs[0].clone())), |
1600 | _ => None, | 1602 | _ => None, |
1601 | } | 1603 | } |
1602 | } | 1604 | } |
1603 | 1605 | ||
1604 | pub fn is_unknown(&self) -> bool { | 1606 | pub fn is_unknown(&self) -> bool { |
1605 | matches!(self.ty.value, Ty::Unknown) | 1607 | self.ty.value.is_unknown() |
1606 | } | 1608 | } |
1607 | 1609 | ||
1608 | /// Checks that particular type `ty` implements `std::future::Future`. | 1610 | /// Checks that particular type `ty` implements `std::future::Future`. |
@@ -1683,8 +1685,11 @@ impl Type { | |||
1683 | .fill(args.iter().map(|t| t.ty.value.clone())) | 1685 | .fill(args.iter().map(|t| t.ty.value.clone())) |
1684 | .build(); | 1686 | .build(); |
1685 | let predicate = ProjectionPredicate { | 1687 | let predicate = ProjectionPredicate { |
1686 | projection_ty: ProjectionTy { associated_ty: alias.id, parameters: subst }, | 1688 | projection_ty: ProjectionTy { |
1687 | ty: Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)), | 1689 | associated_ty: to_assoc_type_id(alias.id), |
1690 | parameters: subst, | ||
1691 | }, | ||
1692 | ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner), | ||
1688 | }; | 1693 | }; |
1689 | let goal = Canonical { | 1694 | let goal = Canonical { |
1690 | value: InEnvironment::new( | 1695 | value: InEnvironment::new( |
@@ -1712,26 +1717,23 @@ impl Type { | |||
1712 | } | 1717 | } |
1713 | 1718 | ||
1714 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { | 1719 | pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> { |
1715 | let def = match self.ty.value { | 1720 | let def = self.ty.value.callable_def(db); |
1716 | Ty::FnDef(def, _) => Some(def), | ||
1717 | _ => None, | ||
1718 | }; | ||
1719 | 1721 | ||
1720 | let sig = self.ty.value.callable_sig(db)?; | 1722 | let sig = self.ty.value.callable_sig(db)?; |
1721 | Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) | 1723 | Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) |
1722 | } | 1724 | } |
1723 | 1725 | ||
1724 | pub fn is_closure(&self) -> bool { | 1726 | pub fn is_closure(&self) -> bool { |
1725 | matches!(&self.ty.value, Ty::Closure { .. }) | 1727 | matches!(&self.ty.value.interned(&Interner), TyKind::Closure { .. }) |
1726 | } | 1728 | } |
1727 | 1729 | ||
1728 | pub fn is_fn(&self) -> bool { | 1730 | pub fn is_fn(&self) -> bool { |
1729 | matches!(&self.ty.value, Ty::FnDef(..) | Ty::Function { .. }) | 1731 | matches!(&self.ty.value.interned(&Interner), TyKind::FnDef(..) | TyKind::Function { .. }) |
1730 | } | 1732 | } |
1731 | 1733 | ||
1732 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { | 1734 | pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { |
1733 | let adt_id = match self.ty.value { | 1735 | let adt_id = match self.ty.value.interned(&Interner) { |
1734 | Ty::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, | 1736 | &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, |
1735 | _ => return false, | 1737 | _ => return false, |
1736 | }; | 1738 | }; |
1737 | 1739 | ||
@@ -1743,24 +1745,25 @@ impl Type { | |||
1743 | } | 1745 | } |
1744 | 1746 | ||
1745 | pub fn is_raw_ptr(&self) -> bool { | 1747 | pub fn is_raw_ptr(&self) -> bool { |
1746 | matches!(&self.ty.value, Ty::Raw(..)) | 1748 | matches!(&self.ty.value.interned(&Interner), TyKind::Raw(..)) |
1747 | } | 1749 | } |
1748 | 1750 | ||
1749 | pub fn contains_unknown(&self) -> bool { | 1751 | pub fn contains_unknown(&self) -> bool { |
1750 | return go(&self.ty.value); | 1752 | return go(&self.ty.value); |
1751 | 1753 | ||
1752 | fn go(ty: &Ty) -> bool { | 1754 | fn go(ty: &Ty) -> bool { |
1753 | match ty { | 1755 | if ty.is_unknown() { |
1754 | Ty::Unknown => true, | 1756 | true |
1755 | _ => ty.substs().map_or(false, |substs| substs.iter().any(go)), | 1757 | } else { |
1758 | ty.substs().map_or(false, |substs| substs.iter().any(go)) | ||
1756 | } | 1759 | } |
1757 | } | 1760 | } |
1758 | } | 1761 | } |
1759 | 1762 | ||
1760 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | 1763 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { |
1761 | let (variant_id, substs) = match self.ty.value { | 1764 | let (variant_id, substs) = match self.ty.value.interned(&Interner) { |
1762 | Ty::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), | 1765 | &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), | 1766 | &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), |
1764 | _ => return Vec::new(), | 1767 | _ => return Vec::new(), |
1765 | }; | 1768 | }; |
1766 | 1769 | ||
@@ -1775,7 +1778,7 @@ impl Type { | |||
1775 | } | 1778 | } |
1776 | 1779 | ||
1777 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { | 1780 | pub fn tuple_fields(&self, _db: &dyn HirDatabase) -> Vec<Type> { |
1778 | if let Ty::Tuple(_, substs) = &self.ty.value { | 1781 | if let TyKind::Tuple(_, substs) = &self.ty.value.interned(&Interner) { |
1779 | substs.iter().map(|ty| self.derived(ty.clone())).collect() | 1782 | substs.iter().map(|ty| self.derived(ty.clone())).collect() |
1780 | } else { | 1783 | } else { |
1781 | Vec::new() | 1784 | Vec::new() |
@@ -1957,33 +1960,33 @@ impl Type { | |||
1957 | 1960 | ||
1958 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { | 1961 | fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { |
1959 | let ty = type_.ty.value.strip_references(); | 1962 | let ty = type_.ty.value.strip_references(); |
1960 | match ty { | 1963 | match ty.interned(&Interner) { |
1961 | Ty::Adt(..) => { | 1964 | TyKind::Adt(..) => { |
1962 | cb(type_.derived(ty.clone())); | 1965 | cb(type_.derived(ty.clone())); |
1963 | } | 1966 | } |
1964 | Ty::AssociatedType(..) => { | 1967 | TyKind::AssociatedType(..) => { |
1965 | if let Some(_) = ty.associated_type_parent_trait(db) { | 1968 | if let Some(_) = ty.associated_type_parent_trait(db) { |
1966 | cb(type_.derived(ty.clone())); | 1969 | cb(type_.derived(ty.clone())); |
1967 | } | 1970 | } |
1968 | } | 1971 | } |
1969 | Ty::OpaqueType(..) => { | 1972 | TyKind::OpaqueType(..) => { |
1970 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1973 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1971 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1974 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1972 | } | 1975 | } |
1973 | } | 1976 | } |
1974 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | 1977 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
1975 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1978 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1976 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1979 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1977 | } | 1980 | } |
1978 | 1981 | ||
1979 | walk_substs(db, type_, &opaque_ty.parameters, cb); | 1982 | walk_substs(db, type_, &opaque_ty.parameters, cb); |
1980 | } | 1983 | } |
1981 | Ty::Placeholder(_) => { | 1984 | TyKind::Placeholder(_) => { |
1982 | if let Some(bounds) = ty.impl_trait_bounds(db) { | 1985 | if let Some(bounds) = ty.impl_trait_bounds(db) { |
1983 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); | 1986 | walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); |
1984 | } | 1987 | } |
1985 | } | 1988 | } |
1986 | Ty::Dyn(bounds) => { | 1989 | TyKind::Dyn(bounds) => { |
1987 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); | 1990 | walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb); |
1988 | } | 1991 | } |
1989 | 1992 | ||