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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 indices := []int{len: n, init: it}
rng.shuffle(mut indices)?
rng.shuffle<int>(mut indices)?
for i in 0 .. k {
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
for i, param in func.params {
mut to_set := ast.void_type
// resolve generic struct receiver
if node.is_method && param.typ.has_flag(.generic) {
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 }
if node.args.len <= arg_i {
if node.args.len <= arg_i || typ != ast.void_type {
break
}
arg := node.args[arg_i]
param_type_sym := c.table.sym(param.typ)
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)
if sym.info is ast.FnType {
mut func_ := sym.info.func
func_.name = ''
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() {
to_set = to_set.deref()
typ = typ.deref()
}
// resolve &T &&T ...
if param.typ.nr_muls() > 0 && to_set.nr_muls() > 0 {
to_set = to_set.set_nr_muls(0)
if param.typ.nr_muls() > 0 && typ.nr_muls() > 0 {
typ = typ.set_nr_muls(0)
}
} else if param.typ.has_flag(.generic) {
arg_sym := c.table.sym(arg.typ)
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 {
mut arg_elem_info := arg_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 {
c.error('could not infer generic type `$gt_name` in call to `$func.name`',