Skip to content

Commit aae3ba8

Browse files
authored
parser,fmt,checker: use a trie for matching the generic array methods all, any, count, filter, map, sort and sorted (#25759)
1 parent a51e6e0 commit aae3ba8

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

vlib/v/ast/types.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,12 @@ pub const voidptr_types = new_voidptr_types()
798798
pub const cptr_types = merge_types(voidptr_types, byteptr_types, charptr_types)
799799
pub const nil_type = new_type(nil_type_idx)
800800

801+
pub const builtin_array_generic_methods = ['all', 'any', 'count', 'filter', 'map', 'sort', 'sorted']
802+
pub const builtin_array_generic_methods_matcher = token.new_keywords_matcher_from_array_trie(builtin_array_generic_methods)
803+
804+
pub const builtin_array_generic_methods_no_sort = ['all', 'any', 'count', 'filter', 'map']
805+
pub const builtin_array_generic_methods_no_sort_matcher = token.new_keywords_matcher_from_array_trie(builtin_array_generic_methods_no_sort)
806+
801807
fn new_charptr_types() []Type {
802808
return [charptr_type, new_type(char_type_idx).set_nr_muls(1)]
803809
}

vlib/v/checker/fn.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3306,7 +3306,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
33063306
elem_typ = array_info.elem_type
33073307
node_args_len := node.args.len
33083308
mut arg0 := if node_args_len > 0 { node.args[0] } else { ast.CallArg{} }
3309-
if method_name in ['filter', 'map', 'any', 'all', 'count'] {
3309+
if ast.builtin_array_generic_methods_no_sort_matcher.matches(method_name) {
33103310
if node_args_len > 0 && mut arg0.expr is ast.LambdaExpr {
33113311
if arg0.expr.params.len != 1 {
33123312
c.error('lambda expressions used in the builtin array methods require exactly 1 parameter',

vlib/v/fmt/fmt.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ fn (mut f Fmt) write_static_method(name string, short_name string) {
20532053
pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
20542054
mut is_method_newline := false
20552055
if node.is_method {
2056-
if node.name in ['map', 'filter', 'all', 'any', 'count'] {
2056+
if ast.builtin_array_generic_methods_no_sort_matcher.matches(node.name) {
20572057
f.in_lambda_depth++
20582058
defer(fn) { f.in_lambda_depth-- }
20592059
}

vlib/v/parser/parser.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,8 +2186,7 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
21862186
} else {
21872187
p.name_error = true
21882188
}
2189-
is_filter := field_name in ['filter', 'map', 'any', 'all', 'count']
2190-
if is_filter || field_name == 'sort' || field_name == 'sorted' {
2189+
if ast.builtin_array_generic_methods_matcher.matches(field_name) {
21912190
if p.file_backend_mode == .v || p.file_backend_mode == .c {
21922191
p.register_auto_import('builtin.closure')
21932192
}

0 commit comments

Comments
 (0)