Address some issues flagged in review

This commit is contained in:
Marcus Klaas de Vries 2019-01-26 12:06:41 +01:00
parent 04748a0f16
commit 089b1c57c1
4 changed files with 26 additions and 40 deletions

View file

@ -388,8 +388,7 @@ pub use crate::code_model_impl::function::ScopeEntryWithSyntax;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature {
pub(crate) name: Name,
pub(crate) type_params: Arc<GenericParams>,
pub(crate) args: Vec<TypeRef>,
pub(crate) params: Vec<TypeRef>,
pub(crate) ret_type: TypeRef,
/// True if the first param is `self`. This is relevant to decide whether this
/// can be called as a method.
@ -401,8 +400,8 @@ impl FnSignature {
&self.name
}
pub fn args(&self) -> &[TypeRef] {
&self.args
pub fn params(&self) -> &[TypeRef] {
&self.params
}
pub fn ret_type(&self) -> &TypeRef {
@ -414,10 +413,6 @@ impl FnSignature {
pub fn has_self_param(&self) -> bool {
self.has_self_param
}
pub fn generics(&self) -> &GenericParams {
&self.type_params
}
}
impl Function {

View file

@ -32,7 +32,7 @@ impl FnSignature {
.name()
.map(|n| n.as_name())
.unwrap_or_else(Name::missing);
let mut args = Vec::new();
let mut params = Vec::new();
let mut has_self_param = false;
if let Some(param_list) = node.param_list() {
if let Some(self_param) = param_list.self_param() {
@ -50,15 +50,14 @@ impl FnSignature {
}
}
};
args.push(self_type);
params.push(self_type);
has_self_param = true;
}
for param in param_list.params() {
let type_ref = TypeRef::from_ast_opt(param.type_ref());
args.push(type_ref);
params.push(type_ref);
}
}
let type_params = db.generic_params(func.into());
let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) {
TypeRef::from_ast(type_ref)
} else {
@ -67,8 +66,7 @@ impl FnSignature {
let sig = FnSignature {
name,
type_params,
args,
params,
ret_type,
has_self_param,
};

View file

@ -34,7 +34,7 @@ use test_utils::tested_by;
use crate::{
Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock,
FnSignature, FnScopes, ModuleDef, AdtDef,
FnScopes, ModuleDef, AdtDef,
db::HirDatabase,
type_ref::{TypeRef, Mutability},
name::KnownName,
@ -1164,13 +1164,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let ty = self.insert_type_vars(ty.apply_substs(substs));
(ty, Some(var.into()))
}
TypableDef::Function(func) => {
let ty = type_for_fn(self.db, func);
let ty = self.insert_type_vars(ty.apply_substs(substs));
// FIXME: is this right?
(ty, None)
}
TypableDef::Enum(_) => (Ty::Unknown, None),
TypableDef::Function(_) | TypableDef::Enum(_) => (Ty::Unknown, None),
}
}
@ -1363,15 +1357,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
Ty::FnPtr(sig) => (sig.input.clone(), sig.output.clone()),
Ty::FnDef { def, substs, .. } => {
let fn_sig = def.signature(self.db);
let generic_params = def.generic_params(self.db);
let ret_ty = self
.make_ty(fn_sig.ret_type(), fn_sig.generics())
.make_ty(fn_sig.ret_type(), &generic_params)
.subst(&substs);
let param_tys = fn_sig
.args()
.params()
.iter()
.map(|type_ref| {
self.make_ty(type_ref, fn_sig.generics()).subst(&substs)
})
.map(|type_ref| self.make_ty(type_ref, &generic_params).subst(&substs))
.collect();
(param_tys, ret_ty)
}
@ -1416,13 +1409,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
Ty::FnDef { def, substs, .. } => {
let fn_sig = def.signature(self.db);
let generic_params = def.generic_params(self.db);
let ret_ty = self
.make_ty(fn_sig.ret_type(), fn_sig.generics())
.make_ty(fn_sig.ret_type(), &generic_params)
.subst(&substs);
if fn_sig.args().len() > 0 {
let mut arg_iter = fn_sig.args().iter().map(|type_ref| {
self.make_ty(type_ref, fn_sig.generics()).subst(&substs)
if fn_sig.params().len() > 0 {
let mut arg_iter = fn_sig.params().iter().map(|type_ref| {
self.make_ty(type_ref, &generic_params).subst(&substs)
});
let receiver_ty = arg_iter.next().unwrap();
(receiver_ty, arg_iter.collect(), ret_ty)
@ -1660,15 +1654,16 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
ty
}
fn collect_fn_signature(&mut self, signature: &FnSignature) {
fn collect_fn_signature(&mut self, func: Function) {
let body = Arc::clone(&self.body); // avoid borrow checker problem
let generics = signature.generics();
for (type_ref, pat) in signature.args().iter().zip(body.params()) {
let ty = self.make_ty(type_ref, generics);
let signature = func.signature(self.db);
let generics = func.generic_params(self.db);
for (type_ref, pat) in signature.params().iter().zip(body.params()) {
let ty = self.make_ty(type_ref, &generics);
self.infer_pat(*pat, &ty);
}
self.return_ty = self.make_ty(signature.ret_type(), generics);
self.return_ty = self.make_ty(signature.ret_type(), &generics);
}
fn infer_body(&mut self) {
@ -1687,9 +1682,7 @@ pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
let impl_block = func.impl_block(db);
let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block);
let signature = func.signature(db);
ctx.collect_fn_signature(&signature);
ctx.collect_fn_signature(func);
ctx.infer_body();
Arc::new(ctx.resolve_all())

View file

@ -240,7 +240,7 @@ impl Builder {
if ctx.use_item_syntax.is_none() && !ctx.is_call {
tested_by!(inserts_parens_for_function_calls);
let sig = function.signature(ctx.db);
if sig.args().is_empty() || sig.has_self_param() && sig.args().len() == 1 {
if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 {
self.insert_text = Some(format!("{}()$0", self.label));
} else {
self.insert_text = Some(format!("{}($0)", self.label));