Skip to content

Commit

Permalink
checker: check array.delete() argument mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 25, 2024
1 parent 4248fc5 commit 75f8b70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/array_delete_arg_mismatch_err.out
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 23 additions & 0 deletions vlib/v/checker/tests/array_delete_arg_mismatch_err.vv
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 75f8b70

Please sign in to comment.