args -> params

This commit is contained in:
Florian Diebold 2019-01-12 21:58:16 +01:00
parent 5db5f5cc1d
commit 1ed7fbfc1b
8 changed files with 37 additions and 37 deletions

View file

@ -273,11 +273,11 @@ pub use crate::code_model_impl::function::ScopeEntryWithSyntax;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature { pub struct FnSignature {
pub(crate) name: Name, pub(crate) name: Name,
pub(crate) args: Vec<TypeRef>, pub(crate) params: Vec<TypeRef>,
pub(crate) ret_type: TypeRef, pub(crate) ret_type: TypeRef,
/// True if the first arg is `self`. This is relevant to decide whether this /// True if the first param is `self`. This is relevant to decide whether this
/// can be called as a method. /// can be called as a method.
pub(crate) has_self_arg: bool, pub(crate) has_self_param: bool,
} }
impl FnSignature { impl FnSignature {
@ -285,8 +285,8 @@ impl FnSignature {
&self.name &self.name
} }
pub fn args(&self) -> &[TypeRef] { pub fn params(&self) -> &[TypeRef] {
&self.args &self.params
} }
pub fn ret_type(&self) -> &TypeRef { pub fn ret_type(&self) -> &TypeRef {
@ -295,8 +295,8 @@ impl FnSignature {
/// True if the first arg is `self`. This is relevant to decide whether this /// True if the first arg is `self`. This is relevant to decide whether this
/// can be called as a method. /// can be called as a method.
pub fn has_self_arg(&self) -> bool { pub fn has_self_param(&self) -> bool {
self.has_self_arg self.has_self_param
} }
} }

View file

@ -42,8 +42,8 @@ impl FnSignature {
.name() .name()
.map(|n| n.as_name()) .map(|n| n.as_name())
.unwrap_or_else(Name::missing); .unwrap_or_else(Name::missing);
let mut args = Vec::new(); let mut params = Vec::new();
let mut has_self_arg = false; let mut has_self_param = false;
if let Some(param_list) = node.param_list() { if let Some(param_list) = node.param_list() {
if let Some(self_param) = param_list.self_param() { if let Some(self_param) = param_list.self_param() {
let self_type = if let Some(type_ref) = self_param.type_ref() { let self_type = if let Some(type_ref) = self_param.type_ref() {
@ -60,12 +60,12 @@ impl FnSignature {
} }
} }
}; };
args.push(self_type); params.push(self_type);
has_self_arg = true; has_self_param = true;
} }
for param in param_list.params() { for param in param_list.params() {
let type_ref = TypeRef::from_ast_opt(param.type_ref()); let type_ref = TypeRef::from_ast_opt(param.type_ref());
args.push(type_ref); params.push(type_ref);
} }
} }
let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) {
@ -75,9 +75,9 @@ impl FnSignature {
}; };
let sig = FnSignature { let sig = FnSignature {
name, name,
args, params,
ret_type, ret_type,
has_self_arg, has_self_param,
}; };
Arc::new(sig) Arc::new(sig)
} }

View file

@ -43,7 +43,7 @@ impl FnScopes {
scope_for: FxHashMap::default(), scope_for: FxHashMap::default(),
}; };
let root = scopes.root_scope(); let root = scopes.root_scope();
scopes.add_params_bindings(root, body.args()); scopes.add_params_bindings(root, body.params());
compute_expr_scopes(body.body_expr(), &body, &mut scopes, root); compute_expr_scopes(body.body_expr(), &body, &mut scopes, root);
scopes scopes
} }

View file

@ -18,13 +18,13 @@ impl_arena_id!(ExprId);
pub struct Body { pub struct Body {
exprs: Arena<ExprId, Expr>, exprs: Arena<ExprId, Expr>,
pats: Arena<PatId, Pat>, pats: Arena<PatId, Pat>,
/// The patterns for the function's arguments. While the argument types are /// The patterns for the function's parameters. While the parameter types are
/// part of the function signature, the patterns are not (they don't change /// part of the function signature, the patterns are not (they don't change
/// the external type of the function). /// the external type of the function).
/// ///
/// If this `Body` is for the body of a constant, this will just be /// If this `Body` is for the body of a constant, this will just be
/// empty. /// empty.
args: Vec<PatId>, params: Vec<PatId>,
/// The `ExprId` of the actual body expression. /// The `ExprId` of the actual body expression.
body_expr: ExprId, body_expr: ExprId,
} }
@ -44,8 +44,8 @@ pub struct BodySyntaxMapping {
} }
impl Body { impl Body {
pub fn args(&self) -> &[PatId] { pub fn params(&self) -> &[PatId] {
&self.args &self.params
} }
pub fn body_expr(&self) -> ExprId { pub fn body_expr(&self) -> ExprId {
@ -699,11 +699,11 @@ impl ExprCollector {
} }
} }
fn into_body_syntax_mapping(self, args: Vec<PatId>, body_expr: ExprId) -> BodySyntaxMapping { fn into_body_syntax_mapping(self, params: Vec<PatId>, body_expr: ExprId) -> BodySyntaxMapping {
let body = Body { let body = Body {
exprs: self.exprs, exprs: self.exprs,
pats: self.pats, pats: self.pats,
args, params,
body_expr, body_expr,
}; };
BodySyntaxMapping { BodySyntaxMapping {
@ -719,8 +719,8 @@ impl ExprCollector {
pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping { pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping {
let mut collector = ExprCollector::new(); let mut collector = ExprCollector::new();
let args = if let Some(param_list) = node.param_list() { let params = if let Some(param_list) = node.param_list() {
let mut args = Vec::new(); let mut params = Vec::new();
if let Some(self_param) = param_list.self_param() { if let Some(self_param) = param_list.self_param() {
let self_param = LocalSyntaxPtr::new( let self_param = LocalSyntaxPtr::new(
@ -729,13 +729,13 @@ pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping {
.expect("self param without self keyword") .expect("self param without self keyword")
.syntax(), .syntax(),
); );
let arg = collector.alloc_pat( let param = collector.alloc_pat(
Pat::Bind { Pat::Bind {
name: Name::self_param(), name: Name::self_param(),
}, },
self_param, self_param,
); );
args.push(arg); params.push(param);
} }
for param in param_list.params() { for param in param_list.params() {
@ -744,15 +744,15 @@ pub(crate) fn collect_fn_body_syntax(node: &ast::FnDef) -> BodySyntaxMapping {
} else { } else {
continue; continue;
}; };
args.push(collector.collect_pat(pat)); params.push(collector.collect_pat(pat));
} }
args params
} else { } else {
Vec::new() Vec::new()
}; };
let body = collector.collect_block_opt(node.body()); let body = collector.collect_block_opt(node.body());
collector.into_body_syntax_mapping(args, body) collector.into_body_syntax_mapping(params, body)
} }
pub(crate) fn body_syntax_mapping( pub(crate) fn body_syntax_mapping(

View file

@ -432,7 +432,7 @@ fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable<Ty> {
let impl_block = f.impl_block(db)?; let impl_block = f.impl_block(db)?;
// TODO we ignore type parameters for now // TODO we ignore type parameters for now
let input = signature let input = signature
.args() .params()
.iter() .iter()
.map(|tr| Ty::from_hir(db, &module, impl_block.as_ref(), tr)) .map(|tr| Ty::from_hir(db, &module, impl_block.as_ref(), tr))
.collect::<Cancelable<Vec<_>>>()?; .collect::<Cancelable<Vec<_>>>()?;
@ -876,7 +876,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
} }
Expr::Call { callee, args } => { Expr::Call { callee, args } => {
let callee_ty = self.infer_expr(*callee, &Expectation::none())?; let callee_ty = self.infer_expr(*callee, &Expectation::none())?;
let (arg_tys, ret_ty) = match &callee_ty { let (param_tys, ret_ty) = match &callee_ty {
Ty::FnPtr(sig) => (&sig.input[..], sig.output.clone()), Ty::FnPtr(sig) => (&sig.input[..], sig.output.clone()),
_ => { _ => {
// not callable // not callable
@ -887,7 +887,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
for (i, arg) in args.iter().enumerate() { for (i, arg) in args.iter().enumerate() {
self.infer_expr( self.infer_expr(
*arg, *arg,
&Expectation::has_type(arg_tys.get(i).cloned().unwrap_or(Ty::Unknown)), &Expectation::has_type(param_tys.get(i).cloned().unwrap_or(Ty::Unknown)),
)?; )?;
} }
ret_ty ret_ty
@ -904,7 +904,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
None => Ty::Unknown, None => Ty::Unknown,
}; };
let method_ty = self.insert_type_vars(method_ty); let method_ty = self.insert_type_vars(method_ty);
let (expected_receiver_ty, arg_tys, ret_ty) = match &method_ty { let (expected_receiver_ty, param_tys, ret_ty) = match &method_ty {
Ty::FnPtr(sig) => { Ty::FnPtr(sig) => {
if sig.input.len() > 0 { if sig.input.len() > 0 {
(&sig.input[0], &sig.input[1..], sig.output.clone()) (&sig.input[0], &sig.input[1..], sig.output.clone())
@ -920,7 +920,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
for (i, arg) in args.iter().enumerate() { for (i, arg) in args.iter().enumerate() {
self.infer_expr( self.infer_expr(
*arg, *arg,
&Expectation::has_type(arg_tys.get(i).cloned().unwrap_or(Ty::Unknown)), &Expectation::has_type(param_tys.get(i).cloned().unwrap_or(Ty::Unknown)),
)?; )?;
} }
ret_ty ret_ty
@ -1093,7 +1093,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn collect_fn_signature(&mut self, signature: &FnSignature) -> Cancelable<()> { fn collect_fn_signature(&mut self, signature: &FnSignature) -> Cancelable<()> {
let body = Arc::clone(&self.body); // avoid borrow checker problem let body = Arc::clone(&self.body); // avoid borrow checker problem
for (type_ref, pat) in signature.args().iter().zip(body.args()) { for (type_ref, pat) in signature.params().iter().zip(body.params()) {
let ty = self.make_ty(type_ref)?; let ty = self.make_ty(type_ref)?;
let ty = self.insert_type_vars(ty); let ty = self.insert_type_vars(ty);
self.write_pat_ty(*pat, ty); self.write_pat_ty(*pat, ty);

View file

@ -114,7 +114,7 @@ impl Ty {
pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<DefId>> { pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<DefId>> {
self.iterate_methods(db, |f| { self.iterate_methods(db, |f| {
let sig = f.signature(db); let sig = f.signature(db);
if sig.name() == name && sig.has_self_arg() { if sig.name() == name && sig.has_self_param() {
Ok(Some(f.def_id())) Ok(Some(f.def_id()))
} else { } else {
Ok(None) Ok(None)

View file

@ -63,7 +63,7 @@ fn complete_methods(
) -> Cancelable<()> { ) -> Cancelable<()> {
receiver.iterate_methods(ctx.db, |func| { receiver.iterate_methods(ctx.db, |func| {
let sig = func.signature(ctx.db); let sig = func.signature(ctx.db);
if sig.has_self_arg() { if sig.has_self_param() {
CompletionItem::new(CompletionKind::Reference, sig.name().to_string()) CompletionItem::new(CompletionKind::Reference, sig.name().to_string())
.from_function(ctx, func) .from_function(ctx, func)
.kind(CompletionItemKind::Method) .kind(CompletionItemKind::Method)

View file

@ -191,7 +191,7 @@ impl Builder {
) -> Builder { ) -> Builder {
// If not an import, add parenthesis automatically. // If not an import, add parenthesis automatically.
if ctx.use_item_syntax.is_none() && !ctx.is_call { if ctx.use_item_syntax.is_none() && !ctx.is_call {
if function.signature(ctx.db).args().is_empty() { if function.signature(ctx.db).params().is_empty() {
self.snippet = Some(format!("{}()$0", self.label)); self.snippet = Some(format!("{}()$0", self.label));
} else { } else {
self.snippet = Some(format!("{}($0)", self.label)); self.snippet = Some(format!("{}($0)", self.label));