diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 776cec645786..f6e2d467a7b4 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 ed57562e4c3b..8af74fce6458 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, ""); } diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 8677a00f77e8..88a8bae7359b 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1200,7 +1200,7 @@ test "for loop over pointers to struct, getting field from struct pointer" { try S.doTheTest(); } -test "anon init through eror unions and optionals" { +test "anon init through error unions and optionals" { if (builtin.zig_backend == .stage1) return error.SkipZigTest; if (true) return error.SkipZigTest; // TODO