From 1dfe004f53dbd218e5f28ff3ae26eae030ead449 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 26 Sep 2024 03:03:00 +0800 Subject: [PATCH] checker: check array.delete() argument mismatch (#22307) --- vlib/v/checker/fn.v | 8 +++++++ .../tests/array_delete_arg_mismatch_err.out | 7 ++++++ .../tests/array_delete_arg_mismatch_err.vv | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 vlib/v/checker/tests/array_delete_arg_mismatch_err.out create mode 100644 vlib/v/checker/tests/array_delete_arg_mismatch_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index e73f7d0db215c0..bcc693e90bf929 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3359,6 +3359,14 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if method := c.table.find_method(unwrapped_left_sym, method_name) { node.receiver_type = method.receiver_type } + if node.args.len != 1 { + c.error('`.delete()` expected 1 argument, but got ${node.args.len}', node.pos) + } else { + arg_typ := c.expr(mut node.args[0].expr) + c.check_expected_call_arg(arg_typ, ast.int_type, node.language, node.args[0]) or { + c.error('${err.msg()} in argument 1 to `.delete()`', node.args[0].pos) + } + } node.return_type = ast.void_type } return node.return_type diff --git a/vlib/v/checker/tests/array_delete_arg_mismatch_err.out b/vlib/v/checker/tests/array_delete_arg_mismatch_err.out new file mode 100644 index 00000000000000..1270931896ce71 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_arg_mismatch_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/array_delete_arg_mismatch_err.vv:18:23: error: cannot use `IGameObject` as `int` in argument 1 to `.delete()` + 16 | fn (mut game Game) gc() { + 17 | for obj in game.objects { + 18 | game.objects.delete(obj) + | ~~~ + 19 | } + 20 | game.objects.clear() diff --git a/vlib/v/checker/tests/array_delete_arg_mismatch_err.vv b/vlib/v/checker/tests/array_delete_arg_mismatch_err.vv new file mode 100644 index 00000000000000..10514f37fe37f3 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_arg_mismatch_err.vv @@ -0,0 +1,23 @@ +interface IGameObject { +mut: + name string +} + +struct GameObject implements IGameObject { +mut: + name string +} + +struct Game { +mut: + objects []IGameObject +} + +fn (mut game Game) gc() { + for obj in game.objects { + game.objects.delete(obj) + } + game.objects.clear() +} + +fn main() {}