authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-24 21:53:59-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-24 21:53:59-07:00
loga31ba25a3dc7db037a29938fb4f040896edfdba9
tree52bf4d79558a5fa54856f2b17dba1fbd11ea73a6
parent146b79af153bbd5dafda0ba12a040385c7fc58f8
parentd3fed1a87ee20e5c041f18f9768ef5c9be96e69e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16188 from kcbanner/fix_cbe_airErrUnionPayloadPtrSet

cbe: fix crash caused by calling `mod.intValue` on `type_inferred_error_set`

2 files changed, 19 insertions(+), 3 deletions(-)

src/codegen/c.zig+2-3
......@@ -5534,7 +5534,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
55345534 else
55355535 try f.writeCValueMember(writer, operand, .{ .identifier = "error" })
55365536 else
5537 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Initializer);
5537 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Initializer);
55385538 }
55395539 try writer.writeAll(";\n");
55405540 return local;
......@@ -5654,14 +5654,13 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
56545654 const operand = try f.resolveInst(ty_op.operand);
56555655 const error_union_ty = f.typeOf(ty_op.operand).childType(mod);
56565656
5657 const error_ty = error_union_ty.errorUnionSet(mod);
56585657 const payload_ty = error_union_ty.errorUnionPayload(mod);
56595658
56605659 // First, set the non-error value.
56615660 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
56625661 try f.writeCValueDeref(writer, operand);
56635662 try writer.writeAll(" = ");
5664 try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other);
5663 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
56655664 try writer.writeAll(";\n ");
56665665
56675666 return operand;
test/behavior/error.zig+17
......@@ -921,3 +921,20 @@ test "optional error set return type" {
921921 try expect(null == S.foo(true));
922922 try expect(E.A == S.foo(false).?);
923923}
924
925test "returning an error union containing a type with no runtime bits" {
926 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
927 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
928 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
929
930 const ZeroByteType = struct {
931 foo: void,
932
933 pub fn init() !@This() {
934 return .{ .foo = {} };
935 }
936 };
937
938 var zero_byte: ZeroByteType = undefined;
939 (&zero_byte).* = try ZeroByteType.init();
940}