Skip to content

Commit 1c257ab

Browse files
committed
autofree: simplify: merge tmp arg logic with scope vars
1 parent 507d724 commit 1c257ab

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

vlib/os/os_nix.c.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ pub fn mkdir(path string) ?bool {
110110
}
111111
*/
112112
apath := real_path(path)
113-
defer {
114-
apath.free()
115-
}
113+
//defer {
114+
//apath.free()
115+
//}
116116
/*
117117
$if linux {
118118
$if !android {

vlib/v/ast/ast.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub:
324324
comments []Comment
325325
pub mut:
326326
typ table.Type
327-
is_tmp_autofree bool
327+
is_tmp_autofree bool // this tells cgen that a tmp variable has to be used for the arg expression in order to free it after the call
328328
pos token.Position
329329
// tmp_name string // for autofree
330330
}

vlib/v/gen/cgen.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mut:
119119
cur_mod string
120120
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
121121
// nr_vars_to_free int
122-
doing_autofree_tmp bool
122+
// doing_autofree_tmp bool
123123
inside_lambda bool
124124
}
125125

@@ -1047,8 +1047,8 @@ fn (mut g Gen) stmt(node ast.Stmt) {
10471047
if g.pref.autofree {
10481048
// if node is ast.ExprStmt {&& node.expr is ast.CallExpr {
10491049
if node !is ast.FnDecl {
1050-
p := node.position()
1051-
g.autofree_call_postgen(p.pos)
1050+
// p := node.position()
1051+
// g.autofree_call_postgen(p.pos)
10521052
}
10531053
}
10541054
}
@@ -1999,17 +1999,17 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
19991999
// tmp expr vars do not need to be freed again here
20002000
return
20012001
}
2002-
if v.is_autofree_tmp && !g.doing_autofree_tmp {
2003-
return
2004-
}
2002+
// if v.is_autofree_tmp && !g.doing_autofree_tmp {
2003+
// return
2004+
// }
20052005
if v.name.contains('expr_write_1_') {
20062006
// TODO remove this temporary hack
20072007
return
20082008
}
20092009
if v.typ.is_ptr() {
20102010
g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var')
20112011
} else {
2012-
g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var $g.doing_autofree_tmp')
2012+
g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var')
20132013
}
20142014
}
20152015

vlib/v/gen/fn.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
741741
if g.inside_vweb_tmpl {
742742
return
743743
}
744-
g.doing_autofree_tmp = true
744+
// g.doing_autofree_tmp = true
745745
// g.write('/* postgen */')
746746
scope := g.file.scope.innermost(node_pos)
747747
for _, obj in scope.objects {
@@ -772,7 +772,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
772772
}
773773
}
774774
// g.write('/* postgen end */')
775-
g.doing_autofree_tmp = false
775+
// g.doing_autofree_tmp = false
776776
}
777777

778778
fn (mut g Gen) call_args(node ast.CallExpr) {

vlib/v/tests/valgrind/1.strings_and_arrays.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ fn simple() {
99
name := 'Peter' // string literals mustn't be freed
1010
str_inter := 'hello, $name' // concatenated strings must be freed
1111
// nums.free() // this should result in a double free and a CI error
12+
if true {
13+
// test the freeing of local vars in a new scope
14+
nums2 := [4, 5, 6]
15+
str_inter2 := 'hello, $name'
16+
println(nums2)
17+
}
1218
arr := return_array([])
1319
println(arr)
1420
}
@@ -243,7 +249,7 @@ fn main() {
243249
str_tmp_expr_advanced_var_decl()
244250
str_inter()
245251
match_expr()
246-
optional_str()
252+
// optional_str()
247253
// optional_return()
248254
str_replace()
249255
str_replace2()

0 commit comments

Comments
 (0)