From 063dfa0ab9ce5412b85eb5283734ed3c64669d76 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 2 May 2023 16:48:40 -0300 Subject: [PATCH] checker: missing mutability check for array.delete calls (#18096) --- vlib/v/checker/fn.v | 1 + vlib/v/checker/tests/array_delete_imut_err.out | 7 +++++++ vlib/v/checker/tests/array_delete_imut_err.vv | 7 +++++++ vlib/v/checker/tests/array_delete_print_err.out | 4 ++-- vlib/v/checker/tests/array_delete_print_err.vv | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/array_delete_imut_err.out create mode 100644 vlib/v/checker/tests/array_delete_imut_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index c72fe9bf57..1780668ec3 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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 diff --git a/vlib/v/checker/tests/array_delete_imut_err.out b/vlib/v/checker/tests/array_delete_imut_err.out new file mode 100644 index 0000000000..28ebcb7902 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_imut_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/array_delete_imut_err.vv b/vlib/v/checker/tests/array_delete_imut_err.vv new file mode 100644 index 0000000000..0c71fb56e6 --- /dev/null +++ b/vlib/v/checker/tests/array_delete_imut_err.vv @@ -0,0 +1,7 @@ +fn main() { + a := [1, 2, 3, 4] + println(a) + + a.delete(1) + println(a) +} diff --git a/vlib/v/checker/tests/array_delete_print_err.out b/vlib/v/checker/tests/array_delete_print_err.out index 94b39565f4..be932c5d87 100644 --- a/vlib/v/checker/tests/array_delete_print_err.out +++ b/vlib/v/checker/tests/array_delete_print_err.out @@ -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)}') | ~~~~~~~~~ diff --git a/vlib/v/checker/tests/array_delete_print_err.vv b/vlib/v/checker/tests/array_delete_print_err.vv index 79268cdeb2..7d85eae095 100644 --- a/vlib/v/checker/tests/array_delete_print_err.vv +++ b/vlib/v/checker/tests/array_delete_print_err.vv @@ -1,5 +1,5 @@ fn main() { - a := []string{} + mut a := []string{} println(a.delete(0)) println('${a.delete(0)}') }