Skip to content

Commit edbafcb

Browse files
authored
checker: check fixed array builtin method args mismatch (#22626)
1 parent 8b36856 commit edbafcb

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,10 +3484,12 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
34843484
if method_name == 'index' {
34853485
if node.args.len != 1 {
34863486
c.error('`.index()` expected 1 argument, but got ${node.args.len}', node.pos)
3487+
return ast.void_type
34873488
} else if !left_sym.has_method('index') {
34883489
arg_typ := c.expr(mut node.args[0].expr)
34893490
c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or {
34903491
c.error('${err.msg()} in argument 1 to `.index()`', node.args[0].pos)
3492+
return ast.void_type
34913493
}
34923494
}
34933495
for i, mut arg in node.args {
@@ -3497,17 +3499,24 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
34973499
} else if method_name == 'contains' {
34983500
if node.args.len != 1 {
34993501
c.error('`.contains()` expected 1 argument, but got ${node.args.len}', node.pos)
3502+
return ast.void_type
35003503
} else if !left_sym.has_method('contains') {
35013504
arg_typ := c.expr(mut node.args[0].expr)
35023505
c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or {
35033506
c.error('${err.msg()} in argument 1 to `.contains()`', node.args[0].pos)
3507+
return ast.void_type
35043508
}
35053509
}
35063510
for i, mut arg in node.args {
35073511
node.args[i].typ = c.expr(mut arg.expr)
35083512
}
35093513
node.return_type = ast.bool_type
35103514
} else if method_name in ['any', 'all'] {
3515+
if node.args.len != 1 {
3516+
c.error('`.${method_name}` expected 1 argument, but got ${node.args.len}',
3517+
node.pos)
3518+
return ast.void_type
3519+
}
35113520
if node.args.len > 0 && mut node.args[0].expr is ast.LambdaExpr {
35123521
if node.args[0].expr.params.len != 1 {
35133522
c.error('lambda expressions used in the builtin array methods require exactly 1 parameter',
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:4:11: error: `.index()` expected 1 argument, but got 0
2+
2 | arr := [1, 2, 3]!
3+
3 |
4+
4 | _ := arr.index()
5+
| ~~~~~~~
6+
5 | _ := arr.index('hello')
7+
6 | _ := arr.contains()
8+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:4:4: error: assignment mismatch: 1 variable but `index()` returns 0 values
9+
2 | arr := [1, 2, 3]!
10+
3 |
11+
4 | _ := arr.index()
12+
| ~~
13+
5 | _ := arr.index('hello')
14+
6 | _ := arr.contains()
15+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:5:17: error: cannot use `string` as `int` in argument 1 to `.index()`
16+
3 |
17+
4 | _ := arr.index()
18+
5 | _ := arr.index('hello')
19+
| ~~~~~~~
20+
6 | _ := arr.contains()
21+
7 | _ := arr.contains('hello')
22+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:5:4: error: assignment mismatch: 1 variable but `index()` returns 0 values
23+
3 |
24+
4 | _ := arr.index()
25+
5 | _ := arr.index('hello')
26+
| ~~
27+
6 | _ := arr.contains()
28+
7 | _ := arr.contains('hello')
29+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:6:11: error: `.contains()` expected 1 argument, but got 0
30+
4 | _ := arr.index()
31+
5 | _ := arr.index('hello')
32+
6 | _ := arr.contains()
33+
| ~~~~~~~~~~
34+
7 | _ := arr.contains('hello')
35+
8 | _ := arr.any()
36+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:6:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values
37+
4 | _ := arr.index()
38+
5 | _ := arr.index('hello')
39+
6 | _ := arr.contains()
40+
| ~~
41+
7 | _ := arr.contains('hello')
42+
8 | _ := arr.any()
43+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:7:20: error: cannot use `string` as `int` in argument 1 to `.contains()`
44+
5 | _ := arr.index('hello')
45+
6 | _ := arr.contains()
46+
7 | _ := arr.contains('hello')
47+
| ~~~~~~~
48+
8 | _ := arr.any()
49+
9 | _ := arr.any(22)
50+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:7:4: error: assignment mismatch: 1 variable but `contains()` returns 0 values
51+
5 | _ := arr.index('hello')
52+
6 | _ := arr.contains()
53+
7 | _ := arr.contains('hello')
54+
| ~~
55+
8 | _ := arr.any()
56+
9 | _ := arr.any(22)
57+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:8:11: error: `.any` expected 1 argument, but got 0
58+
6 | _ := arr.contains()
59+
7 | _ := arr.contains('hello')
60+
8 | _ := arr.any()
61+
| ~~~~~
62+
9 | _ := arr.any(22)
63+
10 | _ := arr.all()
64+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:8:4: error: assignment mismatch: 1 variable but `any()` returns 0 values
65+
6 | _ := arr.contains()
66+
7 | _ := arr.contains('hello')
67+
8 | _ := arr.any()
68+
| ~~
69+
9 | _ := arr.any(22)
70+
10 | _ := arr.all()
71+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:10:11: error: `.all` expected 1 argument, but got 0
72+
8 | _ := arr.any()
73+
9 | _ := arr.any(22)
74+
10 | _ := arr.all()
75+
| ~~~~~
76+
11 | _ := arr.all('hello')
77+
12 | }
78+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:10:4: error: assignment mismatch: 1 variable but `all()` returns 0 values
79+
8 | _ := arr.any()
80+
9 | _ := arr.any(22)
81+
10 | _ := arr.all()
82+
| ~~
83+
11 | _ := arr.all('hello')
84+
12 | }
85+
vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv:11:15: error: type mismatch, should use e.g. `all(it > 2)`
86+
9 | _ := arr.any(22)
87+
10 | _ := arr.all()
88+
11 | _ := arr.all('hello')
89+
| ~~~~~~~
90+
12 | }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
arr := [1, 2, 3]!
3+
4+
_ := arr.index()
5+
_ := arr.index('hello')
6+
_ := arr.contains()
7+
_ := arr.contains('hello')
8+
_ := arr.any()
9+
_ := arr.any(22)
10+
_ := arr.all()
11+
_ := arr.all('hello')
12+
}

0 commit comments

Comments
 (0)