1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: simplify infer_fn_generic_types() (#15408)

This commit is contained in:
yuyi
2022-08-11 19:25:43 +08:00
committed by GitHub
parent 275a997ec1
commit 9e0bf005f7
2 changed files with 8 additions and 13 deletions

View File

@ -376,7 +376,7 @@ pub fn (mut rng PRNG) choose<T>(array []T, k int) ?[]T {
} }
mut results := []T{len: k} mut results := []T{len: k}
mut indices := []int{len: n, init: it} mut indices := []int{len: n, init: it}
rng.shuffle(mut indices)? rng.shuffle<int>(mut indices)?
for i in 0 .. k { for i in 0 .. k {
results[i] = array[indices[i]] results[i] = array[indices[i]]
} }

View File

@ -620,7 +620,6 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
} }
mut typ := ast.void_type mut typ := ast.void_type
for i, param in func.params { for i, param in func.params {
mut to_set := ast.void_type
// resolve generic struct receiver // resolve generic struct receiver
if node.is_method && param.typ.has_flag(.generic) { if node.is_method && param.typ.has_flag(.generic) {
sym := c.table.final_sym(node.receiver_type) sym := c.table.final_sym(node.receiver_type)
@ -645,32 +644,32 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
} }
} }
arg_i := if i != 0 && node.is_method { i - 1 } else { i } arg_i := if i != 0 && node.is_method { i - 1 } else { i }
if node.args.len <= arg_i { if node.args.len <= arg_i || typ != ast.void_type {
break break
} }
arg := node.args[arg_i] arg := node.args[arg_i]
param_type_sym := c.table.sym(param.typ) param_type_sym := c.table.sym(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name { if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {
to_set = ast.mktyp(arg.typ) typ = ast.mktyp(arg.typ)
sym := c.table.sym(arg.typ) sym := c.table.sym(arg.typ)
if sym.info is ast.FnType { if sym.info is ast.FnType {
mut func_ := sym.info.func mut func_ := sym.info.func
func_.name = '' func_.name = ''
idx := c.table.find_or_register_fn_type(c.mod, func_, true, false) idx := c.table.find_or_register_fn_type(c.mod, func_, true, false)
to_set = ast.new_type(idx).derive(arg.typ) typ = ast.new_type(idx).derive(arg.typ)
} }
if arg.expr.is_auto_deref_var() { if arg.expr.is_auto_deref_var() {
to_set = to_set.deref() typ = typ.deref()
} }
// resolve &T &&T ... // resolve &T &&T ...
if param.typ.nr_muls() > 0 && to_set.nr_muls() > 0 { if param.typ.nr_muls() > 0 && typ.nr_muls() > 0 {
to_set = to_set.set_nr_muls(0) typ = typ.set_nr_muls(0)
} }
} else if param.typ.has_flag(.generic) { } else if param.typ.has_flag(.generic) {
arg_sym := c.table.sym(arg.typ) arg_sym := c.table.sym(arg.typ)
if param.typ.has_flag(.variadic) { if param.typ.has_flag(.variadic) {
to_set = ast.mktyp(arg.typ) typ = ast.mktyp(arg.typ)
} else if arg_sym.kind == .array && param_type_sym.kind == .array { } else if arg_sym.kind == .array && param_type_sym.kind == .array {
mut arg_elem_info := arg_sym.info as ast.Array mut arg_elem_info := arg_sym.info as ast.Array
mut param_elem_info := param_type_sym.info as ast.Array mut param_elem_info := param_type_sym.info as ast.Array
@ -754,10 +753,6 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
} }
} }
} }
if to_set != ast.void_type && typ == ast.void_type {
typ = to_set
}
} }
if typ == ast.void_type { if typ == ast.void_type {
c.error('could not infer generic type `$gt_name` in call to `$func.name`', c.error('could not infer generic type `$gt_name` in call to `$func.name`',