From 6ccf4ae5f1921aa722942737b656037dd8762ce3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 28 Dec 2023 17:22:10 +0800 Subject: [PATCH] checker: cleanup array and alias of array method call --- vlib/strings/builder.js.v | 8 ++++---- vlib/v/checker/fn.v | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/vlib/strings/builder.js.v b/vlib/strings/builder.js.v index aca8d7f1d424af..caf2adb49b35ec 100644 --- a/vlib/strings/builder.js.v +++ b/vlib/strings/builder.js.v @@ -73,7 +73,7 @@ pub fn (mut b Builder) str() string { pub fn (mut b Builder) cut_last(n int) string { cut_pos := b.len - n - x := b[cut_pos..] + x := unsafe { b[cut_pos..] } res := x.bytestr() b.trim(cut_pos) return res @@ -113,17 +113,17 @@ pub fn (mut b Builder) after(n int) string { return '' } - x := b.slice(n, b.len) + x := unsafe { b[n..b.len] } return x.bytestr() } // last_n(5) returns 'world' // buf == 'hello world' -pub fn (b &Builder) last_n(n int) string { +pub fn (mut b Builder) last_n(n int) string { if n >= b.len { return '' } - x := b.slice(b.len - n, b.len) + x := unsafe { b[b.len - n..b.len] } return x.bytestr() } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index fc29be22bc2874..25f7d049bb3f7c 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1741,7 +1741,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { // TODO: remove this for actual methods, use only for compiler magic // FIXME: Argument count != 1 will break these if left_sym.kind == .array && array_builtin_methods_chk.matches(method_name) { - return c.array_builtin_method_call(mut node, left_type, c.table.sym(left_type)) + return c.array_builtin_method_call(mut node, left_type) } else if (left_sym.kind == .map || final_left_sym.kind == .map) && method_name in ['clone', 'keys', 'values', 'move', 'delete'] { if left_sym.kind == .map { @@ -1778,9 +1778,10 @@ 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) { 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 ['filter', 'map', 'sort', 'sorted', 'contains', 'any', 'all', 'first', 'last', 'pop'] { - return c.array_builtin_method_call(mut node, unwrapped_left_type, final_left_sym) + } else if left_sym.kind == .alias && final_left_sym.kind == .array + && array_builtin_methods_chk.matches(method_name) + && !left_sym.has_method_with_generic_parent(method_name) { + return c.array_builtin_method_call(mut node, unwrapped_left_type) } else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[') && method_name == 'wait' { info := left_sym.info as ast.Struct @@ -2731,7 +2732,8 @@ fn (mut c Checker) ensure_same_array_return_type(mut node ast.CallExpr, left_typ } } -fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type ast.Type, left_sym ast.TypeSymbol) ast.Type { +fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type ast.Type) ast.Type { + left_sym := c.table.final_sym(left_type) method_name := node.name mut elem_typ := ast.void_type if !c.is_builtin_mod && method_name == 'slice' {