sort: require a mutable receiver
parent
93e6c3df6a
commit
06967d9297
|
@ -872,6 +872,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
|
||||||
if is_filter_map {
|
if is_filter_map {
|
||||||
scope.update_var_type('it', array_info.elem_type)
|
scope.update_var_type('it', array_info.elem_type)
|
||||||
} else if is_sort {
|
} else if is_sort {
|
||||||
|
c.fail_if_immutable(call_expr.left)
|
||||||
scope.update_var_type('a', array_info.elem_type)
|
scope.update_var_type('a', array_info.elem_type)
|
||||||
scope.update_var_type('b', array_info.elem_type)
|
scope.update_var_type('b', array_info.elem_type)
|
||||||
// Verify `.sort(a < b)`
|
// Verify `.sort(a < b)`
|
||||||
|
|
|
@ -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 | }
|
|
@ -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')
|
||||||
|
}
|
Loading…
Reference in New Issue