Skip to content

Commit

Permalink
checker: fix compiler panic for `for k, mut val in app.field[x].value…
Browse files Browse the repository at this point in the history
…s {` (#17468)
  • Loading branch information
spytheman committed Mar 2, 2023
1 parent 7497d84 commit 9fc2860
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 9 additions & 6 deletions vlib/v/checker/for.v
Expand Up @@ -195,12 +195,15 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
c.error('map literal is immutable, it cannot be changed', node.cond.pos)
}
ast.SelectorExpr {
root_ident := node.cond.root_ident() or { node.cond.expr as ast.Ident }
if root_ident.kind != .unresolved {
if !(root_ident.obj as ast.Var).is_mut {
sym2 := c.table.sym(root_ident.obj.typ)
c.error('field `${sym2.name}.${node.cond.field_name}` is immutable, it cannot be changed',
node.cond.pos)
if root_ident := node.cond.root_ident() {
if root_ident.kind != .unresolved {
if var := node.scope.find_var(root_ident.name) {
if !var.is_mut {
sym2 := c.table.sym(root_ident.obj.typ)
c.error('field `${sym2.name}.${node.cond.field_name}` is immutable, it cannot be changed',
node.cond.pos)
}
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/for_in_mut_mutable_app_field_test.v
@@ -0,0 +1,20 @@
struct Item {
mut:
field []int
}

struct App {
mut:
index [][]Item
}

fn test_for_in_struct_with_mutable_array_field_indexed_several_times_in_the_loop_condition_part() {
mut app, x, y := App{[[Item{
field: [1, 2, 3]
}]]}, 0, 0
for i, mut v in app.index[x][y].field {
assert v != 0
v = 555
}
assert app.index[x][y] == Item{[555, 555, 555]}
}

0 comments on commit 9fc2860

Please sign in to comment.