From 8824f5f103873ea7e9ceefd259783cfa349a3c79 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 21 Apr 2022 17:49:46 +0800 Subject: [PATCH] checker: check map builtin method's arguments (#14120) --- vlib/v/checker/fn.v | 6 ++++++ .../tests/map_builtin_method_args_err.out | 21 +++++++++++++++++++ .../tests/map_builtin_method_args_err.vv | 12 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 vlib/v/checker/tests/map_builtin_method_args_err.out create mode 100644 vlib/v/checker/tests/map_builtin_method_args_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 1ac5c9719c..7200e18047 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -1743,6 +1743,9 @@ fn (mut c Checker) map_builtin_method_call(mut node ast.CallExpr, left_type ast. mut ret_type := ast.void_type match method_name { 'clone', 'move' { + if node.args.len != 0 { + c.error('`.${method_name}()` does not have any arguments', node.args[0].pos) + } if method_name[0] == `m` { c.fail_if_immutable(node.left) } @@ -1754,6 +1757,9 @@ fn (mut c Checker) map_builtin_method_call(mut node ast.CallExpr, left_type ast. ret_type = ret_type.clear_flag(.shared_f) } 'keys' { + if node.args.len != 0 { + c.error('`.keys()` does not have any arguments', node.args[0].pos) + } info := left_sym.info as ast.Map typ := c.table.find_or_register_array(info.key_type) ret_type = ast.Type(typ) diff --git a/vlib/v/checker/tests/map_builtin_method_args_err.out b/vlib/v/checker/tests/map_builtin_method_args_err.out new file mode 100644 index 0000000000..1ac2eed224 --- /dev/null +++ b/vlib/v/checker/tests/map_builtin_method_args_err.out @@ -0,0 +1,21 @@ +vlib/v/checker/tests/map_builtin_method_args_err.vv:4:16: error: `.clone()` does not have any arguments + 2 | mut m := {11: 22} + 3 | + 4 | a1 := m.clone(11) + | ~~ + 5 | println(a1) + 6 | +vlib/v/checker/tests/map_builtin_method_args_err.vv:7:15: error: `.move()` does not have any arguments + 5 | println(a1) + 6 | + 7 | a2 := m.move(11) + | ~~ + 8 | println(a2) + 9 | +vlib/v/checker/tests/map_builtin_method_args_err.vv:10:15: error: `.keys()` does not have any arguments + 8 | println(a2) + 9 | + 10 | a3 := m.keys('aaa') + | ~~~~~ + 11 | println(a3) + 12 | } diff --git a/vlib/v/checker/tests/map_builtin_method_args_err.vv b/vlib/v/checker/tests/map_builtin_method_args_err.vv new file mode 100644 index 0000000000..5381788fcd --- /dev/null +++ b/vlib/v/checker/tests/map_builtin_method_args_err.vv @@ -0,0 +1,12 @@ +fn main() { + mut m := {11: 22} + + a1 := m.clone(11) + println(a1) + + a2 := m.move(11) + println(a2) + + a3 := m.keys('aaa') + println(a3) +}