From edbafcb70a73298c8bb4c93d280e50fd78727a8f Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 23 Oct 2024 22:52:13 +0800 Subject: [PATCH] checker: check fixed array builtin method args mismatch (#22626) --- vlib/v/checker/fn.v | 9 ++ .../fixed_array_builtin_method_args_err.out | 90 +++++++++++++++++++ .../fixed_array_builtin_method_args_err.vv | 12 +++ 3 files changed, 111 insertions(+) create mode 100644 vlib/v/checker/tests/fixed_array_builtin_method_args_err.out create mode 100644 vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 286de40d7c..f60fc0234a 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3484,10 +3484,12 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t if method_name == 'index' { if node.args.len != 1 { c.error('`.index()` expected 1 argument, but got ${node.args.len}', node.pos) + return ast.void_type } else if !left_sym.has_method('index') { arg_typ := c.expr(mut node.args[0].expr) c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or { c.error('${err.msg()} in argument 1 to `.index()`', node.args[0].pos) + return ast.void_type } } for i, mut arg in node.args { @@ -3497,10 +3499,12 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t } else if method_name == 'contains' { if node.args.len != 1 { c.error('`.contains()` expected 1 argument, but got ${node.args.len}', node.pos) + return ast.void_type } else if !left_sym.has_method('contains') { arg_typ := c.expr(mut node.args[0].expr) c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or { c.error('${err.msg()} in argument 1 to `.contains()`', node.args[0].pos) + return ast.void_type } } for i, mut arg in node.args { @@ -3508,6 +3512,11 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t } node.return_type = ast.bool_type } else if method_name in ['any', 'all'] { + if node.args.len != 1 { + c.error('`.${method_name}` expected 1 argument, but got ${node.args.len}', + node.pos) + return ast.void_type + } if node.args.len > 0 && mut node.args[0].expr is ast.LambdaExpr { if node.args[0].expr.params.len != 1 { c.error('lambda expressions used in the builtin array methods require exactly 1 parameter', diff --git a/vlib/v/checker/tests/fixed_array_builtin_method_args_err.out b/vlib/v/checker/tests/fixed_array_builtin_method_args_err.out new file mode 100644 index 0000000000..5920cde67c --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_builtin_method_args_err.out @@ -0,0 +1,90 @@ +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:4:11: error: `.index()` expected 1 argument, but got 0 + 2 | arr := [1, 2, 3]! + 3 | + 4 | _ := arr.index() + | ~~~~~~~ + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:4:4: error: assignment mismatch: 1 variable but `index()` returns 0 values + 2 | arr := [1, 2, 3]! + 3 | + 4 | _ := arr.index() + | ~~ + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:5:17: error: cannot use `string` as `int` in argument 1 to `.index()` + 3 | + 4 | _ := arr.index() + 5 | _ := arr.index('hello') + | ~~~~~~~ + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:5:4: error: assignment mismatch: 1 variable but `index()` returns 0 values + 3 | + 4 | _ := arr.index() + 5 | _ := arr.index('hello') + | ~~ + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:6:11: error: `.contains()` expected 1 argument, but got 0 + 4 | _ := arr.index() + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() + | ~~~~~~~~~~ + 7 | _ := arr.contains('hello') + 8 | _ := arr.any() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:6:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values + 4 | _ := arr.index() + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() + | ~~ + 7 | _ := arr.contains('hello') + 8 | _ := arr.any() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:7:20: error: cannot use `string` as `int` in argument 1 to `.contains()` + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') + | ~~~~~~~ + 8 | _ := arr.any() + 9 | _ := arr.any(22) +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:7:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values + 5 | _ := arr.index('hello') + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') + | ~~ + 8 | _ := arr.any() + 9 | _ := arr.any(22) +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:8:11: error: `.any` expected 1 argument, but got 0 + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') + 8 | _ := arr.any() + | ~~~~~ + 9 | _ := arr.any(22) + 10 | _ := arr.all() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:8:4: error: assignment mismatch: 1 variable but `any()` returns 0 values + 6 | _ := arr.contains() + 7 | _ := arr.contains('hello') + 8 | _ := arr.any() + | ~~ + 9 | _ := arr.any(22) + 10 | _ := arr.all() +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:10:11: error: `.all` expected 1 argument, but got 0 + 8 | _ := arr.any() + 9 | _ := arr.any(22) + 10 | _ := arr.all() + | ~~~~~ + 11 | _ := arr.all('hello') + 12 | } +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:10:4: error: assignment mismatch: 1 variable but `all()` returns 0 values + 8 | _ := arr.any() + 9 | _ := arr.any(22) + 10 | _ := arr.all() + | ~~ + 11 | _ := arr.all('hello') + 12 | } +vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:11:15: error: type mismatch, should use e.g. `all(it > 2)` + 9 | _ := arr.any(22) + 10 | _ := arr.all() + 11 | _ := arr.all('hello') + | ~~~~~~~ + 12 | } diff --git a/vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv b/vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv new file mode 100644 index 0000000000..9acece71c1 --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv @@ -0,0 +1,12 @@ +fn main() { + arr := [1, 2, 3]! + + _ := arr.index() + _ := arr.index('hello') + _ := arr.contains() + _ := arr.contains('hello') + _ := arr.any() + _ := arr.any(22) + _ := arr.all() + _ := arr.all('hello') +}