mirror of
https://github.com/vlang/v.git
synced 2025-09-14 23:12:33 +03:00
checker: cleanup array and alias of array method call (#20290)
This commit is contained in:
parent
d816f0c2fd
commit
5b96d8d179
2 changed files with 11 additions and 9 deletions
|
@ -73,7 +73,7 @@ pub fn (mut b Builder) str() string {
|
||||||
|
|
||||||
pub fn (mut b Builder) cut_last(n int) string {
|
pub fn (mut b Builder) cut_last(n int) string {
|
||||||
cut_pos := b.len - n
|
cut_pos := b.len - n
|
||||||
x := b[cut_pos..]
|
x := unsafe { b[cut_pos..] }
|
||||||
res := x.bytestr()
|
res := x.bytestr()
|
||||||
b.trim(cut_pos)
|
b.trim(cut_pos)
|
||||||
return res
|
return res
|
||||||
|
@ -113,17 +113,17 @@ pub fn (mut b Builder) after(n int) string {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
x := b.slice(n, b.len)
|
x := unsafe { b[n..b.len] }
|
||||||
return x.bytestr()
|
return x.bytestr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// last_n(5) returns 'world'
|
// last_n(5) returns 'world'
|
||||||
// buf == 'hello 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 {
|
if n >= b.len {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
||||||
x := b.slice(b.len - n, b.len)
|
x := unsafe { b[b.len - n..b.len] }
|
||||||
return x.bytestr()
|
return x.bytestr()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
// TODO: remove this for actual methods, use only for compiler magic
|
||||||
// FIXME: Argument count != 1 will break these
|
// FIXME: Argument count != 1 will break these
|
||||||
if left_sym.kind == .array && array_builtin_methods_chk.matches(method_name) {
|
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)
|
} else if (left_sym.kind == .map || final_left_sym.kind == .map)
|
||||||
&& method_name in ['clone', 'keys', 'values', 'move', 'delete'] {
|
&& method_name in ['clone', 'keys', 'values', 'move', 'delete'] {
|
||||||
if left_sym.kind == .map {
|
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) {
|
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())
|
c.error('cannot ${method_name} `${arg_sym.name}` to `${left_sym.name}`', arg_expr.pos())
|
||||||
}
|
}
|
||||||
} else if final_left_sym.kind == .array
|
} else if left_sym.kind == .alias && final_left_sym.kind == .array
|
||||||
&& method_name in ['filter', 'map', 'sort', 'sorted', 'contains', 'any', 'all', 'first', 'last', 'pop'] {
|
&& array_builtin_methods_chk.matches(method_name) && method_name != 'clone'
|
||||||
return c.array_builtin_method_call(mut node, unwrapped_left_type, final_left_sym)
|
&& !left_sym.has_method(method_name) {
|
||||||
|
return c.array_builtin_method_call(mut node, left_type)
|
||||||
} else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[')
|
} else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[')
|
||||||
&& method_name == 'wait' {
|
&& method_name == 'wait' {
|
||||||
info := left_sym.info as ast.Struct
|
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
|
method_name := node.name
|
||||||
mut elem_typ := ast.void_type
|
mut elem_typ := ast.void_type
|
||||||
if !c.is_builtin_mod && method_name == 'slice' {
|
if !c.is_builtin_mod && method_name == 'slice' {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue