mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
autofree: advanced expr test
This commit is contained in:
parent
403cd0d915
commit
47a62b12d4
@ -729,7 +729,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||
defer {
|
||||
// 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);`
|
||||
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
|
||||
if g.strs_to_free.len != 0 {
|
||||
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) {
|
||||
// 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)`
|
||||
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
|
||||
|
@ -1,5 +1,6 @@
|
||||
import os
|
||||
|
||||
// import time
|
||||
fn return_array(array_arg []string) []int { // array argument must not be freed
|
||||
s := [1, 2, 3] // escaping array must not be freed
|
||||
return s
|
||||
@ -22,12 +23,20 @@ fn handle_strings(s, p string) int {
|
||||
fn handle_int(n int) {
|
||||
}
|
||||
|
||||
fn add_strings(a, b string) string {
|
||||
return a + b
|
||||
}
|
||||
|
||||
fn str_tmp_expr() {
|
||||
println('a' + 'b') // tmp expression result 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
|
||||
}
|
||||
|
||||
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 {
|
||||
a int
|
||||
b string
|
||||
@ -68,6 +77,7 @@ fn opt(s string) ?int {
|
||||
return 1
|
||||
}
|
||||
|
||||
/*
|
||||
fn optional_str() {
|
||||
q := 'select'
|
||||
s := 'query: select'
|
||||
@ -85,15 +95,20 @@ fn optional_str() {
|
||||
}
|
||||
println(pos + 1)
|
||||
}
|
||||
*/
|
||||
fn tt() {
|
||||
// time.parse_rfc2822('1234')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println('start')
|
||||
foo()
|
||||
str_tmp_expr()
|
||||
str_tmp_expr_advanced()
|
||||
str_inter()
|
||||
match_expr()
|
||||
reassign_str()
|
||||
optional_str()
|
||||
// optional_str()
|
||||
// str_replace()
|
||||
println('end')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user