diff options
Diffstat (limited to 'crates/hir/src/lib.rs')
-rw-r--r-- | crates/hir/src/lib.rs | 90 |
1 files changed, 66 insertions, 24 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 079a5f7b8..ad79a79f8 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -29,6 +29,8 @@ mod has_source; | |||
29 | pub mod diagnostics; | 29 | pub mod diagnostics; |
30 | pub mod db; | 30 | pub mod db; |
31 | 31 | ||
32 | mod display; | ||
33 | |||
32 | use std::{iter, sync::Arc}; | 34 | use std::{iter, sync::Arc}; |
33 | 35 | ||
34 | use arrayvec::ArrayVec; | 36 | use arrayvec::ArrayVec; |
@@ -50,8 +52,8 @@ use hir_def::{ | |||
50 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | 52 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; |
51 | use hir_ty::{ | 53 | use hir_ty::{ |
52 | autoderef, | 54 | autoderef, |
53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | ||
54 | method_resolution::{self, TyFingerprint}, | 55 | method_resolution::{self, TyFingerprint}, |
56 | primitive::UintTy, | ||
55 | to_assoc_type_id, | 57 | to_assoc_type_id, |
56 | traits::{FnTrait, Solution, SolutionVariables}, | 58 | traits::{FnTrait, Solution, SolutionVariables}, |
57 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, | 59 | AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate, |
@@ -579,6 +581,12 @@ impl Struct { | |||
579 | } | 581 | } |
580 | } | 582 | } |
581 | 583 | ||
584 | impl HasVisibility for Struct { | ||
585 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
586 | db.struct_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
587 | } | ||
588 | } | ||
589 | |||
582 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 590 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
583 | pub struct Union { | 591 | pub struct Union { |
584 | pub(crate) id: UnionId, | 592 | pub(crate) id: UnionId, |
@@ -611,6 +619,12 @@ impl Union { | |||
611 | } | 619 | } |
612 | } | 620 | } |
613 | 621 | ||
622 | impl HasVisibility for Union { | ||
623 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
624 | db.union_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
625 | } | ||
626 | } | ||
627 | |||
614 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 628 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
615 | pub struct Enum { | 629 | pub struct Enum { |
616 | pub(crate) id: EnumId, | 630 | pub(crate) id: EnumId, |
@@ -638,6 +652,12 @@ impl Enum { | |||
638 | } | 652 | } |
639 | } | 653 | } |
640 | 654 | ||
655 | impl HasVisibility for Enum { | ||
656 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
657 | db.enum_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
658 | } | ||
659 | } | ||
660 | |||
641 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 661 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
642 | pub struct Variant { | 662 | pub struct Variant { |
643 | pub(crate) parent: Enum, | 663 | pub(crate) parent: Enum, |
@@ -829,7 +849,8 @@ impl Function { | |||
829 | db.function_data(self.id) | 849 | db.function_data(self.id) |
830 | .params | 850 | .params |
831 | .iter() | 851 | .iter() |
832 | .map(|type_ref| { | 852 | .enumerate() |
853 | .map(|(idx, type_ref)| { | ||
833 | let ty = Type { | 854 | let ty = Type { |
834 | krate, | 855 | krate, |
835 | ty: InEnvironment { | 856 | ty: InEnvironment { |
@@ -837,7 +858,7 @@ impl Function { | |||
837 | environment: environment.clone(), | 858 | environment: environment.clone(), |
838 | }, | 859 | }, |
839 | }; | 860 | }; |
840 | Param { ty } | 861 | Param { func: self, ty, idx } |
841 | }) | 862 | }) |
842 | .collect() | 863 | .collect() |
843 | } | 864 | } |
@@ -851,7 +872,7 @@ impl Function { | |||
851 | } | 872 | } |
852 | 873 | ||
853 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { | 874 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { |
854 | db.function_data(self.id).is_unsafe | 875 | db.function_data(self.id).qualifier.is_unsafe |
855 | } | 876 | } |
856 | 877 | ||
857 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 878 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
@@ -900,6 +921,9 @@ impl From<hir_ty::Mutability> for Access { | |||
900 | 921 | ||
901 | #[derive(Debug)] | 922 | #[derive(Debug)] |
902 | pub struct Param { | 923 | pub struct Param { |
924 | func: Function, | ||
925 | /// The index in parameter list, including self parameter. | ||
926 | idx: usize, | ||
903 | ty: Type, | 927 | ty: Type, |
904 | } | 928 | } |
905 | 929 | ||
@@ -907,6 +931,15 @@ impl Param { | |||
907 | pub fn ty(&self) -> &Type { | 931 | pub fn ty(&self) -> &Type { |
908 | &self.ty | 932 | &self.ty |
909 | } | 933 | } |
934 | |||
935 | pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> { | ||
936 | let params = self.func.source(db)?.value.param_list()?; | ||
937 | if params.self_param().is_some() { | ||
938 | params.params().nth(self.idx.checked_sub(1)?)?.pat() | ||
939 | } else { | ||
940 | params.params().nth(self.idx)?.pat() | ||
941 | } | ||
942 | } | ||
910 | } | 943 | } |
911 | 944 | ||
912 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 945 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -929,6 +962,14 @@ impl SelfParam { | |||
929 | }) | 962 | }) |
930 | .unwrap_or(Access::Owned) | 963 | .unwrap_or(Access::Owned) |
931 | } | 964 | } |
965 | |||
966 | pub fn display(self, db: &dyn HirDatabase) -> &'static str { | ||
967 | match self.access(db) { | ||
968 | Access::Shared => "&self", | ||
969 | Access::Exclusive => "&mut self", | ||
970 | Access::Owned => "self", | ||
971 | } | ||
972 | } | ||
932 | } | 973 | } |
933 | 974 | ||
934 | impl HasVisibility for Function { | 975 | impl HasVisibility for Function { |
@@ -956,6 +997,10 @@ impl Const { | |||
956 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | 997 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { |
957 | db.const_data(self.id).name.clone() | 998 | db.const_data(self.id).name.clone() |
958 | } | 999 | } |
1000 | |||
1001 | pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef { | ||
1002 | db.const_data(self.id).type_ref.clone() | ||
1003 | } | ||
959 | } | 1004 | } |
960 | 1005 | ||
961 | impl HasVisibility for Const { | 1006 | impl HasVisibility for Const { |
@@ -989,6 +1034,12 @@ impl Static { | |||
989 | } | 1034 | } |
990 | } | 1035 | } |
991 | 1036 | ||
1037 | impl HasVisibility for Static { | ||
1038 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1039 | db.static_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
1040 | } | ||
1041 | } | ||
1042 | |||
992 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 1043 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
993 | pub struct Trait { | 1044 | pub struct Trait { |
994 | pub(crate) id: TraitId, | 1045 | pub(crate) id: TraitId, |
@@ -1008,7 +1059,13 @@ impl Trait { | |||
1008 | } | 1059 | } |
1009 | 1060 | ||
1010 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { | 1061 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { |
1011 | db.trait_data(self.id).auto | 1062 | db.trait_data(self.id).is_auto |
1063 | } | ||
1064 | } | ||
1065 | |||
1066 | impl HasVisibility for Trait { | ||
1067 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1068 | db.trait_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
1012 | } | 1069 | } |
1013 | } | 1070 | } |
1014 | 1071 | ||
@@ -1420,19 +1477,6 @@ impl TypeParam { | |||
1420 | } | 1477 | } |
1421 | } | 1478 | } |
1422 | 1479 | ||
1423 | impl HirDisplay for TypeParam { | ||
1424 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
1425 | write!(f, "{}", self.name(f.db))?; | ||
1426 | let bounds = f.db.generic_predicates_for_param(self.id); | ||
1427 | let substs = Substs::type_params(f.db, self.id.parent); | ||
1428 | let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>(); | ||
1429 | if !(predicates.is_empty() || f.omit_verbose_types()) { | ||
1430 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; | ||
1431 | } | ||
1432 | Ok(()) | ||
1433 | } | ||
1434 | } | ||
1435 | |||
1436 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 1480 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
1437 | pub struct LifetimeParam { | 1481 | pub struct LifetimeParam { |
1438 | pub(crate) id: LifetimeParamId, | 1482 | pub(crate) id: LifetimeParamId, |
@@ -1646,6 +1690,10 @@ impl Type { | |||
1646 | matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) | 1690 | matches!(self.ty.value.interned(&Interner), TyKind::Ref(hir_ty::Mutability::Mut, ..)) |
1647 | } | 1691 | } |
1648 | 1692 | ||
1693 | pub fn is_usize(&self) -> bool { | ||
1694 | matches!(self.ty.value.interned(&Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) | ||
1695 | } | ||
1696 | |||
1649 | pub fn remove_ref(&self) -> Option<Type> { | 1697 | pub fn remove_ref(&self) -> Option<Type> { |
1650 | match &self.ty.value.interned(&Interner) { | 1698 | match &self.ty.value.interned(&Interner) { |
1651 | TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), | 1699 | TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), |
@@ -2069,12 +2117,6 @@ impl Type { | |||
2069 | } | 2117 | } |
2070 | } | 2118 | } |
2071 | 2119 | ||
2072 | impl HirDisplay for Type { | ||
2073 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
2074 | self.ty.value.hir_fmt(f) | ||
2075 | } | ||
2076 | } | ||
2077 | |||
2078 | // FIXME: closures | 2120 | // FIXME: closures |
2079 | #[derive(Debug)] | 2121 | #[derive(Debug)] |
2080 | pub struct Callable { | 2122 | pub struct Callable { |