diff options
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 25ab9d872..f22232324 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -73,6 +73,7 @@ pub use lower::{ | |||
73 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; | 73 | pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; |
74 | 74 | ||
75 | pub use chalk_ir::{BoundVar, DebruijnIndex}; | 75 | pub use chalk_ir::{BoundVar, DebruijnIndex}; |
76 | use itertools::Itertools; | ||
76 | 77 | ||
77 | /// A type constructor or type name: this might be something like the primitive | 78 | /// A type constructor or type name: this might be something like the primitive |
78 | /// type `bool`, a struct like `Vec`, or things like function pointers or | 79 | /// type `bool`, a struct like `Vec`, or things like function pointers or |
@@ -815,6 +816,11 @@ impl Ty { | |||
815 | } | 816 | } |
816 | } | 817 | } |
817 | 818 | ||
819 | /// If this is a `dyn Trait`, returns that trait. | ||
820 | pub fn dyn_trait(&self) -> Option<TraitId> { | ||
821 | self.dyn_trait_ref().map(|it| it.trait_) | ||
822 | } | ||
823 | |||
818 | fn builtin_deref(&self) -> Option<Ty> { | 824 | fn builtin_deref(&self) -> Option<Ty> { |
819 | match self { | 825 | match self { |
820 | Ty::Apply(a_ty) => match a_ty.ctor { | 826 | Ty::Apply(a_ty) => match a_ty.ctor { |
@@ -867,18 +873,7 @@ impl Ty { | |||
867 | } | 873 | } |
868 | } | 874 | } |
869 | 875 | ||
870 | /// If this is a `dyn Trait`, returns that trait. | 876 | pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { |
871 | pub fn dyn_trait(&self) -> Option<TraitId> { | ||
872 | match self { | ||
873 | Ty::Dyn(predicates) => predicates.iter().find_map(|pred| match pred { | ||
874 | GenericPredicate::Implemented(tr) => Some(tr.trait_), | ||
875 | _ => None, | ||
876 | }), | ||
877 | _ => None, | ||
878 | } | ||
879 | } | ||
880 | |||
881 | pub fn impl_trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> { | ||
882 | match self { | 877 | match self { |
883 | Ty::Opaque(opaque_ty) => { | 878 | Ty::Opaque(opaque_ty) => { |
884 | let predicates = match opaque_ty.opaque_ty_id { | 879 | let predicates = match opaque_ty.opaque_ty_id { |
@@ -892,25 +887,21 @@ impl Ty { | |||
892 | } | 887 | } |
893 | }; | 888 | }; |
894 | 889 | ||
895 | predicates.and_then(|it| { | 890 | predicates.map(|it| it.value) |
896 | it.value.iter().find_map(|pred| match pred { | ||
897 | GenericPredicate::Implemented(tr) => Some(tr.clone()), | ||
898 | _ => None, | ||
899 | }) | ||
900 | }) | ||
901 | } | 891 | } |
902 | Ty::Placeholder(id) => { | 892 | Ty::Placeholder(id) => { |
903 | let generic_params = db.generic_params(id.parent); | 893 | let generic_params = db.generic_params(id.parent); |
904 | let param_data = &generic_params.types[id.local_id]; | 894 | let param_data = &generic_params.types[id.local_id]; |
905 | match param_data.provenance { | 895 | match param_data.provenance { |
906 | hir_def::generics::TypeParamProvenance::ArgumentImplTrait => db | 896 | hir_def::generics::TypeParamProvenance::ArgumentImplTrait => { |
907 | .generic_predicates_for_param(*id) | 897 | let predicates = db |
908 | .into_iter() | 898 | .generic_predicates_for_param(*id) |
909 | .map(|pred| pred.value.clone()) | 899 | .into_iter() |
910 | .find_map(|pred| match pred { | 900 | .map(|pred| pred.value.clone()) |
911 | GenericPredicate::Implemented(tr) => Some(tr.clone()), | 901 | .collect_vec(); |
912 | _ => None, | 902 | |
913 | }), | 903 | Some(predicates) |
904 | } | ||
914 | _ => None, | 905 | _ => None, |
915 | } | 906 | } |
916 | } | 907 | } |
@@ -926,6 +917,12 @@ impl Ty { | |||
926 | _ => None, | 917 | _ => None, |
927 | } | 918 | } |
928 | } | 919 | } |
920 | Ty::Projection(projection_ty) => { | ||
921 | match projection_ty.associated_ty.lookup(db.upcast()).container { | ||
922 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | ||
923 | _ => None, | ||
924 | } | ||
925 | } | ||
929 | _ => None, | 926 | _ => None, |
930 | } | 927 | } |
931 | } | 928 | } |
@@ -1104,10 +1101,10 @@ pub enum OpaqueTyId { | |||
1104 | 1101 | ||
1105 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 1102 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
1106 | pub struct ReturnTypeImplTraits { | 1103 | pub struct ReturnTypeImplTraits { |
1107 | pub impl_traits: Vec<ReturnTypeImplTrait>, | 1104 | pub(crate) impl_traits: Vec<ReturnTypeImplTrait>, |
1108 | } | 1105 | } |
1109 | 1106 | ||
1110 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 1107 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
1111 | pub struct ReturnTypeImplTrait { | 1108 | pub(crate) struct ReturnTypeImplTrait { |
1112 | pub bounds: Binders<Vec<GenericPredicate>>, | 1109 | pub bounds: Binders<Vec<GenericPredicate>>, |
1113 | } | 1110 | } |