checker: disallow array sort with fancy args for now (#11388)

pull/11400/head
yuyi 2021-09-04 19:34:29 +08:00 committed by GitHub
parent 0115a51de4
commit 923ef733c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -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' +

View File

@ -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 | }

View File

@ -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'))
}