1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix compiler panic for for k, mut val in app.field[x].values { (#17468)

This commit is contained in:
Delyan Angelov 2023-03-02 15:40:14 +02:00 committed by GitHub
parent 7497d8457f
commit 9fc2860074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 6 deletions

View File

@ -195,15 +195,18 @@ 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 := node.cond.root_ident() {
if root_ident.kind != .unresolved {
if !(root_ident.obj as ast.Var).is_mut {
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)
}
}
}
}
}
else {}
}
} else if node.val_is_ref {

View File

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