From 9355c60b4d1e7d0fb259ae64ab0cce76d66f9352 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 14 Oct 2020 13:34:49 +0200 Subject: [PATCH] cgen: fix for mut val and enable the test --- vlib/v/gen/cgen.v | 18 ++++++++++-------- vlib/v/tests/for_loops_test.v | 2 -- vlib/v/tests/valgrind/1.strings_and_arrays.v | 3 --- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 9176234b28..eb06e092fc 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1060,26 +1060,27 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { styp := g.typ(it.val_type) val_sym := g.table.get_type_symbol(it.val_type) cond_type_is_ptr := it.cond_type.is_ptr() - atmp := g.new_tmp_var() - atmp_type := if cond_type_is_ptr { 'array *' } else { 'array' } - g.write('$atmp_type $atmp = ') + tmp := g.new_tmp_var() + tmp_type := if cond_type_is_ptr { 'array *' } else { 'array' } + g.write('$tmp_type $tmp = ') g.expr(it.cond) g.writeln(';') i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } 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 val_sym.kind == .function { g.write('\t') 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 { // 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 // `int* val = ((int**)arr.data)[i];` - 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 } else { styp + '*' } + 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) @@ -3503,6 +3504,7 @@ fn (mut g Gen) return_statement(node ast.Return, af bool) { if af { // free the tmp arg expr if we have one before the return g.autofree_call_postgen() + g.strs_to_free = [] } styp := g.typ(g.fn_decl.return_type) g.writeln('return *($styp*)&$tmp;') diff --git a/vlib/v/tests/for_loops_test.v b/vlib/v/tests/for_loops_test.v index acc6fbbf08..0072101349 100644 --- a/vlib/v/tests/for_loops_test.v +++ b/vlib/v/tests/for_loops_test.v @@ -56,12 +56,10 @@ fn test_for_char_in_map() { } fn test_mut_for() { - /* mut vals := [1, 2, 3] for mut val in vals { (*val)++ } assert vals == [2, 3, 4] println(vals) - */ } diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 316b5f8799..496241eb68 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -1,6 +1,3 @@ -import os - -// import time fn return_array(array_arg []string) []int { // array argument must not be freed s := [1, 2, 3] // escaping array must not be freed return s