aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/db.rs5
-rw-r--r--crates/ra_hir/src/ty.rs18
-rw-r--r--crates/ra_hir/src/ty/infer.rs6
-rw-r--r--crates/ra_hir/src/ty/lower.rs18
4 files changed, 26 insertions, 21 deletions
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 21d22aa7f..5ad9547f1 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -12,7 +12,7 @@ use crate::{
12 macros::MacroExpansion, 12 macros::MacroExpansion,
13 module_tree::ModuleTree, 13 module_tree::ModuleTree,
14 nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}}, 14 nameres::{ItemMap, lower::{LoweredModule, ImportSourceMap}},
15 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef}, 15 ty::{InferenceResult, Ty, method_resolution::CrateImplBlocks, TypableDef, CallableDef, FnSig},
16 adt::{StructData, EnumData}, 16 adt::{StructData, EnumData},
17 impl_block::{ModuleImplBlocks, ImplSourceMap}, 17 impl_block::{ModuleImplBlocks, ImplSourceMap},
18 generics::{GenericParams, GenericDef}, 18 generics::{GenericParams, GenericDef},
@@ -105,6 +105,9 @@ pub trait HirDatabase: PersistentHirDatabase {
105 #[salsa::invoke(crate::ty::type_for_field)] 105 #[salsa::invoke(crate::ty::type_for_field)]
106 fn type_for_field(&self, field: StructField) -> Ty; 106 fn type_for_field(&self, field: StructField) -> Ty;
107 107
108 #[salsa::invoke(crate::ty::callable_item_sig)]
109 fn callable_item_signature(&self, def: CallableDef) -> FnSig;
110
108 #[salsa::invoke(crate::expr::body_with_source_map_query)] 111 #[salsa::invoke(crate::expr::body_with_source_map_query)]
109 fn body_with_source_map( 112 fn body_with_source_map(
110 &self, 113 &self,
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 31f726f35..2ea3b341f 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -16,7 +16,7 @@ use std::{fmt, mem};
16 16
17use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase}; 17use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase};
18 18
19pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field}; 19pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field, callable_item_sig};
20pub(crate) use infer::{infer, InferenceResult, InferTy}; 20pub(crate) use infer::{infer, InferenceResult, InferTy};
21use display::{HirDisplay, HirFormatter}; 21use display::{HirDisplay, HirFormatter};
22 22
@@ -77,8 +77,6 @@ pub enum Ty {
77 FnDef { 77 FnDef {
78 /// The definition of the function / constructor. 78 /// The definition of the function / constructor.
79 def: CallableDef, 79 def: CallableDef,
80 /// Parameters and return type
81 sig: FnSig,
82 /// Substitutions for the generic parameters of the type 80 /// Substitutions for the generic parameters of the type
83 substs: Substs, 81 substs: Substs,
84 }, 82 },
@@ -189,11 +187,7 @@ impl Ty {
189 } 187 }
190 sig.ret().walk(f); 188 sig.ret().walk(f);
191 } 189 }
192 Ty::FnDef { substs, sig, .. } => { 190 Ty::FnDef { substs, .. } => {
193 for input in sig.params() {
194 input.walk(f);
195 }
196 sig.ret().walk(f);
197 for t in substs.0.iter() { 191 for t in substs.0.iter() {
198 t.walk(f); 192 t.walk(f);
199 } 193 }
@@ -232,8 +226,7 @@ impl Ty {
232 Ty::FnPtr(sig) => { 226 Ty::FnPtr(sig) => {
233 sig.walk_mut(f); 227 sig.walk_mut(f);
234 } 228 }
235 Ty::FnDef { substs, sig, .. } => { 229 Ty::FnDef { substs, .. } => {
236 sig.walk_mut(f);
237 substs.walk_mut(f); 230 substs.walk_mut(f);
238 } 231 }
239 Ty::Adt { substs, .. } => { 232 Ty::Adt { substs, .. } => {
@@ -275,7 +268,7 @@ impl Ty {
275 pub fn apply_substs(self, substs: Substs) -> Ty { 268 pub fn apply_substs(self, substs: Substs) -> Ty {
276 match self { 269 match self {
277 Ty::Adt { def_id, .. } => Ty::Adt { def_id, substs }, 270 Ty::Adt { def_id, .. } => Ty::Adt { def_id, substs },
278 Ty::FnDef { def, sig, .. } => Ty::FnDef { def, sig, substs }, 271 Ty::FnDef { def, .. } => Ty::FnDef { def, substs },
279 _ => self, 272 _ => self,
280 } 273 }
281 } 274 }
@@ -344,7 +337,8 @@ impl HirDisplay for Ty {
344 f.write_joined(sig.params(), ", ")?; 337 f.write_joined(sig.params(), ", ")?;
345 write!(f, ") -> {}", sig.ret().display(f.db))?; 338 write!(f, ") -> {}", sig.ret().display(f.db))?;
346 } 339 }
347 Ty::FnDef { def, substs, sig, .. } => { 340 Ty::FnDef { def, substs, .. } => {
341 let sig = f.db.callable_item_signature(*def);
348 let name = match def { 342 let name = match def {
349 CallableDef::Function(ff) => ff.name(f.db), 343 CallableDef::Function(ff) => ff.name(f.db),
350 CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing), 344 CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing),
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index 2eb73726e..c9a5bc7a1 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -725,7 +725,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
725 let callee_ty = self.infer_expr(*callee, &Expectation::none()); 725 let callee_ty = self.infer_expr(*callee, &Expectation::none());
726 let (param_tys, ret_ty) = match &callee_ty { 726 let (param_tys, ret_ty) = match &callee_ty {
727 Ty::FnPtr(sig) => (sig.params().to_vec(), sig.ret().clone()), 727 Ty::FnPtr(sig) => (sig.params().to_vec(), sig.ret().clone()),
728 Ty::FnDef { substs, sig, .. } => { 728 Ty::FnDef { substs, def, .. } => {
729 let sig = self.db.callable_item_signature(*def);
729 let ret_ty = sig.ret().clone().subst(&substs); 730 let ret_ty = sig.ret().clone().subst(&substs);
730 let param_tys = 731 let param_tys =
731 sig.params().iter().map(|ty| ty.clone().subst(&substs)).collect(); 732 sig.params().iter().map(|ty| ty.clone().subst(&substs)).collect();
@@ -768,7 +769,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
768 (Ty::Unknown, Vec::new(), sig.ret().clone()) 769 (Ty::Unknown, Vec::new(), sig.ret().clone())
769 } 770 }
770 } 771 }
771 Ty::FnDef { substs, sig, .. } => { 772 Ty::FnDef { substs, def, .. } => {
773 let sig = self.db.callable_item_signature(*def);
772 let ret_ty = sig.ret().clone().subst(&substs); 774 let ret_ty = sig.ret().clone().subst(&substs);
773 775
774 if !sig.params().is_empty() { 776 if !sig.params().is_empty() {
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 7d065203a..278f592d3 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -212,6 +212,15 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
212 } 212 }
213} 213}
214 214
215/// Build the signature of a callable item (function, struct or enum variant).
216pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSig {
217 match def {
218 CallableDef::Function(f) => fn_sig_for_fn(db, f),
219 CallableDef::Struct(s) => fn_sig_for_struct_constructor(db, s),
220 CallableDef::EnumVariant(e) => fn_sig_for_enum_variant_constructor(db, e),
221 }
222}
223
215/// Build the type of a specific field of a struct or enum variant. 224/// Build the type of a specific field of a struct or enum variant.
216pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty { 225pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
217 let parent_def = field.parent_def(db); 226 let parent_def = field.parent_def(db);
@@ -236,10 +245,9 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
236/// Build the declared type of a function. This should not need to look at the 245/// Build the declared type of a function. This should not need to look at the
237/// function body. 246/// function body.
238fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { 247fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
239 let sig = fn_sig_for_fn(db, def);
240 let generics = def.generic_params(db); 248 let generics = def.generic_params(db);
241 let substs = make_substs(&generics); 249 let substs = make_substs(&generics);
242 Ty::FnDef { def: def.into(), sig, substs } 250 Ty::FnDef { def: def.into(), substs }
243} 251}
244 252
245/// Build the declared type of a const. 253/// Build the declared type of a const.
@@ -279,10 +287,9 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
279 if var_data.fields().is_none() { 287 if var_data.fields().is_none() {
280 return type_for_struct(db, def); // Unit struct 288 return type_for_struct(db, def); // Unit struct
281 } 289 }
282 let sig = fn_sig_for_struct_constructor(db, def);
283 let generics = def.generic_params(db); 290 let generics = def.generic_params(db);
284 let substs = make_substs(&generics); 291 let substs = make_substs(&generics);
285 Ty::FnDef { def: def.into(), sig, substs } 292 Ty::FnDef { def: def.into(), substs }
286} 293}
287 294
288fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig { 295fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) -> FnSig {
@@ -308,10 +315,9 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) ->
308 if var_data.fields().is_none() { 315 if var_data.fields().is_none() {
309 return type_for_enum(db, def.parent_enum(db)); // Unit variant 316 return type_for_enum(db, def.parent_enum(db)); // Unit variant
310 } 317 }
311 let sig = fn_sig_for_enum_variant_constructor(db, def);
312 let generics = def.parent_enum(db).generic_params(db); 318 let generics = def.parent_enum(db).generic_params(db);
313 let substs = make_substs(&generics); 319 let substs = make_substs(&generics);
314 Ty::FnDef { def: def.into(), sig, substs } 320 Ty::FnDef { def: def.into(), substs }
315} 321}
316 322
317fn make_substs(generics: &GenericParams) -> Substs { 323fn make_substs(generics: &GenericParams) -> Substs {