From 10b9ea32583a8eaa7ecff248a299a1cd53e2e22f Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Thu, 10 Jun 2021 12:18:14 -0400 Subject: [PATCH] checker: add an error for assigning to array slices `a[..2] = [0, 0]` (#10412) --- vlib/v/checker/checker.v | 5 +++++ vlib/v/checker/tests/slice_reassignment.out | 7 +++++++ vlib/v/checker/tests/slice_reassignment.vv | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 vlib/v/checker/tests/slice_reassignment.out create mode 100644 vlib/v/checker/tests/slice_reassignment.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 54beed80e0..a6456c2322 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3268,6 +3268,11 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { if left is ast.CallExpr { c.error('cannot call function `${left.name}()` on the left side of an assignment', left.pos) + } else if left is ast.IndexExpr { + if left.index is ast.RangeExpr { + c.error('cannot reassign using range expression on the left side of an assignment', + left.pos) + } } is_blank_ident := left.is_blank_ident() mut left_type := ast.void_type diff --git a/vlib/v/checker/tests/slice_reassignment.out b/vlib/v/checker/tests/slice_reassignment.out new file mode 100644 index 0000000000..7ab478fa4c --- /dev/null +++ b/vlib/v/checker/tests/slice_reassignment.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/slice_reassignment.vv:3:5: error: cannot reassign using range expression on the left side of an assignment + 1 | fn main() { + 2 | mut arr := [1, 2, 3, 4, 5] + 3 | arr[..2] = [0, 0] + | ~~~~~ + 4 | println(arr) + 5 | } diff --git a/vlib/v/checker/tests/slice_reassignment.vv b/vlib/v/checker/tests/slice_reassignment.vv new file mode 100644 index 0000000000..602bd647b7 --- /dev/null +++ b/vlib/v/checker/tests/slice_reassignment.vv @@ -0,0 +1,5 @@ +fn main() { + mut arr := [1, 2, 3, 4, 5] + arr[..2] = [0, 0] + println(arr) +}