Skip to content

Commit

Permalink
checker: cleanup array and alias of array method call
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 28, 2023
1 parent b944927 commit 6ccf4ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions vlib/strings/builder.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
12 changes: 7 additions & 5 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit 6ccf4ae

Please sign in to comment.