1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check arguments count of sort (#9105)

This commit is contained in:
zakuro 2021-03-04 22:22:47 +09:00 committed by GitHub
parent 746fe47e94
commit 4f02da00ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -1323,6 +1323,10 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
c.fail_if_immutable(call_expr.left) c.fail_if_immutable(call_expr.left)
// position of `a` and `b` doesn't matter, they're the same // position of `a` and `b` doesn't matter, they're the same
scope_register_ab(mut call_expr.scope, call_expr.pos, array_info.elem_type) scope_register_ab(mut call_expr.scope, call_expr.pos, array_info.elem_type)
if call_expr.args.len > 1 {
c.error('expected 0 or 1 argument, but got $call_expr.args.len', call_expr.pos)
}
// Verify `.sort(a < b)` // Verify `.sort(a < b)`
if call_expr.args.len > 0 { if call_expr.args.len > 0 {
if call_expr.args[0].expr !is ast.InfixExpr { if call_expr.args[0].expr !is ast.InfixExpr {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/array_sort_err.vv:3:6: error: expected 0 or 1 argument, but got 2
1 | fn main() {
2 | mut arr := [3, 2, 1]
3 | arr.sort(a < b, a)
| ~~~~~~~~~~~~~~
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
mut arr := [3, 2, 1]
arr.sort(a < b, a)
}