Skip to content

Commit 409f6fb

Browse files
authored
checker: check array builtin method calls, that do need a mutable receiver, but are called on an immutable one (fix #22850) (#22853)
1 parent 58e5799 commit 409f6fb

File tree

5 files changed

+92
-14
lines changed

5 files changed

+92
-14
lines changed

vlib/v/checker/fn.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,6 +3301,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
33013301
return ast.void_type
33023302
}
33033303
}
3304+
c.check_for_mut_receiver(mut node.left)
33043305
info := left_sym.info as ast.Array
33053306
mut arg_expr := if method_name == 'insert' {
33063307
node.args[1].expr
@@ -3362,6 +3363,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
33623363
}
33633364
}
33643365
if method_name == 'sort_with_compare' {
3366+
c.check_for_mut_receiver(mut node.left)
33653367
node.return_type = ast.void_type
33663368
node.receiver_type = node.left_type.ref()
33673369
} else {
@@ -3773,6 +3775,7 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
37733775
c.check_expr_option_or_result_call(arg.expr, c.expr(mut arg.expr))
37743776
}
37753777
if method_name == 'sort_with_compare' {
3778+
c.check_for_mut_receiver(mut node.left)
37763779
node.return_type = ast.void_type
37773780
node.receiver_type = node.left_type.ref()
37783781
} else {
@@ -3787,6 +3790,7 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
37873790
if method_name == 'reverse' {
37883791
node.return_type = node.left_type
37893792
} else {
3793+
c.check_for_mut_receiver(mut node.left)
37903794
node.return_type = ast.void_type
37913795
}
37923796
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
vlib/v/checker/tests/array_method_call_immutable_err.vv:7:2: error: `a2` is immutable, declare it with `mut` to make it mutable
2+
5 | a3 := ['aa', 'bb']
3+
6 |
4+
7 | a2.prepend(a1)
5+
| ~~
6+
8 | a1.insert(0, a0)
7+
9 | a3.sort_with_compare(fn (a &string, b &string) int {
8+
vlib/v/checker/tests/array_method_call_immutable_err.vv:8:2: error: `a1` is immutable, declare it with `mut` to make it mutable
9+
6 |
10+
7 | a2.prepend(a1)
11+
8 | a1.insert(0, a0)
12+
| ~~
13+
9 | a3.sort_with_compare(fn (a &string, b &string) int {
14+
10 | if a < b {
15+
vlib/v/checker/tests/array_method_call_immutable_err.vv:9:2: error: `a3` is immutable, declare it with `mut` to make it mutable
16+
7 | a2.prepend(a1)
17+
8 | a1.insert(0, a0)
18+
9 | a3.sort_with_compare(fn (a &string, b &string) int {
19+
| ~~
20+
10 | if a < b {
21+
11 | return -1
22+
vlib/v/checker/tests/array_method_call_immutable_err.vv:18:2: error: `a3` is immutable, declare it with `mut` to make it mutable
23+
16 | return 0
24+
17 | })
25+
18 | a3.delete(0)
26+
| ~~
27+
19 | a2.pop()
28+
20 | a3.sort()
29+
vlib/v/checker/tests/array_method_call_immutable_err.vv:19:2: error: `a2` is immutable, declare it with `mut` to make it mutable
30+
17 | })
31+
18 | a3.delete(0)
32+
19 | a2.pop()
33+
| ~~
34+
20 | a3.sort()
35+
21 |
36+
vlib/v/checker/tests/array_method_call_immutable_err.vv:20:2: error: `a3` is immutable, declare it with `mut` to make it mutable
37+
18 | a3.delete(0)
38+
19 | a2.pop()
39+
20 | a3.sort()
40+
| ~~
41+
21 |
42+
22 | b0 := ['aa', 'bb']!
43+
vlib/v/checker/tests/array_method_call_immutable_err.vv:23:2: error: `b0` is immutable, declare it with `mut` to make it mutable
44+
21 |
45+
22 | b0 := ['aa', 'bb']!
46+
23 | b0.sort_with_compare(fn (a &string, b &string) int {
47+
| ~~
48+
24 | if a < b {
49+
25 | return -1
50+
vlib/v/checker/tests/array_method_call_immutable_err.vv:32:2: error: `b0` is immutable, declare it with `mut` to make it mutable
51+
30 | return 0
52+
31 | })
53+
32 | b0.sort()
54+
| ~~
55+
33 | }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fn main() {
2+
a0 := [1, 2]
3+
a1 := [3, 4]
4+
a2 := [5, 6]
5+
a3 := ['aa', 'bb']
6+
7+
a2.prepend(a1)
8+
a1.insert(0, a0)
9+
a3.sort_with_compare(fn (a &string, b &string) int {
10+
if a < b {
11+
return -1
12+
}
13+
if a > b {
14+
return 1
15+
}
16+
return 0
17+
})
18+
a3.delete(0)
19+
a2.pop()
20+
a3.sort()
21+
22+
b0 := ['aa', 'bb']!
23+
b0.sort_with_compare(fn (a &string, b &string) int {
24+
if a < b {
25+
return -1
26+
}
27+
if a > b {
28+
return 1
29+
}
30+
return 0
31+
})
32+
b0.sort()
33+
}

vlib/v/checker/tests/array_pop_immutable_err.out

Lines changed: 0 additions & 7 deletions
This file was deleted.

vlib/v/checker/tests/array_pop_immutable_err.vv

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)