gen: fix generated `defer` (#5717)

pull/5700/head
yuyi 2020-07-08 13:39:11 +08:00 committed by GitHub
parent 317b2dc579
commit b0ce66d141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -850,15 +850,18 @@ fn (mut g Gen) stmt(node ast.Stmt) {
fn (mut g Gen) write_defer_stmts() { fn (mut g Gen) write_defer_stmts() {
for defer_stmt in g.defer_stmts { for defer_stmt in g.defer_stmts {
g.writeln('// defer') g.writeln('// Defer begin')
if defer_stmt.ifdef.len > 0 { if defer_stmt.ifdef.len > 0 {
g.writeln(defer_stmt.ifdef) g.writeln(defer_stmt.ifdef)
g.stmts(defer_stmt.stmts) g.stmts(defer_stmt.stmts)
g.writeln('') g.writeln('')
g.writeln('#endif') g.writeln('#endif')
} else { } else {
g.indent--
g.stmts(defer_stmt.stmts) g.stmts(defer_stmt.stmts)
g.indent++
} }
g.writeln('// Defer end')
} }
} }

View File

@ -136,7 +136,9 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
} }
g.stmts(it.stmts) g.stmts(it.stmts)
// //////////// // ////////////
if it.return_type == table.void_type {
g.write_defer_stmts_when_needed() g.write_defer_stmts_when_needed()
}
// ///////// // /////////
if g.autofree { if g.autofree {
// TODO: remove this, when g.write_autofree_stmts_when_needed works properly // TODO: remove this, when g.write_autofree_stmts_when_needed works properly