checker: cleanup method_call() (#22639)

This commit is contained in:
yuyi 2024-10-24 15:51:46 +08:00 committed by GitHub
parent 42cc345381
commit 75de056dfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 22 deletions

View File

@ -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']

View File

@ -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
}