From 923ef733c023a302a98dd02bd0da9258c673dde1 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 4 Sep 2021 19:34:29 +0800 Subject: [PATCH] checker: disallow array sort with fancy args for now (#11388) --- vlib/v/checker/checker.v | 9 +++++++++ vlib/v/checker/tests/array_fancy_sort_err.out | 8 ++++++++ vlib/v/checker/tests/array_fancy_sort_err.vv | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 vlib/v/checker/tests/array_fancy_sort_err.out create mode 100644 vlib/v/checker/tests/array_fancy_sort_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2542a03c88..c6ec90b7e1 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2555,6 +2555,15 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as } else if left_name == right_name { c.error('`.sort()` cannot use same argument', node.pos) } + if (node.args[0].expr.left !is ast.Ident + && node.args[0].expr.left !is ast.SelectorExpr + && node.args[0].expr.left !is ast.IndexExpr) + || (node.args[0].expr.right !is ast.Ident + && node.args[0].expr.right !is ast.SelectorExpr + && node.args[0].expr.right !is ast.IndexExpr) { + c.error('`.sort()` can only use ident, index or selector as argument, \ne.g. `arr.sort(a < b)`, `arr.sort(a.id < b.id)`, `arr.sort(a[0] < b[0])`', + node.pos) + } } else { c.error( '`.sort()` requires a `<` or `>` comparison as the first and only argument' + diff --git a/vlib/v/checker/tests/array_fancy_sort_err.out b/vlib/v/checker/tests/array_fancy_sort_err.out new file mode 100644 index 0000000000..c61409c1aa --- /dev/null +++ b/vlib/v/checker/tests/array_fancy_sort_err.out @@ -0,0 +1,8 @@ +vlib/v/checker/tests/array_fancy_sort_err.vv:6:8: error: `.sort()` can only use ident, index or selector as argument, +e.g. `arr.sort(a < b)`, `arr.sort(a.id < b.id)`, `arr.sort(a[0] < b[0])` + 4 | text := os.read_file(os.args[0]) ? + 5 | mut lines := text.split_into_lines() + 6 | lines.sort(a.split('/').last() < b.split('/').last()) + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 7 | println(lines.join('\n')) + 8 | } diff --git a/vlib/v/checker/tests/array_fancy_sort_err.vv b/vlib/v/checker/tests/array_fancy_sort_err.vv new file mode 100644 index 0000000000..3c05a5e6bb --- /dev/null +++ b/vlib/v/checker/tests/array_fancy_sort_err.vv @@ -0,0 +1,8 @@ +import os + +fn main() { + text := os.read_file(os.args[0]) ? + mut lines := text.split_into_lines() + lines.sort(a.split('/').last() < b.split('/').last()) + println(lines.join('\n')) +}