1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sort: require a mutable receiver

This commit is contained in:
Delyan Angelov 2020-08-21 21:17:55 +03:00
parent 93e6c3df6a
commit 06967d9297
3 changed files with 23 additions and 0 deletions

View File

@ -872,6 +872,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
if is_filter_map {
scope.update_var_type('it', array_info.elem_type)
} else if is_sort {
c.fail_if_immutable(call_expr.left)
scope.update_var_type('a', array_info.elem_type)
scope.update_var_type('b', array_info.elem_type)
// Verify `.sort(a < b)`

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/sort_method_called_on_immutable_receiver.v:2:2: error: `a` is immutable, declare it with `mut` to make it mutable
1 | fn abc (a []int) {
2 | a.sort()
| ^
3 | }
4 |
vlib/v/checker/tests/sort_method_called_on_immutable_receiver.v:7:2: error: `a` is immutable, declare it with `mut` to make it mutable
5 | fn main() {
6 | a := [2,30,10,20,1]
7 | a.sort(a>b)
| ^
8 | eprintln(' a: $a')
9 | }

View File

@ -0,0 +1,9 @@
fn abc (a []int) {
a.sort()
}
fn main() {
a := [2,30,10,20,1]
a.sort(a>b)
eprintln(' a: $a')
}