autofree: advanced expr test
parent
403cd0d915
commit
47a62b12d4
|
@ -729,7 +729,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||||
defer {
|
defer {
|
||||||
// If we have temporary string exprs to free after this statement, do it. e.g.:
|
// If we have temporary string exprs to free after this statement, do it. e.g.:
|
||||||
// `foo('a' + 'b')` => `tmp := 'a' + 'b'; foo(tmp); string_free(&tmp);`
|
// `foo('a' + 'b')` => `tmp := 'a' + 'b'; foo(tmp); string_free(&tmp);`
|
||||||
if g.pref.autofree && !g.inside_or_block {
|
if g.pref.autofree { // && !g.inside_or_block {
|
||||||
// TODO remove the inside_or_block hack. strings are not freed in or{} atm
|
// TODO remove the inside_or_block hack. strings are not freed in or{} atm
|
||||||
if g.strs_to_free.len != 0 {
|
if g.strs_to_free.len != 0 {
|
||||||
g.writeln('// strs_to_free:')
|
g.writeln('// strs_to_free:')
|
||||||
|
|
|
@ -615,7 +615,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) generate_tmp_autofree_arg_vars(node ast.CallExpr, name string) {
|
fn (mut g Gen) generate_tmp_autofree_arg_vars(node ast.CallExpr, name string) {
|
||||||
// Create a temporary var for each argument in order to free it (only if it's a complex expression,
|
// Create a temporary var before fn call for each argument in order to free it (only if it's a complex expression,
|
||||||
// like `foo(get_string())` or `foo(a + b)`
|
// like `foo(get_string())` or `foo(a + b)`
|
||||||
mut free_tmp_arg_vars := g.autofree && g.pref.experimental && !g.is_builtin_mod &&
|
mut free_tmp_arg_vars := g.autofree && g.pref.experimental && !g.is_builtin_mod &&
|
||||||
node.args.len > 0 && !node.args[0].typ.has_flag(.optional) // TODO copy pasta checker.v
|
node.args.len > 0 && !node.args[0].typ.has_flag(.optional) // TODO copy pasta checker.v
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
// import time
|
||||||
fn return_array(array_arg []string) []int { // array argument must not be freed
|
fn return_array(array_arg []string) []int { // array argument must not be freed
|
||||||
s := [1, 2, 3] // escaping array must not be freed
|
s := [1, 2, 3] // escaping array must not be freed
|
||||||
return s
|
return s
|
||||||
|
@ -22,12 +23,20 @@ fn handle_strings(s, p string) int {
|
||||||
fn handle_int(n int) {
|
fn handle_int(n int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_strings(a, b string) string {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
|
||||||
fn str_tmp_expr() {
|
fn str_tmp_expr() {
|
||||||
println('a' + 'b') // tmp expression result must be freed
|
println('a' + 'b') // tmp expression result must be freed
|
||||||
handle_strings('c' + 'd', 'e' + 'f') // multiple tmp expressions must be freed
|
handle_strings('c' + 'd', 'e' + 'f') // multiple tmp expressions must be freed
|
||||||
handle_int(handle_strings('x' + 'y', 'f')) // exprs 2 levels deep must bee freed
|
handle_int(handle_strings('x' + 'y', 'f')) // exprs 2 levels deep must bee freed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn str_tmp_expr_advanced() {
|
||||||
|
// handle_strings('c' + 'd', add_strings('e' + 'f', 'g')) // both lvl 1 and lvl2 exprs must be freed
|
||||||
|
}
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
a int
|
a int
|
||||||
b string
|
b string
|
||||||
|
@ -68,6 +77,7 @@ fn opt(s string) ?int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
fn optional_str() {
|
fn optional_str() {
|
||||||
q := 'select'
|
q := 'select'
|
||||||
s := 'query: select'
|
s := 'query: select'
|
||||||
|
@ -85,15 +95,20 @@ fn optional_str() {
|
||||||
}
|
}
|
||||||
println(pos + 1)
|
println(pos + 1)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
fn tt() {
|
||||||
|
// time.parse_rfc2822('1234')
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println('start')
|
println('start')
|
||||||
foo()
|
foo()
|
||||||
str_tmp_expr()
|
str_tmp_expr()
|
||||||
|
str_tmp_expr_advanced()
|
||||||
str_inter()
|
str_inter()
|
||||||
match_expr()
|
match_expr()
|
||||||
reassign_str()
|
reassign_str()
|
||||||
optional_str()
|
// optional_str()
|
||||||
// str_replace()
|
// str_replace()
|
||||||
println('end')
|
println('end')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue