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.rs85
1 files changed, 22 insertions, 63 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index 7f3f5e771..7698cb0d4 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -6,25 +6,6 @@ macro_rules! eprintln {
6 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; 6 ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
7} 7}
8 8
9macro_rules! impl_froms {
10 ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => {
11 $(
12 impl From<$v> for $e {
13 fn from(it: $v) -> $e {
14 $e::$v(it)
15 }
16 }
17 $($(
18 impl From<$sv> for $e {
19 fn from(it: $sv) -> $e {
20 $e::$v($v::$sv(it))
21 }
22 }
23 )*)?
24 )*
25 }
26}
27
28mod autoderef; 9mod autoderef;
29pub mod primitive; 10pub mod primitive;
30pub mod traits; 11pub mod traits;
@@ -32,22 +13,18 @@ pub mod method_resolution;
32mod op; 13mod op;
33mod lower; 14mod lower;
34pub(crate) mod infer; 15pub(crate) mod infer;
35pub mod display;
36pub(crate) mod utils; 16pub(crate) mod utils;
17
18pub mod display;
37pub mod db; 19pub mod db;
38pub mod diagnostics; 20pub mod diagnostics;
39pub mod expr;
40pub mod unsafe_validation;
41 21
42#[cfg(test)] 22#[cfg(test)]
43mod tests; 23mod tests;
44#[cfg(test)] 24#[cfg(test)]
45mod test_db; 25mod test_db;
46mod _match;
47 26
48use std::ops::Deref; 27use std::{iter, mem, ops::Deref, sync::Arc};
49use std::sync::Arc;
50use std::{iter, mem};
51 28
52use hir_def::{ 29use hir_def::{
53 expr::ExprId, 30 expr::ExprId,
@@ -55,18 +32,19 @@ use hir_def::{
55 AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, 32 AdtId, AssocContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId,
56 TypeParamId, 33 TypeParamId,
57}; 34};
58use ra_db::{impl_intern_key, salsa, CrateId}; 35use itertools::Itertools;
36use ra_db::{salsa, CrateId};
59 37
60use crate::{ 38use crate::{
61 db::HirDatabase, 39 db::HirDatabase,
40 display::HirDisplay,
62 primitive::{FloatTy, IntTy}, 41 primitive::{FloatTy, IntTy},
63 utils::{generics, make_mut_slice, Generics}, 42 utils::{generics, make_mut_slice, Generics},
64}; 43};
65use display::HirDisplay;
66 44
67pub use autoderef::autoderef; 45pub use autoderef::autoderef;
68pub use infer::{InferTy, InferenceResult}; 46pub use infer::{InferTy, InferenceResult};
69pub use lower::CallableDef; 47pub use lower::CallableDefId;
70pub use lower::{ 48pub use lower::{
71 associated_type_shorthand_candidates, callable_item_sig, ImplTraitLoweringMode, TyDefId, 49 associated_type_shorthand_candidates, callable_item_sig, ImplTraitLoweringMode, TyDefId,
72 TyLoweringContext, ValueTyDefId, 50 TyLoweringContext, ValueTyDefId,
@@ -74,7 +52,6 @@ pub use lower::{
74pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment}; 52pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
75 53
76pub use chalk_ir::{BoundVar, DebruijnIndex}; 54pub use chalk_ir::{BoundVar, DebruijnIndex};
77use itertools::Itertools;
78 55
79/// A type constructor or type name: this might be something like the primitive 56/// A type constructor or type name: this might be something like the primitive
80/// type `bool`, a struct like `Vec`, or things like function pointers or 57/// type `bool`, a struct like `Vec`, or things like function pointers or
@@ -125,7 +102,7 @@ pub enum TypeCtor {
125 /// fn foo() -> i32 { 1 } 102 /// fn foo() -> i32 { 1 }
126 /// let bar = foo; // bar: fn() -> i32 {foo} 103 /// let bar = foo; // bar: fn() -> i32 {foo}
127 /// ``` 104 /// ```
128 FnDef(CallableDef), 105 FnDef(CallableDefId),
129 106
130 /// A pointer to a function. Written as `fn() -> i32`. 107 /// A pointer to a function. Written as `fn() -> i32`.
131 /// 108 ///
@@ -135,7 +112,8 @@ pub enum TypeCtor {
135 /// fn foo() -> i32 { 1 } 112 /// fn foo() -> i32 { 1 }
136 /// let bar: fn() -> i32 = foo; 113 /// let bar: fn() -> i32 = foo;
137 /// ``` 114 /// ```
138 FnPtr { num_args: u16 }, 115 // FIXME make this a Ty variant like in Chalk
116 FnPtr { num_args: u16, is_varargs: bool },
139 117
140 /// The never type `!`. 118 /// The never type `!`.
141 Never, 119 Never,
@@ -162,19 +140,6 @@ pub enum TypeCtor {
162 Closure { def: DefWithBodyId, expr: ExprId }, 140 Closure { def: DefWithBodyId, expr: ExprId },
163} 141}
164 142
165/// This exists just for Chalk, because Chalk just has a single `StructId` where
166/// we have different kinds of ADTs, primitive types and special type
167/// constructors like tuples and function pointers.
168#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
169pub struct TypeCtorId(salsa::InternId);
170impl_intern_key!(TypeCtorId);
171
172/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
173/// we have different IDs for struct and enum variant constructors.
174#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
175pub struct CallableDefId(salsa::InternId);
176impl_intern_key!(CallableDefId);
177
178impl TypeCtor { 143impl TypeCtor {
179 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize { 144 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
180 match self { 145 match self {
@@ -210,7 +175,7 @@ impl TypeCtor {
210 } 175 }
211 } 176 }
212 } 177 }
213 TypeCtor::FnPtr { num_args } => num_args as usize + 1, 178 TypeCtor::FnPtr { num_args, is_varargs: _ } => num_args as usize + 1,
214 TypeCtor::Tuple { cardinality } => cardinality as usize, 179 TypeCtor::Tuple { cardinality } => cardinality as usize,
215 } 180 }
216 } 181 }
@@ -690,19 +655,20 @@ pub enum TyKind {
690#[derive(Clone, PartialEq, Eq, Debug)] 655#[derive(Clone, PartialEq, Eq, Debug)]
691pub struct FnSig { 656pub struct FnSig {
692 params_and_return: Arc<[Ty]>, 657 params_and_return: Arc<[Ty]>,
658 is_varargs: bool,
693} 659}
694 660
695/// A polymorphic function signature. 661/// A polymorphic function signature.
696pub type PolyFnSig = Binders<FnSig>; 662pub type PolyFnSig = Binders<FnSig>;
697 663
698impl FnSig { 664impl FnSig {
699 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty) -> FnSig { 665 pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> FnSig {
700 params.push(ret); 666 params.push(ret);
701 FnSig { params_and_return: params.into() } 667 FnSig { params_and_return: params.into(), is_varargs }
702 } 668 }
703 669
704 pub fn from_fn_ptr_substs(substs: &Substs) -> FnSig { 670 pub fn from_fn_ptr_substs(substs: &Substs, is_varargs: bool) -> FnSig {
705 FnSig { params_and_return: Arc::clone(&substs.0) } 671 FnSig { params_and_return: Arc::clone(&substs.0), is_varargs }
706 } 672 }
707 673
708 pub fn params(&self) -> &[Ty] { 674 pub fn params(&self) -> &[Ty] {
@@ -747,7 +713,7 @@ impl Ty {
747 } 713 }
748 pub fn fn_ptr(sig: FnSig) -> Self { 714 pub fn fn_ptr(sig: FnSig) -> Self {
749 Ty::apply( 715 Ty::apply(
750 TypeCtor::FnPtr { num_args: sig.params().len() as u16 }, 716 TypeCtor::FnPtr { num_args: sig.params().len() as u16, is_varargs: sig.is_varargs },
751 Substs(sig.params_and_return), 717 Substs(sig.params_and_return),
752 ) 718 )
753 } 719 }
@@ -801,15 +767,6 @@ impl Ty {
801 } 767 }
802 } 768 }
803 769
804 pub fn as_callable(&self) -> Option<(CallableDef, &Substs)> {
805 match self {
806 Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(callable_def), parameters }) => {
807 Some((*callable_def, parameters))
808 }
809 _ => None,
810 }
811 }
812
813 pub fn is_never(&self) -> bool { 770 pub fn is_never(&self) -> bool {
814 matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. })) 771 matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }))
815 } 772 }
@@ -841,10 +798,12 @@ impl Ty {
841 } 798 }
842 } 799 }
843 800
844 fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> { 801 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<FnSig> {
845 match self { 802 match self {
846 Ty::Apply(a_ty) => match a_ty.ctor { 803 Ty::Apply(a_ty) => match a_ty.ctor {
847 TypeCtor::FnPtr { .. } => Some(FnSig::from_fn_ptr_substs(&a_ty.parameters)), 804 TypeCtor::FnPtr { is_varargs, .. } => {
805 Some(FnSig::from_fn_ptr_substs(&a_ty.parameters, is_varargs))
806 }
848 TypeCtor::FnDef(def) => { 807 TypeCtor::FnDef(def) => {
849 let sig = db.callable_item_signature(def); 808 let sig = db.callable_item_signature(def);
850 Some(sig.subst(&a_ty.parameters)) 809 Some(sig.subst(&a_ty.parameters))
@@ -891,7 +850,7 @@ impl Ty {
891 let data = (*it) 850 let data = (*it)
892 .as_ref() 851 .as_ref()
893 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); 852 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
894 data.clone().subst(&opaque_ty.parameters) 853 data.subst(&opaque_ty.parameters)
895 }) 854 })
896 } 855 }
897 }; 856 };