aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model_api.rs11
-rw-r--r--crates/ra_hir/src/code_model_impl/function.rs10
-rw-r--r--crates/ra_hir/src/ty.rs43
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs2
4 files changed, 26 insertions, 40 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index a13da58d2..e4008058c 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -388,8 +388,7 @@ pub use crate::code_model_impl::function::ScopeEntryWithSyntax;
388#[derive(Debug, Clone, PartialEq, Eq)] 388#[derive(Debug, Clone, PartialEq, Eq)]
389pub struct FnSignature { 389pub struct FnSignature {
390 pub(crate) name: Name, 390 pub(crate) name: Name,
391 pub(crate) type_params: Arc<GenericParams>, 391 pub(crate) params: Vec<TypeRef>,
392 pub(crate) args: Vec<TypeRef>,
393 pub(crate) ret_type: TypeRef, 392 pub(crate) ret_type: TypeRef,
394 /// True if the first param is `self`. This is relevant to decide whether this 393 /// True if the first param is `self`. This is relevant to decide whether this
395 /// can be called as a method. 394 /// can be called as a method.
@@ -401,8 +400,8 @@ impl FnSignature {
401 &self.name 400 &self.name
402 } 401 }
403 402
404 pub fn args(&self) -> &[TypeRef] { 403 pub fn params(&self) -> &[TypeRef] {
405 &self.args 404 &self.params
406 } 405 }
407 406
408 pub fn ret_type(&self) -> &TypeRef { 407 pub fn ret_type(&self) -> &TypeRef {
@@ -414,10 +413,6 @@ impl FnSignature {
414 pub fn has_self_param(&self) -> bool { 413 pub fn has_self_param(&self) -> bool {
415 self.has_self_param 414 self.has_self_param
416 } 415 }
417
418 pub fn generics(&self) -> &GenericParams {
419 &self.type_params
420 }
421} 416}
422 417
423impl Function { 418impl Function {
diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs
index f4beab6ae..e0dd4d629 100644
--- a/crates/ra_hir/src/code_model_impl/function.rs
+++ b/crates/ra_hir/src/code_model_impl/function.rs
@@ -32,7 +32,7 @@ impl FnSignature {
32 .name() 32 .name()
33 .map(|n| n.as_name()) 33 .map(|n| n.as_name())
34 .unwrap_or_else(Name::missing); 34 .unwrap_or_else(Name::missing);
35 let mut args = Vec::new(); 35 let mut params = Vec::new();
36 let mut has_self_param = false; 36 let mut has_self_param = false;
37 if let Some(param_list) = node.param_list() { 37 if let Some(param_list) = node.param_list() {
38 if let Some(self_param) = param_list.self_param() { 38 if let Some(self_param) = param_list.self_param() {
@@ -50,15 +50,14 @@ impl FnSignature {
50 } 50 }
51 } 51 }
52 }; 52 };
53 args.push(self_type); 53 params.push(self_type);
54 has_self_param = true; 54 has_self_param = true;
55 } 55 }
56 for param in param_list.params() { 56 for param in param_list.params() {
57 let type_ref = TypeRef::from_ast_opt(param.type_ref()); 57 let type_ref = TypeRef::from_ast_opt(param.type_ref());
58 args.push(type_ref); 58 params.push(type_ref);
59 } 59 }
60 } 60 }
61 let type_params = db.generic_params(func.into());
62 let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { 61 let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) {
63 TypeRef::from_ast(type_ref) 62 TypeRef::from_ast(type_ref)
64 } else { 63 } else {
@@ -67,8 +66,7 @@ impl FnSignature {
67 66
68 let sig = FnSignature { 67 let sig = FnSignature {
69 name, 68 name,
70 type_params, 69 params,
71 args,
72 ret_type, 70 ret_type,
73 has_self_param, 71 has_self_param,
74 }; 72 };
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 43181ffc9..3cf3eaded 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -34,7 +34,7 @@ use test_utils::tested_by;
34 34
35use crate::{ 35use crate::{
36 Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock, 36 Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock,
37 FnSignature, FnScopes, ModuleDef, AdtDef, 37 FnScopes, ModuleDef, AdtDef,
38 db::HirDatabase, 38 db::HirDatabase,
39 type_ref::{TypeRef, Mutability}, 39 type_ref::{TypeRef, Mutability},
40 name::KnownName, 40 name::KnownName,
@@ -1164,13 +1164,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1164 let ty = self.insert_type_vars(ty.apply_substs(substs)); 1164 let ty = self.insert_type_vars(ty.apply_substs(substs));
1165 (ty, Some(var.into())) 1165 (ty, Some(var.into()))
1166 } 1166 }
1167 TypableDef::Function(func) => { 1167 TypableDef::Function(_) | TypableDef::Enum(_) => (Ty::Unknown, None),
1168 let ty = type_for_fn(self.db, func);
1169 let ty = self.insert_type_vars(ty.apply_substs(substs));
1170 // FIXME: is this right?
1171 (ty, None)
1172 }
1173 TypableDef::Enum(_) => (Ty::Unknown, None),
1174 } 1168 }
1175 } 1169 }
1176 1170
@@ -1363,15 +1357,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1363 Ty::FnPtr(sig) => (sig.input.clone(), sig.output.clone()), 1357 Ty::FnPtr(sig) => (sig.input.clone(), sig.output.clone()),
1364 Ty::FnDef { def, substs, .. } => { 1358 Ty::FnDef { def, substs, .. } => {
1365 let fn_sig = def.signature(self.db); 1359 let fn_sig = def.signature(self.db);
1360 let generic_params = def.generic_params(self.db);
1366 let ret_ty = self 1361 let ret_ty = self
1367 .make_ty(fn_sig.ret_type(), fn_sig.generics()) 1362 .make_ty(fn_sig.ret_type(), &generic_params)
1368 .subst(&substs); 1363 .subst(&substs);
1369 let param_tys = fn_sig 1364 let param_tys = fn_sig
1370 .args() 1365 .params()
1371 .iter() 1366 .iter()
1372 .map(|type_ref| { 1367 .map(|type_ref| self.make_ty(type_ref, &generic_params).subst(&substs))
1373 self.make_ty(type_ref, fn_sig.generics()).subst(&substs)
1374 })
1375 .collect(); 1368 .collect();
1376 (param_tys, ret_ty) 1369 (param_tys, ret_ty)
1377 } 1370 }
@@ -1416,13 +1409,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1416 } 1409 }
1417 Ty::FnDef { def, substs, .. } => { 1410 Ty::FnDef { def, substs, .. } => {
1418 let fn_sig = def.signature(self.db); 1411 let fn_sig = def.signature(self.db);
1412 let generic_params = def.generic_params(self.db);
1419 let ret_ty = self 1413 let ret_ty = self
1420 .make_ty(fn_sig.ret_type(), fn_sig.generics()) 1414 .make_ty(fn_sig.ret_type(), &generic_params)
1421 .subst(&substs); 1415 .subst(&substs);
1422 1416
1423 if fn_sig.args().len() > 0 { 1417 if fn_sig.params().len() > 0 {
1424 let mut arg_iter = fn_sig.args().iter().map(|type_ref| { 1418 let mut arg_iter = fn_sig.params().iter().map(|type_ref| {
1425 self.make_ty(type_ref, fn_sig.generics()).subst(&substs) 1419 self.make_ty(type_ref, &generic_params).subst(&substs)
1426 }); 1420 });
1427 let receiver_ty = arg_iter.next().unwrap(); 1421 let receiver_ty = arg_iter.next().unwrap();
1428 (receiver_ty, arg_iter.collect(), ret_ty) 1422 (receiver_ty, arg_iter.collect(), ret_ty)
@@ -1660,15 +1654,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1660 ty 1654 ty
1661 } 1655 }
1662 1656
1663 fn collect_fn_signature(&mut self, signature: &FnSignature) { 1657 fn collect_fn_signature(&mut self, func: Function) {
1664 let body = Arc::clone(&self.body); // avoid borrow checker problem 1658 let body = Arc::clone(&self.body); // avoid borrow checker problem
1665 let generics = signature.generics(); 1659 let signature = func.signature(self.db);
1666 for (type_ref, pat) in signature.args().iter().zip(body.params()) { 1660 let generics = func.generic_params(self.db);
1667 let ty = self.make_ty(type_ref, generics); 1661 for (type_ref, pat) in signature.params().iter().zip(body.params()) {
1662 let ty = self.make_ty(type_ref, &generics);
1668 1663
1669 self.infer_pat(*pat, &ty); 1664 self.infer_pat(*pat, &ty);
1670 } 1665 }
1671 self.return_ty = self.make_ty(signature.ret_type(), generics); 1666 self.return_ty = self.make_ty(signature.ret_type(), &generics);
1672 } 1667 }
1673 1668
1674 fn infer_body(&mut self) { 1669 fn infer_body(&mut self) {
@@ -1687,9 +1682,7 @@ pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
1687 let impl_block = func.impl_block(db); 1682 let impl_block = func.impl_block(db);
1688 let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block); 1683 let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block);
1689 1684
1690 let signature = func.signature(db); 1685 ctx.collect_fn_signature(func);
1691 ctx.collect_fn_signature(&signature);
1692
1693 ctx.infer_body(); 1686 ctx.infer_body();
1694 1687
1695 Arc::new(ctx.resolve_all()) 1688 Arc::new(ctx.resolve_all())
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index 6e9a68e40..b16ac2b28 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -240,7 +240,7 @@ impl Builder {
240 if ctx.use_item_syntax.is_none() && !ctx.is_call { 240 if ctx.use_item_syntax.is_none() && !ctx.is_call {
241 tested_by!(inserts_parens_for_function_calls); 241 tested_by!(inserts_parens_for_function_calls);
242 let sig = function.signature(ctx.db); 242 let sig = function.signature(ctx.db);
243 if sig.args().is_empty() || sig.has_self_param() && sig.args().len() == 1 { 243 if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
244 self.insert_text = Some(format!("{}()$0", self.label)); 244 self.insert_text = Some(format!("{}()$0", self.label));
245 } else { 245 } else {
246 self.insert_text = Some(format!("{}($0)", self.label)); 246 self.insert_text = Some(format!("{}($0)", self.label));