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

cgen: fix for_in_mut_val for maps (#8221)

This commit is contained in:
yuyi 2021-01-21 19:44:22 +08:00 committed by GitHub
parent 3ecbf78707
commit f13ba3a9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -1362,11 +1362,21 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
g.write(' = (*(voidptr*)')
} else {
val_styp := g.typ(it.val_type)
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)')
if it.val_type.is_ptr() {
g.write('\t$val_styp ${c_name(it.val_var)} = &(*($val_styp)')
} else {
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)')
}
}
g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));')
}
if it.val_is_mut {
g.for_in_mul_val_name = it.val_var
}
g.stmts(it.stmts)
if it.val_is_mut {
g.for_in_mul_val_name = ''
}
if it.key_type == table.string_type && !g.is_builtin_mod {
// g.writeln('string_free(&$key);')
}

View File

@ -49,3 +49,12 @@ fn test_for_in_mut_val_of_map() {
println(m)
assert '$m' == "{'hello': [2, 4, 6]}"
}
fn test_for_in_mut_val_of_map_direct() {
mut m := {'foo': 1, 'bar': 2}
for _, mut j in m {
j = 3
}
println(m)
assert '$m' == "{'foo': 3, 'bar': 3}"
}