autofree: skip lambda for now

pull/6695/head
Alexander Medvednikov 2020-10-29 08:59:13 +01:00
parent 71dfec3c4d
commit 423044d4d6
2 changed files with 13 additions and 1 deletions

View File

@ -119,6 +119,7 @@ mut:
cur_mod string cur_mod string
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)` is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
nr_vars_to_free int nr_vars_to_free int
inside_lambda bool
} }
const ( const (
@ -2017,7 +2018,7 @@ fn (mut g Gen) expr(node ast.Expr) {
// if g.fileis('1.strings') { // if g.fileis('1.strings') {
// println('before:' + node.autofree_pregen) // 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) // 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() // and it'll get messed up here, since it's handled recursively in autofree_call_pregen()
// so just skip it // 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)` // `nums.map(it % 2 == 0)`
fn (mut g Gen) gen_array_map(node ast.CallExpr) { fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.inside_lambda = true
tmp := g.new_tmp_var() tmp := g.new_tmp_var()
s := g.go_before_stmt(0) s := g.go_before_stmt(0)
// println('filter s="$s"') // println('filter s="$s"')
@ -4486,6 +4488,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
g.writeln('}') g.writeln('}')
g.write(s) g.write(s)
g.write(tmp) g.write(tmp)
g.inside_lambda = false
} }
// `users.sort(a.age < b.age)` // `users.sort(a.age < b.age)`

View File

@ -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() { fn free_inside_opt_block() {
x := opt('a' + 'b') or { x := opt('a' + 'b') or {
get_string('c' + 'd') // c+d must be freed before a+b get_string('c' + 'd') // c+d must be freed before a+b
@ -219,6 +227,7 @@ fn main() {
if_expr() if_expr()
return_if_expr() return_if_expr()
free_inside_opt_block() free_inside_opt_block()
// free_map()
// loop_map() // loop_map()
println('end') println('end')
} }