diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 41ec308163..11da0fa69f 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -850,15 +850,18 @@ fn (mut g Gen) stmt(node ast.Stmt) { fn (mut g Gen) write_defer_stmts() { for defer_stmt in g.defer_stmts { - g.writeln('// defer') + g.writeln('// Defer begin') if defer_stmt.ifdef.len > 0 { g.writeln(defer_stmt.ifdef) g.stmts(defer_stmt.stmts) g.writeln('') g.writeln('#endif') } else { + g.indent-- g.stmts(defer_stmt.stmts) + g.indent++ } + g.writeln('// Defer end') } } diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 539ac9da76..4113b6e55e 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -136,7 +136,9 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) { } g.stmts(it.stmts) // //////////// - g.write_defer_stmts_when_needed() + if it.return_type == table.void_type { + g.write_defer_stmts_when_needed() + } // ///////// if g.autofree { // TODO: remove this, when g.write_autofree_stmts_when_needed works properly