1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

autofree: handle break

This commit is contained in:
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 { ast.BranchStmt {
g.write_v_source_line_info(node.pos) g.write_v_source_line_info(node.pos)
if node.label.len > 0 { if node.label != '' {
if node.kind == .key_break { if node.kind == .key_break {
g.writeln('goto ${node.label}__break;') g.writeln('goto ${node.label}__break;')
} else { } else {
@ -856,6 +856,10 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} }
} else { } else {
// continue or break // 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;') g.writeln('$node.kind;')
} }
} }

View File

@ -256,10 +256,31 @@ fn free_before_break() {
s := 'a' + 'b' s := 'a' + 'b'
for { for {
q := [1, 2, 3] q := [1, 2, 3]
break
}
/*
for {
qq := [1, 2, 3]
if true { if true {
// breaking should free only vars in the closest for loop's scope
// `qq`, not `s`
break break
} }
} }
*/
/*
mut i := 0
for {
i++
qq := [1, 2, 3]
if i > 10 {
break
}
if true {
continue
}
}
*/
} }
struct User { struct User {
@ -315,7 +336,7 @@ fn main() {
comp_if() comp_if()
free_before_return() free_before_return()
free_before_return_bool() free_before_return_bool()
// free_before_break() free_before_break()
// free_map() // free_map()
// loop_map() // loop_map()
// free_array_except_returned_element() // free_array_except_returned_element()