From 460f4523aaf855260596bb1c52fb994d7a1e7ac8 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 15 Nov 2021 03:00:22 +0800 Subject: [PATCH] checker: check array pop immutable (#12458) --- vlib/v/checker/checker.v | 1 + vlib/v/checker/tests/array_pop_immutable_err.out | 7 +++++++ vlib/v/checker/tests/array_pop_immutable_err.vv | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 vlib/v/checker/tests/array_pop_immutable_err.out create mode 100644 vlib/v/checker/tests/array_pop_immutable_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 25f778e1f5..20a6a7a229 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2493,6 +2493,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as } else if method_name in ['first', 'last', 'pop'] { node.return_type = array_info.elem_type if method_name == 'pop' { + c.fail_if_immutable(node.left) node.receiver_type = left_type.ref() } else { node.receiver_type = left_type diff --git a/vlib/v/checker/tests/array_pop_immutable_err.out b/vlib/v/checker/tests/array_pop_immutable_err.out new file mode 100644 index 0000000000..6fd8dcad3e --- /dev/null +++ b/vlib/v/checker/tests/array_pop_immutable_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/array_pop_immutable_err.vv:5:2: error: `a` is immutable, declare it with `mut` to make it mutable + 3 | dump(a) + 4 | + 5 | a.pop() + | ^ + 6 | dump(a) + 7 | } diff --git a/vlib/v/checker/tests/array_pop_immutable_err.vv b/vlib/v/checker/tests/array_pop_immutable_err.vv new file mode 100644 index 0000000000..789466b822 --- /dev/null +++ b/vlib/v/checker/tests/array_pop_immutable_err.vv @@ -0,0 +1,7 @@ +fn main() { + a := [1,2,3] + dump(a) + + a.pop() + dump(a) +}