@@ -7237,7 +7237,7 @@ fn (mut g Gen) sort_structs(typesa []&ast.TypeSymbol) []&ast.TypeSymbol {
72377237}
72387238
72397239fn (mut g Gen) gen_or_block_stmts (cvar_name string , cast_typ string , stmts []ast.Stmt, return_type ast.Type,
7240- is_option bool ) {
7240+ is_option bool , scope & ast.Scope, pos token.Pos ) {
72417241 g.indent++
72427242 for i, stmt in stmts {
72437243 if i == stmts.len - 1 {
@@ -7247,6 +7247,7 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
72477247 || expr_stmt.typ in [ast.none_type, ast.error_type]) {
72487248 // `return foo() or { error('failed') }`
72497249 if g.cur_fn != unsafe { nil } {
7250+ g.write_defer_stmts (scope, true , pos)
72507251 if g.cur_fn.return_type.has_flag (.result) {
72517252 g.write ('return ' )
72527253 g.gen_result_error (g.cur_fn.return_type, expr_stmt.expr)
@@ -7262,6 +7263,7 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
72627263 g.write ('${cvar_name} = ' )
72637264 g.gen_option_error (return_type, expr_stmt.expr)
72647265 g.writeln (';' )
7266+ g.write_defer_stmts (scope, false , pos)
72657267 } else if return_type == ast.rvoid_type {
72667268 // fn returns !, do not fill var.data
72677269 old_inside_opt_data := g.inside_opt_data
@@ -7318,6 +7320,7 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
73187320 g.write (', sizeof(${cast_typ} ))' )
73197321 }
73207322 g.writeln (';' )
7323+ g.write_defer_stmts (scope, return_wrapped, pos)
73217324 g.stmt_path_pos.delete_last ()
73227325 if return_wrapped {
73237326 g.writeln ('return ${cvar_name} ;' )
@@ -7376,13 +7379,19 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty
73767379 }
73777380 stmts := or_block.stmts
73787381 if stmts.len > 0 && stmts.last () is ast.ExprStmt && stmts.last ().typ != ast.void_type {
7379- g.gen_or_block_stmts (cvar_name, mr_styp, stmts, return_type, true )
7382+ g.gen_or_block_stmts (cvar_name, mr_styp, stmts, return_type, true , or_block.scope,
7383+ or_block.pos)
73807384 } else {
73817385 g.stmts (stmts)
7382- if stmts.len > 0 && stmts.last () is ast.ExprStmt {
7383- g.writeln (';' )
7386+ if stmts.len > 0 {
7387+ stmt_last := stmts.last ()
7388+ if stmt_last is ast.ExprStmt {
7389+ g.writeln (';' )
7390+ }
7391+ if stmt_last ! in [ast.Return, ast.BranchStmt] {
7392+ g.write_defer_stmts (or_block.scope, false , or_block.pos)
7393+ }
73847394 }
7385- g.write_defer_stmts (or_block.scope, false , or_block.pos)
73867395 }
73877396 g.or_expr_return_type = ast.void_type
73887397 } else if or_block.kind == .propagate_result
0 commit comments