From 4f02da00ba97a9f3ef15ed2a1ae659585596c54e Mon Sep 17 00:00:00 2001 From: zakuro Date: Thu, 4 Mar 2021 22:22:47 +0900 Subject: [PATCH] checker: check arguments count of sort (#9105) --- vlib/v/checker/checker.v | 4 ++++ vlib/v/checker/tests/array_sort_err.out | 6 ++++++ vlib/v/checker/tests/array_sort_err.vv | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 vlib/v/checker/tests/array_sort_err.out create mode 100644 vlib/v/checker/tests/array_sort_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ba063e10d5..2346d1c3ba 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) // 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) + + 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)` if call_expr.args.len > 0 { if call_expr.args[0].expr !is ast.InfixExpr { diff --git a/vlib/v/checker/tests/array_sort_err.out b/vlib/v/checker/tests/array_sort_err.out new file mode 100644 index 0000000000..bc399e9303 --- /dev/null +++ b/vlib/v/checker/tests/array_sort_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/array_sort_err.vv b/vlib/v/checker/tests/array_sort_err.vv new file mode 100644 index 0000000000..01e5ebcecf --- /dev/null +++ b/vlib/v/checker/tests/array_sort_err.vv @@ -0,0 +1,4 @@ +fn main() { + mut arr := [3, 2, 1] + arr.sort(a < b, a) +}