autofree: handle` break`

pull/7141/head
Alexander Medvednikov 2020-12-05 05:06:47 +01:00
parent 4fb37e81b2
commit d1281ac6c1
2 changed files with 27 additions and 2 deletions

View File

@ -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;')
}
}

View File

@ -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()