cgen: fix codegen for `for k,v in map_of_pointers {`

pull/12486/head
Delyan Angelov 2021-11-16 16:07:39 +02:00
parent 045579fd8a
commit 3fab0a5d05
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 22 additions and 1 deletions

View File

@ -1221,6 +1221,9 @@ pub fn (mut t Table) complete_interface_check() {
if idecl.fields.len > info.fields.len { if idecl.fields.len > info.fields.len {
continue continue
} }
if idecl.typ == 0 {
continue
}
// empty interface only generate type cast functions of the current module // empty interface only generate type cast functions of the current module
if idecl.methods.len == 0 && idecl.fields.len == 0 if idecl.methods.len == 0 && idecl.fields.len == 0
&& tsym.mod != t.get_type_symbol(idecl.typ).mod { && tsym.mod != t.get_type_symbol(idecl.typ).mod {

View File

@ -2120,10 +2120,14 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
} else { } else {
val_styp := g.typ(node.val_type) val_styp := g.typ(node.val_type)
if node.val_type.is_ptr() { if node.val_type.is_ptr() {
if node.val_is_mut {
g.write('$val_styp ${c_name(node.val_var)} = &(*($val_styp)') g.write('$val_styp ${c_name(node.val_var)} = &(*($val_styp)')
} else { } else {
g.write('$val_styp ${c_name(node.val_var)} = (*($val_styp*)') g.write('$val_styp ${c_name(node.val_var)} = (*($val_styp*)')
} }
} else {
g.write('$val_styp ${c_name(node.val_var)} = (*($val_styp*)')
}
g.writeln('DenseArray_value(&$cond_var${arw_or_pt}key_values, $idx));') g.writeln('DenseArray_value(&$cond_var${arw_or_pt}key_values, $idx));')
} }
} }

View File

@ -0,0 +1,14 @@
fn test_for_in_map_of_pointers() {
i := 123456
pi := &i
println('pi: $pi')
mut my_map := map[int]&int{}
my_map[0] = pi
for k, wrong_value in my_map {
good_value := my_map[k]
println('k: $k \n wrong_value: $wrong_value \n good_value: $good_value')
up_good_value := voidptr(good_value).hex_full()
up_wrong_value := voidptr(wrong_value).hex_full()
assert up_good_value == up_wrong_value
}
}