aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/lib.rs')
-rw-r--r--crates/ra_hir_ty/src/lib.rs56
1 files changed, 44 insertions, 12 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index a6f56c661..9fa8d3bdc 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -42,7 +42,6 @@ pub mod expr;
42mod tests; 42mod tests;
43#[cfg(test)] 43#[cfg(test)]
44mod test_db; 44mod test_db;
45mod marks;
46mod _match; 45mod _match;
47 46
48use std::ops::Deref; 47use std::ops::Deref;
@@ -50,8 +49,10 @@ use std::sync::Arc;
50use std::{iter, mem}; 49use std::{iter, mem};
51 50
52use hir_def::{ 51use hir_def::{
53 expr::ExprId, type_ref::Mutability, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, 52 expr::ExprId,
54 HasModule, Lookup, TraitId, TypeAliasId, TypeParamId, 53 type_ref::{Mutability, Rawness},
54 AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
55 TypeParamId,
55}; 56};
56use ra_db::{impl_intern_key, salsa, CrateId}; 57use ra_db::{impl_intern_key, salsa, CrateId};
57 58
@@ -156,10 +157,16 @@ pub enum TypeCtor {
156/// This exists just for Chalk, because Chalk just has a single `StructId` where 157/// This exists just for Chalk, because Chalk just has a single `StructId` where
157/// we have different kinds of ADTs, primitive types and special type 158/// we have different kinds of ADTs, primitive types and special type
158/// constructors like tuples and function pointers. 159/// constructors like tuples and function pointers.
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 160#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
160pub struct TypeCtorId(salsa::InternId); 161pub struct TypeCtorId(salsa::InternId);
161impl_intern_key!(TypeCtorId); 162impl_intern_key!(TypeCtorId);
162 163
164/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
165/// we have different IDs for struct and enum variant constructors.
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
167pub struct CallableDefId(salsa::InternId);
168impl_intern_key!(CallableDefId);
169
163impl TypeCtor { 170impl TypeCtor {
164 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize { 171 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
165 match self { 172 match self {
@@ -427,6 +434,11 @@ impl Substs {
427 } 434 }
428} 435}
429 436
437/// Return an index of a parameter in the generic type parameter list by it's id.
438pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
439 generics(db.upcast(), id.parent).param_idx(id)
440}
441
430#[derive(Debug, Clone)] 442#[derive(Debug, Clone)]
431pub struct SubstsBuilder { 443pub struct SubstsBuilder {
432 vec: Vec<Ty>, 444 vec: Vec<Ty>,
@@ -683,6 +695,12 @@ impl Ty {
683 pub fn unit() -> Self { 695 pub fn unit() -> Self {
684 Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) 696 Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty())
685 } 697 }
698 pub fn fn_ptr(sig: FnSig) -> Self {
699 Ty::apply(
700 TypeCtor::FnPtr { num_args: sig.params().len() as u16 },
701 Substs(sig.params_and_return),
702 )
703 }
686 704
687 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 705 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
688 match self { 706 match self {
@@ -693,6 +711,18 @@ impl Ty {
693 } 711 }
694 } 712 }
695 713
714 pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
715 match self {
716 Ty::Apply(ApplicationTy { ctor: TypeCtor::Ref(mutability), parameters }) => {
717 Some((parameters.as_single(), Rawness::Ref, *mutability))
718 }
719 Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(mutability), parameters }) => {
720 Some((parameters.as_single(), Rawness::RawPtr, *mutability))
721 }
722 _ => None,
723 }
724 }
725
696 pub fn strip_references(&self) -> &Ty { 726 pub fn strip_references(&self) -> &Ty {
697 let mut t: &Ty = self; 727 let mut t: &Ty = self;
698 728
@@ -730,6 +760,10 @@ impl Ty {
730 } 760 }
731 } 761 }
732 762
763 pub fn is_never(&self) -> bool {
764 matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }))
765 }
766
733 /// If this is a `dyn Trait` type, this returns the `Trait` part. 767 /// If this is a `dyn Trait` type, this returns the `Trait` part.
734 pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { 768 pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
735 match self { 769 match self {
@@ -793,15 +827,13 @@ impl Ty {
793 } 827 }
794 } 828 }
795 829
796 /// If this is an `impl Trait` or `dyn Trait`, returns that trait. 830 /// If this is a `dyn Trait`, returns that trait.
797 pub fn inherent_trait(&self) -> Option<TraitId> { 831 pub fn dyn_trait(&self) -> Option<TraitId> {
798 match self { 832 match self {
799 Ty::Dyn(predicates) | Ty::Opaque(predicates) => { 833 Ty::Dyn(predicates) => predicates.iter().find_map(|pred| match pred {
800 predicates.iter().find_map(|pred| match pred { 834 GenericPredicate::Implemented(tr) => Some(tr.trait_),
801 GenericPredicate::Implemented(tr) => Some(tr.trait_), 835 _ => None,
802 _ => None, 836 }),
803 })
804 }
805 _ => None, 837 _ => None,
806 } 838 }
807 } 839 }