From baa2b62e88c5438c95ab439b12d3bafa55fd21a0 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 24 Jun 2023 13:37:39 -0400 Subject: [PATCH 1/2] cbe: fix crash caused by calling `mod.intValue` on `type_inferred_error_set` --- src/codegen/c.zig | 3 +-- test/behavior/error.zig | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 8afaae7cfa..8bf7c4e54e 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -5654,14 +5654,13 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { const operand = try f.resolveInst(ty_op.operand); const error_union_ty = f.typeOf(ty_op.operand).childType(mod); - const error_ty = error_union_ty.errorUnionSet(mod); const payload_ty = error_union_ty.errorUnionPayload(mod); // First, set the non-error value. if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { try f.writeCValueDeref(writer, operand); try writer.writeAll(" = "); - try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Other); + try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other); try writer.writeAll(";\n "); return operand; diff --git a/test/behavior/error.zig b/test/behavior/error.zig index 14b0eca030..be0de6932d 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -921,3 +921,20 @@ test "optional error set return type" { try expect(null == S.foo(true)); try expect(E.A == S.foo(false).?); } + +test "returning an error union containing a type with no runtime bits" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + + const ZeroByteType = struct { + foo: void, + + pub fn init() !@This() { + return .{ .foo = {} }; + } + }; + + var zero_byte: ZeroByteType = undefined; + (&zero_byte).* = try ZeroByteType.init(); +} From d3fed1a87ee20e5c041f18f9768ef5c9be96e69e Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sat, 24 Jun 2023 16:22:00 -0400 Subject: [PATCH 2/2] cbe: fix another instance of calling `intValue` with an error type --- src/codegen/c.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 8bf7c4e54e..d0e1c8c9a4 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -5534,7 +5534,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { else try f.writeCValueMember(writer, operand, .{ .identifier = "error" }) else - try f.object.dg.renderValue(writer, error_ty, try mod.intValue(error_ty, 0), .Initializer); + try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Initializer); } try writer.writeAll(";\n"); return local;