Commit 89f6ff1771

Veikka Tuominen <git@vexu.eu>
2022-02-19 14:24:22
stage2: correct use of .unwrap_err_union_* in LLVM and C backend
1 parent e027492
Changed files (2)
src
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;
     }
 
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, "");
         }