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

checker: missing mutability check for array.delete calls (#18096)

This commit is contained in:
Felipe Pena 2023-05-02 16:48:40 -03:00 committed by GitHub
parent 5631e2f01d
commit 063dfa0ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 3 deletions

View File

@ -2535,6 +2535,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
node.receiver_type = left_type
}
} else if method_name == 'delete' {
c.fail_if_immutable(node.left)
unwrapped_left_sym := c.table.sym(c.unwrap_generic(left_type))
if method := c.table.find_method(unwrapped_left_sym, method_name) {
node.receiver_type = method.receiver_type

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/array_delete_imut_err.vv:5:2: error: `a` is immutable, declare it with `mut` to make it mutable
3 | println(a)
4 |
5 | a.delete(1)
| ^
6 | println(a)
7 | }

View File

@ -0,0 +1,7 @@
fn main() {
a := [1, 2, 3, 4]
println(a)
a.delete(1)
println(a)
}

View File

@ -1,12 +1,12 @@
vlib/v/checker/tests/array_delete_print_err.vv:3:2: error: `println` can not print void expressions
1 | fn main() {
2 | a := []string{}
2 | mut a := []string{}
3 | println(a.delete(0))
| ~~~~~~~~~~~~~~~~~~~~
4 | println('${a.delete(0)}')
5 | }
vlib/v/checker/tests/array_delete_print_err.vv:4:15: error: expression does not return a value
2 | a := []string{}
2 | mut a := []string{}
3 | println(a.delete(0))
4 | println('${a.delete(0)}')
| ~~~~~~~~~

View File

@ -1,5 +1,5 @@
fn main() {
a := []string{}
mut a := []string{}
println(a.delete(0))
println('${a.delete(0)}')
}