checker: check array builtin method's arguments (#14119)

master
yuyi 2022-04-21 17:51:04 +08:00 committed by GitHub
parent 8013bd43b0
commit f13583b04a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View File

@ -1884,6 +1884,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
c.check_map_and_filter(false, elem_typ, node)
node.return_type = ast.bool_type
} else if method_name == 'clone' {
if node.args.len != 0 {
c.error('`.clone()` does not have any arguments', node.args[0].pos)
}
// need to return `array_xxx` instead of `array`
// in ['clone', 'str'] {
node.receiver_type = left_type.ref()
@ -1914,6 +1917,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
} else if method_name == 'index' {
node.return_type = ast.int_type
} else if method_name in ['first', 'last', 'pop'] {
if node.args.len != 0 {
c.error('`.${method_name}()` does not have any arguments', node.args[0].pos)
}
node.return_type = array_info.elem_type
if method_name == 'pop' {
c.fail_if_immutable(node.left)

View File

@ -0,0 +1,28 @@
vlib/v/checker/tests/array_builtin_method_args_err.vv:4:18: error: `.clone()` does not have any arguments
2 | arr := [1, 2, 3]
3 |
4 | a1 := arr.clone(22)
| ~~
5 | println(a1)
6 |
vlib/v/checker/tests/array_builtin_method_args_err.vv:7:18: error: `.first()` does not have any arguments
5 | println(a1)
6 |
7 | a2 := arr.first('a2')
| ~~~~
8 | println(a2)
9 |
vlib/v/checker/tests/array_builtin_method_args_err.vv:10:17: error: `.last()` does not have any arguments
8 | println(a2)
9 |
10 | a3 := arr.last(1)
| ^
11 | println(a3)
12 |
vlib/v/checker/tests/array_builtin_method_args_err.vv:13:16: error: `.pop()` does not have any arguments
11 | println(a3)
12 |
13 | a4 := arr.pop(2)
| ^
14 | println(a4)
15 | }

View File

@ -0,0 +1,15 @@
fn main() {
arr := [1, 2, 3]
a1 := arr.clone(22)
println(a1)
a2 := arr.first('a2')
println(a2)
a3 := arr.last(1)
println(a3)
a4 := arr.pop(2)
println(a4)
}