From 89f6ff177178dba4e66680227a02de7b4068123c Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 19 Feb 2022 15:24:22 +0200 Subject: [PATCH] stage2: correct use of .unwrap_err_union_* in LLVM and C backend --- src/codegen/c.zig | 26 +++++++++++++++----------- src/codegen/llvm.zig | 10 ++++++---- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 776cec6457..f6e2d467a7 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -3090,17 +3090,18 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { const operand = try f.resolveInst(ty_op.operand); const operand_ty = f.air.typeOf(ty_op.operand); - const payload_ty = operand_ty.errorUnionPayload(); - if (!payload_ty.hasRuntimeBits()) { - if (operand_ty.zigTypeTag() == .Pointer) { - const local = try f.allocLocal(inst_ty, .Const); - try writer.writeAll(" = *"); - try f.writeCValue(writer, operand); - try writer.writeAll(";\n"); - return local; - } else { + if (operand_ty.zigTypeTag() == .Pointer) { + if (!operand_ty.childType().errorUnionPayload().hasRuntimeBits()) { return operand; } + const local = try f.allocLocal(inst_ty, .Const); + try writer.writeAll(" = *"); + try f.writeCValue(writer, operand); + try writer.writeAll(";\n"); + return local; + } + if (!operand_ty.errorUnionPayload().hasRuntimeBits()) { + return operand; } const local = try f.allocLocal(inst_ty, .Const); @@ -3123,8 +3124,11 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: []cons const operand = try f.resolveInst(ty_op.operand); const operand_ty = f.air.typeOf(ty_op.operand); - const payload_ty = operand_ty.errorUnionPayload(); - if (!payload_ty.hasRuntimeBits()) { + const error_union_ty = if (operand_ty.zigTypeTag() == .Pointer) + operand_ty.childType() + else + operand_ty; + if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) { return CValue.none; } diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index ed57562e4c..8af74fce64 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -3165,8 +3165,9 @@ pub const FuncGen = struct { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const operand = try self.resolveInst(ty_op.operand); - const err_union_ty = self.air.typeOf(ty_op.operand); - const payload_ty = err_union_ty.errorUnionPayload(); + const result_ty = self.air.getRefType(ty_op.ty); + const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty; + if (!payload_ty.hasRuntimeBits()) return null; if (operand_is_ptr or isByRef(payload_ty)) { return self.builder.buildStructGEP(operand, 1, ""); @@ -3185,14 +3186,15 @@ pub const FuncGen = struct { const ty_op = self.air.instructions.items(.data)[inst].ty_op; const operand = try self.resolveInst(ty_op.operand); const operand_ty = self.air.typeOf(ty_op.operand); + const err_set_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; - const payload_ty = operand_ty.errorUnionPayload(); + const payload_ty = err_set_ty.errorUnionPayload(); if (!payload_ty.hasRuntimeBits()) { if (!operand_is_ptr) return operand; return self.builder.buildLoad(operand, ""); } - if (operand_is_ptr or isByRef(payload_ty)) { + if (operand_is_ptr or isByRef(err_set_ty)) { const err_field_ptr = self.builder.buildStructGEP(operand, 0, ""); return self.builder.buildLoad(err_field_ptr, ""); }