Skip to content

Commit

Permalink
checker: check fixed array builtin method args mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 23, 2024
1 parent 9dc1a99 commit a218d21
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
9 changes: 9 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -3497,17 +3499,24 @@ 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 {
node.args[i].typ = c.expr(mut arg.expr)
}
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',
Expand Down
90 changes: 90 additions & 0 deletions vlib/v/checker/tests/fixed_array_builtin_method_args_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/fixed_array_builtin_method_args_err.vv
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit a218d21

Please sign in to comment.