Skip to content

Commit

Permalink
stage2: correct use of .unwrap_err_union_* in LLVM and C backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Feb 19, 2022
1 parent 984dc73 commit 8c2c764
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
26 changes: 15 additions & 11 deletions src/codegen/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand All @@ -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, "");
}
Expand Down
2 changes: 1 addition & 1 deletion test/behavior/struct.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8c2c764

Please sign in to comment.