From d1281ac6c15bb5f7af5a7b6a4c4a23057eaa6ce8 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 5 Dec 2020 05:06:47 +0100 Subject: [PATCH] autofree: handle` break` --- vlib/v/gen/cgen.v | 6 ++++- vlib/v/tests/valgrind/1.strings_and_arrays.v | 23 +++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 107cd7067f..a6341ebc31 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -847,7 +847,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { } ast.BranchStmt { g.write_v_source_line_info(node.pos) - if node.label.len > 0 { + if node.label != '' { if node.kind == .key_break { g.writeln('goto ${node.label}__break;') } else { @@ -856,6 +856,10 @@ fn (mut g Gen) stmt(node ast.Stmt) { } } else { // continue or break + if g.pref.autofree && !g.is_builtin_mod { + g.writeln('// free before continue/break') + g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, false) + } g.writeln('$node.kind;') } } diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index dc12728028..1981bd79ca 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -256,10 +256,31 @@ fn free_before_break() { s := 'a' + 'b' for { q := [1, 2, 3] + break + } + /* + for { + qq := [1, 2, 3] if true { + // breaking should free only vars in the closest for loop's scope + // `qq`, not `s` break } } + */ + /* + mut i := 0 + for { + i++ + qq := [1, 2, 3] + if i > 10 { + break + } + if true { + continue + } + } + */ } struct User { @@ -315,7 +336,7 @@ fn main() { comp_if() free_before_return() free_before_return_bool() - // free_before_break() + free_before_break() // free_map() // loop_map() // free_array_except_returned_element()