checker: add an error for assigning to array slices `a[..2] = [0, 0]` (#10412)

pull/10418/head
shadowninja55 2021-06-10 12:18:14 -04:00 committed by GitHub
parent 5be982d63e
commit 10b9ea3258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -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

View File

@ -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 | }

View File

@ -0,0 +1,5 @@
fn main() {
mut arr := [1, 2, 3, 4, 5]
arr[..2] = [0, 0]
println(arr)
}