Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check array builtin method call immutable errors (fix #22850) #22853

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
return ast.void_type
}
}
c.check_for_mut_receiver(mut node.left)
info := left_sym.info as ast.Array
mut arg_expr := if method_name == 'insert' {
node.args[1].expr
Expand Down Expand Up @@ -3362,6 +3363,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
}
}
if method_name == 'sort_with_compare' {
c.check_for_mut_receiver(mut node.left)
node.return_type = ast.void_type
node.receiver_type = node.left_type.ref()
} else {
Expand Down Expand Up @@ -3773,6 +3775,7 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
c.check_expr_option_or_result_call(arg.expr, c.expr(mut arg.expr))
}
if method_name == 'sort_with_compare' {
c.check_for_mut_receiver(mut node.left)
node.return_type = ast.void_type
node.receiver_type = node.left_type.ref()
} else {
Expand All @@ -3787,6 +3790,7 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t
if method_name == 'reverse' {
node.return_type = node.left_type
} else {
c.check_for_mut_receiver(mut node.left)
node.return_type = ast.void_type
}
}
Expand Down
55 changes: 55 additions & 0 deletions vlib/v/checker/tests/array_method_call_immutable_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
vlib/v/checker/tests/array_method_call_immutable_err.vv:7:2: error: `a2` is immutable, declare it with `mut` to make it mutable
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 {
vlib/v/checker/tests/array_method_call_immutable_err.vv:8:2: error: `a1` is immutable, declare it with `mut` to make it mutable
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 {
vlib/v/checker/tests/array_method_call_immutable_err.vv:9:2: error: `a3` is immutable, declare it with `mut` to make it mutable
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
vlib/v/checker/tests/array_method_call_immutable_err.vv:18:2: error: `a3` is immutable, declare it with `mut` to make it mutable
16 | return 0
17 | })
18 | a3.delete(0)
| ~~
19 | a2.pop()
20 | a3.sort()
vlib/v/checker/tests/array_method_call_immutable_err.vv:19:2: error: `a2` is immutable, declare it with `mut` to make it mutable
17 | })
18 | a3.delete(0)
19 | a2.pop()
| ~~
20 | a3.sort()
21 |
vlib/v/checker/tests/array_method_call_immutable_err.vv:20:2: error: `a3` is immutable, declare it with `mut` to make it mutable
18 | a3.delete(0)
19 | a2.pop()
20 | a3.sort()
| ~~
21 |
22 | b0 := ['aa', 'bb']!
vlib/v/checker/tests/array_method_call_immutable_err.vv:23:2: error: `b0` is immutable, declare it with `mut` to make it mutable
21 |
22 | b0 := ['aa', 'bb']!
23 | b0.sort_with_compare(fn (a &string, b &string) int {
| ~~
24 | if a < b {
25 | return -1
vlib/v/checker/tests/array_method_call_immutable_err.vv:32:2: error: `b0` is immutable, declare it with `mut` to make it mutable
30 | return 0
31 | })
32 | b0.sort()
| ~~
33 | }
33 changes: 33 additions & 0 deletions vlib/v/checker/tests/array_method_call_immutable_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
fn main() {
a0 := [1, 2]
a1 := [3, 4]
a2 := [5, 6]
a3 := ['aa', 'bb']

a2.prepend(a1)
a1.insert(0, a0)
a3.sort_with_compare(fn (a &string, b &string) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
})
a3.delete(0)
a2.pop()
a3.sort()

b0 := ['aa', 'bb']!
b0.sort_with_compare(fn (a &string, b &string) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
})
b0.sort()
}
7 changes: 0 additions & 7 deletions vlib/v/checker/tests/array_pop_immutable_err.out

This file was deleted.

7 changes: 0 additions & 7 deletions vlib/v/checker/tests/array_pop_immutable_err.vv

This file was deleted.

Loading