diff options
Diffstat (limited to 'crates/ra_hir_ty/src/lib.rs')
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index a6f56c661..c87ee06ce 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -42,7 +42,6 @@ pub mod expr; | |||
42 | mod tests; | 42 | mod tests; |
43 | #[cfg(test)] | 43 | #[cfg(test)] |
44 | mod test_db; | 44 | mod test_db; |
45 | mod marks; | ||
46 | mod _match; | 45 | mod _match; |
47 | 46 | ||
48 | use std::ops::Deref; | 47 | use std::ops::Deref; |
@@ -427,6 +426,11 @@ impl Substs { | |||
427 | } | 426 | } |
428 | } | 427 | } |
429 | 428 | ||
429 | /// Return an index of a parameter in the generic type parameter list by it's id. | ||
430 | pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> { | ||
431 | generics(db.upcast(), id.parent).param_idx(id) | ||
432 | } | ||
433 | |||
430 | #[derive(Debug, Clone)] | 434 | #[derive(Debug, Clone)] |
431 | pub struct SubstsBuilder { | 435 | pub struct SubstsBuilder { |
432 | vec: Vec<Ty>, | 436 | vec: Vec<Ty>, |
@@ -683,6 +687,12 @@ impl Ty { | |||
683 | pub fn unit() -> Self { | 687 | pub fn unit() -> Self { |
684 | Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) | 688 | Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) |
685 | } | 689 | } |
690 | pub fn fn_ptr(sig: FnSig) -> Self { | ||
691 | Ty::apply( | ||
692 | TypeCtor::FnPtr { num_args: sig.params().len() as u16 }, | ||
693 | Substs(sig.params_and_return), | ||
694 | ) | ||
695 | } | ||
686 | 696 | ||
687 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { | 697 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { |
688 | match self { | 698 | match self { |
@@ -730,6 +740,10 @@ impl Ty { | |||
730 | } | 740 | } |
731 | } | 741 | } |
732 | 742 | ||
743 | pub fn is_never(&self) -> bool { | ||
744 | matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. })) | ||
745 | } | ||
746 | |||
733 | /// If this is a `dyn Trait` type, this returns the `Trait` part. | 747 | /// If this is a `dyn Trait` type, this returns the `Trait` part. |
734 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { | 748 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { |
735 | match self { | 749 | match self { |
@@ -793,15 +807,13 @@ impl Ty { | |||
793 | } | 807 | } |
794 | } | 808 | } |
795 | 809 | ||
796 | /// If this is an `impl Trait` or `dyn Trait`, returns that trait. | 810 | /// If this is a `dyn Trait`, returns that trait. |
797 | pub fn inherent_trait(&self) -> Option<TraitId> { | 811 | pub fn dyn_trait(&self) -> Option<TraitId> { |
798 | match self { | 812 | match self { |
799 | Ty::Dyn(predicates) | Ty::Opaque(predicates) => { | 813 | Ty::Dyn(predicates) => predicates.iter().find_map(|pred| match pred { |
800 | predicates.iter().find_map(|pred| match pred { | 814 | GenericPredicate::Implemented(tr) => Some(tr.trait_), |
801 | GenericPredicate::Implemented(tr) => Some(tr.trait_), | 815 | _ => None, |
802 | _ => None, | 816 | }), |
803 | }) | ||
804 | } | ||
805 | _ => None, | 817 | _ => None, |
806 | } | 818 | } |
807 | } | 819 | } |