autofree: free before return tests

pull/6785/head
Alexander Medvednikov 2020-11-09 11:54:41 +01:00
parent 7108f89b69
commit 507d724ee6
4 changed files with 29 additions and 6 deletions

View File

@ -110,6 +110,9 @@ pub fn mkdir(path string) ?bool {
} }
*/ */
apath := real_path(path) apath := real_path(path)
defer {
apath.free()
}
/* /*
$if linux { $if linux {
$if !android { $if !android {

View File

@ -1003,11 +1003,12 @@ fn (mut g Gen) stmt(node ast.Stmt) {
af := g.pref.autofree && node.exprs.len > 0 && node.exprs[0] is ast.CallExpr && !g.is_builtin_mod af := g.pref.autofree && node.exprs.len > 0 && node.exprs[0] is ast.CallExpr && !g.is_builtin_mod
if g.pref.autofree { if g.pref.autofree {
g.writeln('// ast.Return free') g.writeln('// ast.Return free')
// if af { if af {
g.writeln('//af tmp')
// g.autofree_call_pregen(node.exprs[0] as ast.CallExpr) // g.autofree_call_pregen(node.exprs[0] as ast.CallExpr)
// } }
// g.autofree_scope_vars(node.pos.pos) // g.autofree_scope_vars(node.pos.pos - 1)
g.write_autofree_stmts_when_needed(node) // g.write_autofree_stmts_when_needed(node)
} }
g.return_statement(node, af) g.return_statement(node, af)
} }

View File

@ -186,6 +186,7 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) {
} }
} }
/*
fn (mut g Gen) write_autofree_stmts_when_needed(r ast.Return) { fn (mut g Gen) write_autofree_stmts_when_needed(r ast.Return) {
// TODO: write_autofree_stmts_when_needed should account for the current local scope vars. // TODO: write_autofree_stmts_when_needed should account for the current local scope vars.
// TODO: write_autofree_stmts_when_needed should not free the returned variables. // TODO: write_autofree_stmts_when_needed should not free the returned variables.
@ -195,7 +196,7 @@ fn (mut g Gen) write_autofree_stmts_when_needed(r ast.Return) {
// g.writeln(g.autofree_scope_vars(g.fn_decl.body_pos.pos)) // g.writeln(g.autofree_scope_vars(g.fn_decl.body_pos.pos))
// g.writeln('//--------------------------------------------------- ') // //g.write( g.autofree_scope_vars(r.pos.pos) ) // g.writeln('//--------------------------------------------------- ') // //g.write( g.autofree_scope_vars(r.pos.pos) )
} }
*/
fn (mut g Gen) write_defer_stmts_when_needed() { fn (mut g Gen) write_defer_stmts_when_needed() {
if g.defer_stmts.len > 0 { if g.defer_stmts.len > 0 {
g.write_defer_stmts() g.write_defer_stmts()

View File

@ -1,3 +1,5 @@
import os
// This program is built and run via Valgrind to ensure there are no leaks with -autofree // This program is built and run via Valgrind to ensure there are no leaks with -autofree
fn simple() { fn simple() {
nums := [1, 2, 3] // local array must be freed nums := [1, 2, 3] // local array must be freed
@ -218,6 +220,20 @@ fn free_inside_opt_block() {
} }
} }
fn free_before_return() {
s := 'a' + 'b'
println(s)
if true {
return
}
}
fn free_before_return_bool() bool {
s := 'a' + 'b'
println(s)
return true
}
fn main() { fn main() {
println('start') println('start')
simple() simple()
@ -236,6 +252,8 @@ fn main() {
q := if_expr() q := if_expr()
s := return_if_expr() s := return_if_expr()
free_inside_opt_block() free_inside_opt_block()
// free_before_return()
// free_before_return_bool()
// free_map() // free_map()
// loop_map() // loop_map()
println('end') println('end')