cgen: fix for mut val and enable the test

pull/6616/head
Alexander Medvednikov 2020-10-14 13:34:49 +02:00
parent 25947a7b23
commit 9355c60b4d
3 changed files with 10 additions and 13 deletions

View File

@ -1060,26 +1060,27 @@ fn (mut g Gen) for_in(it ast.ForInStmt) {
styp := g.typ(it.val_type) styp := g.typ(it.val_type)
val_sym := g.table.get_type_symbol(it.val_type) val_sym := g.table.get_type_symbol(it.val_type)
cond_type_is_ptr := it.cond_type.is_ptr() cond_type_is_ptr := it.cond_type.is_ptr()
atmp := g.new_tmp_var() tmp := g.new_tmp_var()
atmp_type := if cond_type_is_ptr { 'array *' } else { 'array' } tmp_type := if cond_type_is_ptr { 'array *' } else { 'array' }
g.write('$atmp_type $atmp = ') g.write('$tmp_type $tmp = ')
g.expr(it.cond) g.expr(it.cond)
g.writeln(';') g.writeln(';')
i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var }
op_field := if cond_type_is_ptr { '->' } else { '.' } op_field := if cond_type_is_ptr { '->' } else { '.' }
g.writeln('for (int $i = 0; $i < $atmp${op_field}len; ++$i) {') g.writeln('for (int $i = 0; $i < $tmp${op_field}len; ++$i) {')
if it.val_var != '_' { if it.val_var != '_' {
if val_sym.kind == .function { if val_sym.kind == .function {
g.write('\t') g.write('\t')
g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var)) g.write_fn_ptr_decl(val_sym.info as table.FnType, c_name(it.val_var))
g.writeln(' = ((voidptr*)$atmp${op_field}data)[$i];') g.writeln(' = ((voidptr*)$tmp${op_field}data)[$i];')
} else { } else {
// If val is mutable (pointer behind the scenes), we need to generate // If val is mutable (pointer behind the scenes), we need to generate
// `int* val = ((int*)arr.data)[i];` // `int* val = ((int*)arr.data) + i;`
// instead of // instead of
// `int* val = ((int**)arr.data)[i];` // `int* val = ((int**)arr.data)[i];`
styp_right := if it.val_is_mut { styp } else { styp + '*' } // right := if it.val_is_mut { styp } else { styp + '*' }
g.writeln('\t$styp ${c_name(it.val_var)} = (($styp_right)$atmp${op_field}data)[$i];') right := if it.val_is_mut { '(($styp)$tmp${op_field}data) + $i' } else { '(($styp*)$tmp${op_field}data)[$i]' }
g.writeln('\t$styp ${c_name(it.val_var)} = $right;')
} }
} }
g.stmts(it.stmts) g.stmts(it.stmts)
@ -3503,6 +3504,7 @@ fn (mut g Gen) return_statement(node ast.Return, af bool) {
if af { if af {
// free the tmp arg expr if we have one before the return // free the tmp arg expr if we have one before the return
g.autofree_call_postgen() g.autofree_call_postgen()
g.strs_to_free = []
} }
styp := g.typ(g.fn_decl.return_type) styp := g.typ(g.fn_decl.return_type)
g.writeln('return *($styp*)&$tmp;') g.writeln('return *($styp*)&$tmp;')

View File

@ -56,12 +56,10 @@ fn test_for_char_in_map() {
} }
fn test_mut_for() { fn test_mut_for() {
/*
mut vals := [1, 2, 3] mut vals := [1, 2, 3]
for mut val in vals { for mut val in vals {
(*val)++ (*val)++
} }
assert vals == [2, 3, 4] assert vals == [2, 3, 4]
println(vals) println(vals)
*/
} }

View File

@ -1,6 +1,3 @@
import os
// import time
fn return_array(array_arg []string) []int { // array argument must not be freed fn return_array(array_arg []string) []int { // array argument must not be freed
s := [1, 2, 3] // escaping array must not be freed s := [1, 2, 3] // escaping array must not be freed
return s return s