checker: minor cleanup in method_call() (#14342)

master
yuyi 2022-05-09 23:01:44 +08:00 committed by GitHub
parent 79f8a3c796
commit 940c78bdfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 21 deletions

View File

@ -1153,11 +1153,9 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) { if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) {
c.error('cannot $method_name `$arg_sym.name` to `$left_sym.name`', arg_expr.pos()) c.error('cannot $method_name `$arg_sym.name` to `$left_sym.name`', arg_expr.pos())
} }
} else if final_left_sym.kind == .array && method_name in ['first', 'last', 'pop'] { } else if final_left_sym.info is ast.Array && method_name in ['first', 'last', 'pop'] {
if final_left_sym.info is ast.Array { node.return_type = final_left_sym.info.elem_type
node.return_type = final_left_sym.info.elem_type return node.return_type
return node.return_type
}
} else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise<') } else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise<')
&& method_name == 'wait' { && method_name == 'wait' {
info := left_sym.info as ast.Struct info := left_sym.info as ast.Struct
@ -1303,12 +1301,11 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
mut exp_arg_typ := ast.Type(0) // type of 1st arg for special builtin methods mut exp_arg_typ := ast.Type(0) // type of 1st arg for special builtin methods
mut param_is_mut := false mut param_is_mut := false
mut no_type_promotion := false mut no_type_promotion := false
if left_sym.kind == .chan { if left_sym.info is ast.Chan {
elem_typ := (left_sym.info as ast.Chan).elem_type
if method_name == 'try_push' { if method_name == 'try_push' {
exp_arg_typ = elem_typ.ref() exp_arg_typ = left_sym.info.elem_type.ref()
} else if method_name == 'try_pop' { } else if method_name == 'try_pop' {
exp_arg_typ = elem_typ exp_arg_typ = left_sym.info.elem_type
param_is_mut = true param_is_mut = true
no_type_promotion = true no_type_promotion = true
} }
@ -1317,7 +1314,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
for i, mut arg in node.args { for i, mut arg in node.args {
if i > 0 || exp_arg_typ == ast.Type(0) { if i > 0 || exp_arg_typ == ast.Type(0) {
exp_arg_typ = if method.is_variadic && i >= method.params.len - 1 { exp_arg_typ = if method.is_variadic && i >= method.params.len - 1 {
method.params[method.params.len - 1].typ method.params.last().typ
} else { } else {
method.params[i + 1].typ method.params[i + 1].typ
} }
@ -1342,7 +1339,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
mut final_arg_sym := exp_arg_sym mut final_arg_sym := exp_arg_sym
mut final_arg_typ := exp_arg_typ mut final_arg_typ := exp_arg_typ
if method.is_variadic && exp_arg_sym.info is ast.Array { if method.is_variadic && exp_arg_sym.info is ast.Array {
final_arg_typ = exp_arg_sym.array_info().elem_type final_arg_typ = exp_arg_sym.info.elem_type
final_arg_sym = c.table.sym(final_arg_typ) final_arg_sym = c.table.sym(final_arg_typ)
} }
if exp_arg_typ.has_flag(.generic) { if exp_arg_typ.has_flag(.generic) {
@ -1370,7 +1367,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
} }
} }
param := if method.is_variadic && i >= method.params.len - 1 { param := if method.is_variadic && i >= method.params.len - 1 {
method.params[method.params.len - 1] method.params.last()
} else { } else {
method.params[i + 1] method.params[i + 1]
} }
@ -1405,9 +1402,8 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
c.fail_if_unreadable(arg.expr, got_arg_typ, 'argument') c.fail_if_unreadable(arg.expr, got_arg_typ, 'argument')
} }
} }
if left_sym.kind == .array && method_name == 'sort_with_compare' { if left_sym.info is ast.Array && method_name == 'sort_with_compare' {
array_info := left_sym.info as ast.Array elem_typ := left_sym.info.elem_type
elem_typ := array_info.elem_type
arg_sym := c.table.sym(arg.typ) arg_sym := c.table.sym(arg.typ)
if arg_sym.kind == .function { if arg_sym.kind == .function {
func_info := arg_sym.info as ast.FnType func_info := arg_sym.info as ast.FnType
@ -1448,13 +1444,11 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
// } // }
param_typ_sym := c.table.sym(exp_arg_typ) param_typ_sym := c.table.sym(exp_arg_typ)
arg_typ_sym := c.table.sym(got_arg_typ) arg_typ_sym := c.table.sym(got_arg_typ)
if param_typ_sym.kind == .array && arg_typ_sym.kind == .array { if param_typ_sym.info is ast.Array && arg_typ_sym.info is ast.Array {
param_info := param_typ_sym.info as ast.Array param_elem_type := c.table.unaliased_type(param_typ_sym.info.elem_type)
param_elem_type := c.table.unaliased_type(param_info.elem_type) arg_elem_type := c.table.unaliased_type(arg_typ_sym.info.elem_type)
arg_info := arg_typ_sym.info as ast.Array
arg_elem_type := c.table.unaliased_type(arg_info.elem_type)
if exp_arg_typ.nr_muls() == got_arg_typ.nr_muls() if exp_arg_typ.nr_muls() == got_arg_typ.nr_muls()
&& param_info.nr_dims == arg_info.nr_dims && param_typ_sym.info.nr_dims == arg_typ_sym.info.nr_dims
&& param_elem_type == arg_elem_type { && param_elem_type == arg_elem_type {
continue continue
} }