Fix detecting mutable slice including for `const`

pull/13813/head
Nick Treleaven 2022-03-23 12:09:14 +00:00
parent 94262081cc
commit d9394b5041
3 changed files with 17 additions and 2 deletions

View File

@ -792,7 +792,18 @@ pub mut:
name string
kind IdentKind
info IdentInfo
is_mut bool
is_mut bool // if mut *token* is before name. Use `is_mut()` to lookup mut variable
}
pub fn (i &Ident) is_mut() bool {
match i.obj {
Var { return i.obj.is_mut }
ConstField { return false }
GlobalField { return true }
else {
panic('TODO')
}
}
}
pub fn (i &Ident) var_info() IdentVar {

View File

@ -338,7 +338,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
if left_sym.kind == .array && mut left is ast.Ident && mut right is ast.IndexExpr {
sym := c.table.sym(right.left_type)
if sym.kind == .array && mut right.left is ast.Ident {
if left.is_mut && !right.left.is_mut {
if left.is_mut && !right.left.is_mut() {
if right.index is ast.RangeExpr {
c.error('cannot mutate immutable slice', right.left.pos)
} else {

View File

@ -1,7 +1,11 @@
const imm_arr = [10, 20]
fn test_immutable_slice() {
arr := [1, 2, 3, 4, 5]
mut s := arr[1..2]
s[0] = 0
mut s2 := imm_arr[..]
s2[0] = 0
}
fn test_immutable_element() {