checker: minor optimization in fn_call() and method_call() (#14503)

yuyi 2022-05-23 13:00:57 +08:00 committed by Jef Roosens
parent bf15ed95af
commit 1761b81130
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 11 additions and 12 deletions

View File

@ -786,12 +786,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
c.warn('`error($arg)` can be shortened to just `$arg`', node.pos) c.warn('`error($arg)` can be shortened to just `$arg`', node.pos)
} }
} }
// TODO: typ optimize.. this node can get processed more than once c.set_node_expected_arg_types(mut node, func)
if node.expected_arg_types.len == 0 {
for param in func.params {
node.expected_arg_types << param.typ
}
}
if !c.pref.backend.is_js() && node.args.len > 0 && func.params.len == 0 { if !c.pref.backend.is_js() && node.args.len > 0 && func.params.len == 0 {
c.error('too many arguments in call to `$func.name` (non-js backend: $c.pref.backend)', c.error('too many arguments in call to `$func.name` (non-js backend: $c.pref.backend)',
node.pos) node.pos)
@ -1472,12 +1467,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
if !c.table.cur_fn.is_deprecated && method.is_deprecated { if !c.table.cur_fn.is_deprecated && method.is_deprecated {
c.deprecate_fnmethod('method', '${left_sym.name}.$method.name', method, node) c.deprecate_fnmethod('method', '${left_sym.name}.$method.name', method, node)
} }
// TODO: typ optimize.. this node can get processed more than once c.set_node_expected_arg_types(mut node, method)
if node.expected_arg_types.len == 0 {
for i in 1 .. method.params.len {
node.expected_arg_types << method.params[i].typ
}
}
if is_method_from_embed { if is_method_from_embed {
node.receiver_type = node.from_embed_types.last().derive(method.params[0].typ) node.receiver_type = node.from_embed_types.last().derive(method.params[0].typ)
} else if is_generic { } else if is_generic {
@ -1615,6 +1605,15 @@ fn (mut c Checker) go_expr(mut node ast.GoExpr) ast.Type {
} }
} }
fn (mut c Checker) set_node_expected_arg_types(mut node ast.CallExpr, func &ast.Fn) {
if node.expected_arg_types.len == 0 {
start_idx := if func.is_method { 1 } else { 0 }
for i in start_idx .. func.params.len {
node.expected_arg_types << func.params[i].typ
}
}
}
fn (mut c Checker) deprecate_fnmethod(kind string, name string, the_fn ast.Fn, node ast.CallExpr) { fn (mut c Checker) deprecate_fnmethod(kind string, name string, the_fn ast.Fn, node ast.CallExpr) {
mut deprecation_message := '' mut deprecation_message := ''
now := time.now() now := time.now()