checker: check fn_call().sort() (fix #11040) (#11056)

pull/11062/head
yuyi 2021-08-05 02:23:22 +08:00 committed by GitHub
parent f59119485a
commit 11784279ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -2465,6 +2465,10 @@ fn (mut c Checker) array_builtin_method_call(mut call_expr ast.CallExpr, left_ty
// position of `it` doesn't matter
scope_register_it(mut call_expr.scope, call_expr.pos, elem_typ)
} else if method_name == 'sort' {
if call_expr.left is ast.CallExpr {
c.error('the `sort()` method can be called only on mutable receivers, but `$call_expr.left` is a call expression',
call_expr.pos)
}
c.fail_if_immutable(call_expr.left)
// position of `a` and `b` doesn't matter, they're the same
scope_register_a_b(mut call_expr.scope, call_expr.pos, elem_typ)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_return_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 | }

View File

@ -0,0 +1,7 @@
fn ret_array() []int {
return [1, 3, 2]
}
fn main() {
ret_array().sort()
}