From 75de056dfcae3a22bf467b1012a5417e61e647a2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 24 Oct 2024 15:51:46 +0800 Subject: [PATCH] checker: cleanup method_call() (#22639) --- vlib/v/checker/checker.v | 2 ++ vlib/v/checker/fn.v | 42 +++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index d93403406f..a19ef8b7e8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -32,6 +32,8 @@ pub const array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map' 'sort_with_compare', 'sorted', 'sorted_with_compare', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop', 'delete', 'insert', 'prepend'] pub const array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods) +pub const fixed_array_builtin_methods = ['contains', 'index', 'any', 'all', 'wait', 'map', 'sort'] +pub const fixed_array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(fixed_array_builtin_methods) // TODO: remove `byte` from this list when it is no longer supported pub const reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread'] diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 21d4f8db0c..c4ab008964 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -2040,7 +2040,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { && !(left_sym.kind == .alias && left_sym.has_method(method_name)) { return c.array_builtin_method_call(mut node, left_type) } else if final_left_sym.kind == .array_fixed - && method_name in ['contains', 'index', 'all', 'any', 'map'] && !(left_sym.kind == .alias + && fixed_array_builtin_methods_chk.matches(method_name) && !(left_sym.kind == .alias && left_sym.has_method(method_name)) { return c.fixed_array_builtin_method_call(mut node, left_type) } else if final_left_sym.kind == .map @@ -2066,27 +2066,6 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } node.return_type = left_sym.info.return_type return left_sym.info.return_type - } else if final_left_sym.info is ast.ArrayFixed && method_name == 'wait' { - elem_sym := c.table.sym(final_left_sym.info.elem_type) - if elem_sym.kind == .thread { - if node.args.len != 0 { - c.error('`.wait()` does not have any arguments', node.args[0].pos) - } - thread_ret_type := c.unwrap_generic(elem_sym.thread_info().return_type) - if thread_ret_type.has_flag(.option) { - c.error('`.wait()` cannot be called for an array when thread functions return options. Iterate over the arrays elements instead and handle each returned option with `or`.', - node.pos) - } else if thread_ret_type.has_flag(.result) { - c.error('`.wait()` cannot be called for an array when thread functions return results. Iterate over the arrays elements instead and handle each returned result with `or`.', - node.pos) - } - node.return_type = c.table.find_or_register_array(thread_ret_type) - return node.return_type - } else { - c.error('`${left_sym.name}` has no method `wait()` (only thread handles and arrays of them have)', - node.left.pos()) - return ast.void_type - } } else if left_sym.kind == .char && left_type.nr_muls() == 0 && method_name == 'str' { c.error('calling `.str()` on type `char` is not allowed, use its address or cast it to an integer instead', node.left.pos().extend(node.pos)) @@ -3500,6 +3479,25 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t c.expr(mut node.args[0].expr) c.check_map_and_filter(false, elem_typ, node) node.return_type = ast.bool_type + } else if method_name == 'wait' { + elem_sym := c.table.sym(elem_typ) + if elem_sym.kind == .thread { + if node.args.len != 0 { + c.error('`.wait()` does not have any arguments', node.args[0].pos) + } + thread_ret_type := c.unwrap_generic(elem_sym.thread_info().return_type) + if thread_ret_type.has_flag(.option) { + c.error('`.wait()` cannot be called for an array when thread functions return options. Iterate over the arrays elements instead and handle each returned option with `or`.', + node.pos) + } else if thread_ret_type.has_flag(.result) { + c.error('`.wait()` cannot be called for an array when thread functions return results. Iterate over the arrays elements instead and handle each returned result with `or`.', + node.pos) + } + node.return_type = c.table.find_or_register_array(thread_ret_type) + } else { + c.error('`${left_sym.name}` has no method `wait()` (only thread handles and arrays of them have)', + node.left.pos()) + } } return node.return_type }