checker: check fixed array builtin method args mismatch (#22626)

This commit is contained in:
yuyi 2024-10-23 22:52:13 +08:00 committed by GitHub
parent 8b36856692
commit edbafcb70a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 111 additions and 0 deletions

View File

@ -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',

View File

@ -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 | }

View File

@ -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')
}