From 423044d4d692541df4df3ab8214638424245f4ea Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 29 Oct 2020 08:59:13 +0100 Subject: [PATCH] autofree: skip lambda for now --- vlib/v/gen/cgen.v | 5 ++++- vlib/v/tests/valgrind/1.strings_and_arrays.v | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 0a5de1efc6..11fc651983 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -119,6 +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 + inside_lambda bool } const ( @@ -2017,7 +2018,7 @@ fn (mut g Gen) expr(node ast.Expr) { // if g.fileis('1.strings') { // println('before:' + node.autofree_pregen) // } - if g.pref.autofree && !g.is_builtin_mod && g.strs_to_free0.len == 0 { // && g.inside_ternary == + if g.pref.autofree && !g.is_builtin_mod && g.strs_to_free0.len == 0 && !g.inside_lambda { // && g.inside_ternary == // if len != 0, that means we are handling call expr inside call expr (arg) // and it'll get messed up here, since it's handled recursively in autofree_call_pregen() // so just skip it @@ -4432,6 +4433,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype table.Type) ?bool { // `nums.map(it % 2 == 0)` fn (mut g Gen) gen_array_map(node ast.CallExpr) { + g.inside_lambda = true tmp := g.new_tmp_var() s := g.go_before_stmt(0) // println('filter s="$s"') @@ -4486,6 +4488,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) { g.writeln('}') g.write(s) g.write(tmp) + g.inside_lambda = false } // `users.sort(a.age < b.age)` diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 1505f663c8..9ea52b31dc 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -194,6 +194,14 @@ fn loop_map() { } } +fn free_map() { + nums := [1, 2, 3] + /* + nums2 := nums.map(it + handle_strings('a' + 'b', 'c')) + println(nums2) + */ +} + fn free_inside_opt_block() { x := opt('a' + 'b') or { get_string('c' + 'd') // c+d must be freed before a+b @@ -219,6 +227,7 @@ fn main() { if_expr() return_if_expr() free_inside_opt_block() + // free_map() // loop_map() println('end') }