From 8a95bac593ce9aa36b76229a9f2f4aba8c8d30dd Mon Sep 17 00:00:00 2001 From: Matthew Borkowski Date: Sat, 23 Oct 2021 20:05:47 -0400 Subject: [PATCH] astgen.zig: when ret's operand ResultLoc is .ptr, load from ret_ptr before is_non_err or err_union_code --- src/AstGen.zig | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 14ce5a4988..33f538df1b 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -6500,7 +6500,8 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref }, .always => { // Value is always an error. Emit both error defers and regular defers. - const err_code = try gz.addUnNode(.err_union_code, operand, node); + const result = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr, node) else operand; + const err_code = try gz.addUnNode(.err_union_code, result, node); try genDefers(gz, defer_outer, scope, .{ .both = err_code }); try gz.addRet(rl, operand, node); return Zir.Inst.Ref.unreachable_value; @@ -6515,7 +6516,8 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref } // Emit conditional branch for generating errdefers. - const is_non_err = try gz.addUnNode(.is_non_err, operand, node); + const result = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr, node) else operand; + const is_non_err = try gz.addUnNode(.is_non_err, result, node); const condbr = try gz.addCondBr(.condbr, node); var then_scope = gz.makeSubBlock(scope); @@ -6528,7 +6530,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref defer else_scope.instructions.deinit(astgen.gpa); const which_ones: DefersToEmit = if (!defer_counts.need_err_code) .both_sans_err else .{ - .both = try else_scope.addUnNode(.err_union_code, operand, node), + .both = try else_scope.addUnNode(.err_union_code, result, node), }; try genDefers(&else_scope, defer_outer, scope, which_ones); try else_scope.addRet(rl, operand, node);