cgen: autofree scope var position fixes

pull/5849/head
joe-conigliaro 2020-07-16 20:06:50 +10:00
parent 6245876806
commit 612fe1b8fb
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
1 changed files with 10 additions and 5 deletions

View File

@ -622,11 +622,16 @@ fn (mut g Gen) stmts(stmts []ast.Stmt) {
g.write('')
g.write(')')
}
if g.pref.autofree && g.pref.experimental && stmts.len > 0 {
stmt := stmts[stmts.len - 1]
if g.pref.autofree && g.pref.experimental && !g.inside_vweb_tmpl && stmts.len > 0 {
// use the first stmt to get the scope
stmt := stmts[0]
// stmt := stmts[stmts.len-1]
if stmt !is ast.FnDecl {
// g.writeln('// autofree scope')
g.autofree_scope_vars(stmt.position().pos)
// g.writeln('// autofree_scope_vars($stmt.position().pos) | ${typeof(stmt)}')
// go back 1 position is important so we dont get the
// internal scope of for loops and possibly other nodes
g.autofree_scope_vars(stmt.position().pos-1)
}
}
}
@ -1586,9 +1591,9 @@ fn (g &Gen) autofree_var_call(free_fn_name string, v ast.Var) {
return
}
if v.typ.is_ptr() {
g.writeln('\t${free_fn_name}($v.name); // autofreed ptr var')
g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var')
} else {
g.writeln('\t${free_fn_name}(&$v.name); // autofreed var')
g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var')
}
}