authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-02 21:16:06-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-11-02 21:42:40-04:00
log4537c1b8b6575c853ceaa7ab329e8b84d946249d
treee2c82e834dfc8096a300e942d7aca1e60d39da4a
parentfa46f9a3d75e6f3693827bf524244ed3c4902133

cbe: fix crash rendering union with zero-bit tag


2 files changed, 9 insertions(+), 6 deletions(-)

src/codegen/c.zig+9-5
......@@ -704,9 +704,13 @@ pub const DeclGen = struct {
704704
705705 try writer.writeByte('{');
706706 if (ty.unionTagTypeSafety()) |tag_ty| {
707 try writer.writeAll(" .tag = ");
708 try dg.renderValue(writer, tag_ty, val, .Initializer);
709 try writer.writeAll(", .payload = {");
707 const layout = ty.unionGetLayout(target);
708 if (layout.tag_size != 0) {
709 try writer.writeAll(" .tag = ");
710 try dg.renderValue(writer, tag_ty, val, .Initializer);
711 try writer.writeByte(',');
712 }
713 try writer.writeAll(" .payload = {");
710714 }
711715 for (ty.unionFields().values()) |field| {
712716 if (!field.ty.hasRuntimeBits()) continue;
......@@ -1115,7 +1119,6 @@ pub const DeclGen = struct {
11151119 },
11161120 .Union => {
11171121 const union_obj = val.castTag(.@"union").?.data;
1118 const layout = ty.unionGetLayout(target);
11191122
11201123 if (location != .Initializer) {
11211124 try writer.writeByte('(');
......@@ -1125,6 +1128,7 @@ pub const DeclGen = struct {
11251128
11261129 try writer.writeByte('{');
11271130 if (ty.unionTagTypeSafety()) |tag_ty| {
1131 const layout = ty.unionGetLayout(target);
11281132 if (layout.tag_size != 0) {
11291133 try writer.writeAll(".tag = ");
11301134 try dg.renderValue(writer, tag_ty, union_obj.tag, .Initializer);
......@@ -5305,7 +5309,6 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
53055309 const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;
53065310 const union_ty = f.air.typeOfIndex(inst);
53075311 const target = f.object.dg.module.getTarget();
5308 const layout = union_ty.unionGetLayout(target);
53095312 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
53105313 const field_name = union_obj.fields.keys()[extra.field_index];
53115314 const payload = try f.resolveInst(extra.init);
......@@ -5314,6 +5317,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
53145317 const local = try f.allocLocal(union_ty, .Const);
53155318 try writer.writeAll(" = {");
53165319 if (union_ty.unionTagTypeSafety()) |tag_ty| {
5320 const layout = union_ty.unionGetLayout(target);
53175321 if (layout.tag_size != 0) {
53185322 const field_index = tag_ty.enumFieldIndex(field_name).?;
53195323
test/behavior/union.zig-1
......@@ -435,7 +435,6 @@ test "global union with single field is correctly initialized" {
435435 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
436436 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
437437 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
438 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
439438
440439 glbl = Foo1{
441440 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },