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

gen: fix mutable map generation (#7251)

This commit is contained in:
Andréas Livet 2020-12-11 04:48:55 +01:00 committed by GitHub
parent 04346e7ba5
commit 18ec24dd53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View File

@ -10,6 +10,7 @@ particularly useful for checking that errors are printed.
Tip: use `v -cc tcc` when compiling tests for speed. Tip: use `v -cc tcc` when compiling tests for speed.
## `v test-compiler` ## `v test-compiler`
This builds and tests: This builds and tests:

View File

@ -1235,16 +1235,17 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
idx := g.new_tmp_var() idx := g.new_tmp_var()
atmp := g.new_tmp_var() atmp := g.new_tmp_var()
atmp_styp := g.typ(it.cond_type) atmp_styp := g.typ(it.cond_type)
arw_or_pt := if it.cond_type.nr_muls() > 0 { '->' } else { '.' }
g.write('$atmp_styp $atmp = ') g.write('$atmp_styp $atmp = ')
g.expr(it.cond) g.expr(it.cond)
g.writeln(';') g.writeln(';')
g.writeln('for (int $idx = 0; $idx < ${atmp}.key_values.len; ++$idx) {') g.writeln('for (int $idx = 0; $idx < $atmp${arw_or_pt}key_values.len; ++$idx) {')
// TODO: don't have this check when the map has no deleted elements // TODO: don't have this check when the map has no deleted elements
g.writeln('\tif (!DenseArray_has_index(&${atmp}.key_values, $idx)) {continue;}') g.writeln('\tif (!DenseArray_has_index(&$atmp${arw_or_pt}key_values, $idx)) {continue;}')
if it.key_var != '_' { if it.key_var != '_' {
key_styp := g.typ(it.key_type) key_styp := g.typ(it.key_type)
key := c_name(it.key_var) key := c_name(it.key_var)
g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&${atmp}.key_values, $idx);') g.writeln('\t$key_styp $key = /*key*/ *($key_styp*)DenseArray_key(&$atmp${arw_or_pt}key_values, $idx);')
// TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types: // TODO: analyze whether it.key_type has a .clone() method and call .clone() for all types:
if it.key_type == table.string_type { if it.key_type == table.string_type {
g.writeln('\t$key = string_clone($key);') g.writeln('\t$key = string_clone($key);')
@ -1260,7 +1261,7 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
val_styp := g.typ(it.val_type) val_styp := g.typ(it.val_type)
g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)') g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)')
} }
g.writeln('DenseArray_value(&${atmp}.key_values, $idx));') g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));')
} }
g.stmts(it.stmts) g.stmts(it.stmts)
if it.key_type == table.string_type && !g.is_builtin_mod { if it.key_type == table.string_type && !g.is_builtin_mod {

View File

@ -0,0 +1,20 @@
fn print_all(mut m map[string]string) {
for k, v in m {
println(k)
println(v)
m[k] = 'foo'
}
assert m['test'] == 'foo'
}
fn test_map_mutable_arg() {
mut m := map[string]string{}
m['test'] = 'test'
for k, v in m {
println(k)
println(v)
}
print_all(mut m)
}