Fix detecting mutable slice including for `const`
parent
94262081cc
commit
d9394b5041
|
|
@ -792,7 +792,18 @@ pub mut:
|
||||||
name string
|
name string
|
||||||
kind IdentKind
|
kind IdentKind
|
||||||
info IdentInfo
|
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 {
|
pub fn (i &Ident) var_info() IdentVar {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
if left_sym.kind == .array && mut left is ast.Ident && mut right is ast.IndexExpr {
|
||||||
sym := c.table.sym(right.left_type)
|
sym := c.table.sym(right.left_type)
|
||||||
if sym.kind == .array && mut right.left is ast.Ident {
|
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 {
|
if right.index is ast.RangeExpr {
|
||||||
c.error('cannot mutate immutable slice', right.left.pos)
|
c.error('cannot mutate immutable slice', right.left.pos)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
|
const imm_arr = [10, 20]
|
||||||
|
|
||||||
fn test_immutable_slice() {
|
fn test_immutable_slice() {
|
||||||
arr := [1, 2, 3, 4, 5]
|
arr := [1, 2, 3, 4, 5]
|
||||||
mut s := arr[1..2]
|
mut s := arr[1..2]
|
||||||
s[0] = 0
|
s[0] = 0
|
||||||
|
mut s2 := imm_arr[..]
|
||||||
|
s2[0] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_immutable_element() {
|
fn test_immutable_element() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue