From 9e0bf005f75cdf429d7c493dce67d09607f099a5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 11 Aug 2022 19:25:43 +0800 Subject: [PATCH] checker: simplify infer_fn_generic_types() (#15408) --- vlib/rand/rand.v | 2 +- vlib/v/checker/check_types.v | 19 +++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index c2e6cc0d9..cafd76fab 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -376,7 +376,7 @@ pub fn (mut rng PRNG) choose(array []T, k int) ?[]T { } mut results := []T{len: k} mut indices := []int{len: n, init: it} - rng.shuffle(mut indices)? + rng.shuffle(mut indices)? for i in 0 .. k { results[i] = array[indices[i]] } diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index f5716d321..bdfbacb33 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -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`', -- 2.30.2