From 31c01fe9563d9d367c7e5f7b98948a81aeae3b63 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 26 Oct 2024 18:20:17 +0800 Subject: [PATCH] checker: add tests for checking the new errors for fixed arrays .sort() calls (#22656) --- vlib/v/checker/tests/fixed_array_sort_err.out | 45 +++++++++++++++++++ vlib/v/checker/tests/fixed_array_sort_err.vv | 7 +++ .../tests/fn_return_fixed_array_sort_err.out | 12 +++++ .../tests/fn_return_fixed_array_sort_err.vv | 7 +++ 4 files changed, 71 insertions(+) create mode 100644 vlib/v/checker/tests/fixed_array_sort_err.out create mode 100644 vlib/v/checker/tests/fixed_array_sort_err.vv create mode 100644 vlib/v/checker/tests/fn_return_fixed_array_sort_err.out create mode 100644 vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv diff --git a/vlib/v/checker/tests/fixed_array_sort_err.out b/vlib/v/checker/tests/fixed_array_sort_err.out new file mode 100644 index 000000000..860c9fd9c --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_sort_err.out @@ -0,0 +1,45 @@ +vlib/v/checker/tests/fixed_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 | arr.sort(a == b) + 5 | arr.sort(a > a) +vlib/v/checker/tests/fixed_array_sort_err.vv:4:6: error: `.sort()` can only use `<` or `>` comparison + 2 | mut arr := [3, 2, 1]! + 3 | arr.sort(a < b, a) + 4 | arr.sort(a == b) + | ~~~~~~~~~~~~ + 5 | arr.sort(a > a) + 6 | arr.sort(c > d) +vlib/v/checker/tests/fixed_array_sort_err.vv:5:6: error: `.sort()` cannot use same argument + 3 | arr.sort(a < b, a) + 4 | arr.sort(a == b) + 5 | arr.sort(a > a) + | ~~~~~~~~~~~ + 6 | arr.sort(c > d) + 7 | } +vlib/v/checker/tests/fixed_array_sort_err.vv:6:11: error: can not access external variable `c` + 4 | arr.sort(a == b) + 5 | arr.sort(a > a) + 6 | arr.sort(c > d) + | ^ + 7 | } +vlib/v/checker/tests/fixed_array_sort_err.vv:6:6: error: `.sort()` can only use `a` or `b` as argument, e.g. `arr.sort(a < b)` + 4 | arr.sort(a == b) + 5 | arr.sort(a > a) + 6 | arr.sort(c > d) + | ~~~~~~~~~~~ + 7 | } +vlib/v/checker/tests/fixed_array_sort_err.vv:6:11: error: undefined ident: `c` + 4 | arr.sort(a == b) + 5 | arr.sort(a > a) + 6 | arr.sort(c > d) + | ^ + 7 | } +vlib/v/checker/tests/fixed_array_sort_err.vv:6:15: error: undefined ident: `d` + 4 | arr.sort(a == b) + 5 | arr.sort(a > a) + 6 | arr.sort(c > d) + | ^ + 7 | } diff --git a/vlib/v/checker/tests/fixed_array_sort_err.vv b/vlib/v/checker/tests/fixed_array_sort_err.vv new file mode 100644 index 000000000..b20961b33 --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_sort_err.vv @@ -0,0 +1,7 @@ +fn main() { + mut arr := [3, 2, 1]! + arr.sort(a < b, a) + arr.sort(a == b) + arr.sort(a > a) + arr.sort(c > d) +} diff --git a/vlib/v/checker/tests/fn_return_fixed_array_sort_err.out b/vlib/v/checker/tests/fn_return_fixed_array_sort_err.out new file mode 100644 index 000000000..8b5d0e9a9 --- /dev/null +++ b/vlib/v/checker/tests/fn_return_fixed_array_sort_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv:6:14: error: the `sort()` method can be called only on mutable receivers, but `ret_array()` is a call expression + 4 | + 5 | fn main() { + 6 | ret_array().sort() + | ~~~~~~ + 7 | } +vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv:6:2: error: cannot pass expression as `mut` + 4 | + 5 | fn main() { + 6 | ret_array().sort() + | ~~~~~~~~~~~ + 7 | } diff --git a/vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv b/vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv new file mode 100644 index 000000000..53b756820 --- /dev/null +++ b/vlib/v/checker/tests/fn_return_fixed_array_sort_err.vv @@ -0,0 +1,7 @@ +fn ret_array() [3]int { + return [1, 3, 2]! +} + +fn main() { + ret_array().sort() +} -- 2.39.5