From eca95dcedc1e75608bc483c3bbbe31e3e4e4793b Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 21 Apr 2022 17:51:04 +0800 Subject: [PATCH] checker: check array builtin method's arguments (#14119) --- vlib/v/checker/fn.v | 6 ++++ .../tests/array_builtin_method_args_err.out | 28 +++++++++++++++++++ .../tests/array_builtin_method_args_err.vv | 15 ++++++++++ 3 files changed, 49 insertions(+) create mode 100644 vlib/v/checker/tests/array_builtin_method_args_err.out create mode 100644 vlib/v/checker/tests/array_builtin_method_args_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 7200e18047..d092682ce1 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1884,6 +1884,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as c.check_map_and_filter(false, elem_typ, node) node.return_type = ast.bool_type } else if method_name == 'clone' { + if node.args.len != 0 { + c.error('`.clone()` does not have any arguments', node.args[0].pos) + } // need to return `array_xxx` instead of `array` // in ['clone', 'str'] { node.receiver_type = left_type.ref() @@ -1914,6 +1917,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as } else if method_name == 'index' { node.return_type = ast.int_type } else if method_name in ['first', 'last', 'pop'] { + if node.args.len != 0 { + c.error('`.${method_name}()` does not have any arguments', node.args[0].pos) + } node.return_type = array_info.elem_type if method_name == 'pop' { c.fail_if_immutable(node.left) diff --git a/vlib/v/checker/tests/array_builtin_method_args_err.out b/vlib/v/checker/tests/array_builtin_method_args_err.out new file mode 100644 index 0000000000..db760305c3 --- /dev/null +++ b/vlib/v/checker/tests/array_builtin_method_args_err.out @@ -0,0 +1,28 @@ +vlib/v/checker/tests/array_builtin_method_args_err.vv:4:18: error: `.clone()` does not have any arguments + 2 | arr := [1, 2, 3] + 3 | + 4 | a1 := arr.clone(22) + | ~~ + 5 | println(a1) + 6 | +vlib/v/checker/tests/array_builtin_method_args_err.vv:7:18: error: `.first()` does not have any arguments + 5 | println(a1) + 6 | + 7 | a2 := arr.first('a2') + | ~~~~ + 8 | println(a2) + 9 | +vlib/v/checker/tests/array_builtin_method_args_err.vv:10:17: error: `.last()` does not have any arguments + 8 | println(a2) + 9 | + 10 | a3 := arr.last(1) + | ^ + 11 | println(a3) + 12 | +vlib/v/checker/tests/array_builtin_method_args_err.vv:13:16: error: `.pop()` does not have any arguments + 11 | println(a3) + 12 | + 13 | a4 := arr.pop(2) + | ^ + 14 | println(a4) + 15 | } diff --git a/vlib/v/checker/tests/array_builtin_method_args_err.vv b/vlib/v/checker/tests/array_builtin_method_args_err.vv new file mode 100644 index 0000000000..45a836bbc0 --- /dev/null +++ b/vlib/v/checker/tests/array_builtin_method_args_err.vv @@ -0,0 +1,15 @@ +fn main() { + arr := [1, 2, 3] + + a1 := arr.clone(22) + println(a1) + + a2 := arr.first('a2') + println(a2) + + a3 := arr.last(1) + println(a3) + + a4 := arr.pop(2) + println(a4) +}