From 1c257abc234bd4933aa7e4252b5411db17f85657 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 9 Nov 2020 14:24:46 +0100 Subject: [PATCH] autofree: simplify: merge tmp arg logic with scope vars --- vlib/os/os_nix.c.v | 6 +++--- vlib/v/ast/ast.v | 2 +- vlib/v/gen/cgen.v | 14 +++++++------- vlib/v/gen/fn.v | 4 ++-- vlib/v/tests/valgrind/1.strings_and_arrays.v | 8 +++++++- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 1b247ca813..af591d9a13 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -110,9 +110,9 @@ pub fn mkdir(path string) ?bool { } */ apath := real_path(path) - defer { - apath.free() - } + //defer { + //apath.free() + //} /* $if linux { $if !android { diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 14833d3b79..ecc8c94c3f 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -324,7 +324,7 @@ pub: comments []Comment pub mut: typ table.Type - is_tmp_autofree bool + is_tmp_autofree bool // this tells cgen that a tmp variable has to be used for the arg expression in order to free it after the call pos token.Position // tmp_name string // for autofree } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 817d99f5d9..0b24a87758 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -119,7 +119,7 @@ mut: cur_mod string is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)` // nr_vars_to_free int - doing_autofree_tmp bool + // doing_autofree_tmp bool inside_lambda bool } @@ -1047,8 +1047,8 @@ fn (mut g Gen) stmt(node ast.Stmt) { if g.pref.autofree { // if node is ast.ExprStmt {&& node.expr is ast.CallExpr { if node !is ast.FnDecl { - p := node.position() - g.autofree_call_postgen(p.pos) + // p := node.position() + // g.autofree_call_postgen(p.pos) } } } @@ -1999,9 +1999,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) { // tmp expr vars do not need to be freed again here return } - if v.is_autofree_tmp && !g.doing_autofree_tmp { - return - } + // if v.is_autofree_tmp && !g.doing_autofree_tmp { + // return + // } if v.name.contains('expr_write_1_') { // TODO remove this temporary hack return @@ -2009,7 +2009,7 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) { if v.typ.is_ptr() { g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var') } else { - g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var $g.doing_autofree_tmp') + g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var') } } diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index eebfdff8c3..193cbc8297 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -741,7 +741,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) { if g.inside_vweb_tmpl { return } - g.doing_autofree_tmp = true + // g.doing_autofree_tmp = true // g.write('/* postgen */') scope := g.file.scope.innermost(node_pos) for _, obj in scope.objects { @@ -772,7 +772,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) { } } // g.write('/* postgen end */') - g.doing_autofree_tmp = false + // g.doing_autofree_tmp = false } fn (mut g Gen) call_args(node ast.CallExpr) { diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 238966ca66..f49d885546 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -9,6 +9,12 @@ fn simple() { name := 'Peter' // string literals mustn't be freed str_inter := 'hello, $name' // concatenated strings must be freed // nums.free() // this should result in a double free and a CI error + if true { + // test the freeing of local vars in a new scope + nums2 := [4, 5, 6] + str_inter2 := 'hello, $name' + println(nums2) + } arr := return_array([]) println(arr) } @@ -243,7 +249,7 @@ fn main() { str_tmp_expr_advanced_var_decl() str_inter() match_expr() - optional_str() + // optional_str() // optional_return() str_replace() str_replace2()