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 {...@@ -704,9 +704,13 @@ pub const DeclGen = struct {
704704
705 try writer.writeByte('{');705 try writer.writeByte('{');
706 if (ty.unionTagTypeSafety()) |tag_ty| {706 if (ty.unionTagTypeSafety()) |tag_ty| {
707 try writer.writeAll(" .tag = ");707 const layout = ty.unionGetLayout(target);
708 try dg.renderValue(writer, tag_ty, val, .Initializer);708 if (layout.tag_size != 0) {
709 try writer.writeAll(", .payload = {");709 try writer.writeAll(" .tag = ");
710 try dg.renderValue(writer, tag_ty, val, .Initializer);
711 try writer.writeByte(',');
712 }
713 try writer.writeAll(" .payload = {");
710 }714 }
711 for (ty.unionFields().values()) |field| {715 for (ty.unionFields().values()) |field| {
712 if (!field.ty.hasRuntimeBits()) continue;716 if (!field.ty.hasRuntimeBits()) continue;
...@@ -1115,7 +1119,6 @@ pub const DeclGen = struct {...@@ -1115,7 +1119,6 @@ pub const DeclGen = struct {
1115 },1119 },
1116 .Union => {1120 .Union => {
1117 const union_obj = val.castTag(.@"union").?.data;1121 const union_obj = val.castTag(.@"union").?.data;
1118 const layout = ty.unionGetLayout(target);
11191122
1120 if (location != .Initializer) {1123 if (location != .Initializer) {
1121 try writer.writeByte('(');1124 try writer.writeByte('(');
...@@ -1125,6 +1128,7 @@ pub const DeclGen = struct {...@@ -1125,6 +1128,7 @@ pub const DeclGen = struct {
11251128
1126 try writer.writeByte('{');1129 try writer.writeByte('{');
1127 if (ty.unionTagTypeSafety()) |tag_ty| {1130 if (ty.unionTagTypeSafety()) |tag_ty| {
1131 const layout = ty.unionGetLayout(target);
1128 if (layout.tag_size != 0) {1132 if (layout.tag_size != 0) {
1129 try writer.writeAll(".tag = ");1133 try writer.writeAll(".tag = ");
1130 try dg.renderValue(writer, tag_ty, union_obj.tag, .Initializer);1134 try dg.renderValue(writer, tag_ty, union_obj.tag, .Initializer);
...@@ -5305,7 +5309,6 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5305,7 +5309,6 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
5305 const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;5309 const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;
5306 const union_ty = f.air.typeOfIndex(inst);5310 const union_ty = f.air.typeOfIndex(inst);
5307 const target = f.object.dg.module.getTarget();5311 const target = f.object.dg.module.getTarget();
5308 const layout = union_ty.unionGetLayout(target);
5309 const union_obj = union_ty.cast(Type.Payload.Union).?.data;5312 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
5310 const field_name = union_obj.fields.keys()[extra.field_index];5313 const field_name = union_obj.fields.keys()[extra.field_index];
5311 const payload = try f.resolveInst(extra.init);5314 const payload = try f.resolveInst(extra.init);
...@@ -5314,6 +5317,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5314,6 +5317,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
5314 const local = try f.allocLocal(union_ty, .Const);5317 const local = try f.allocLocal(union_ty, .Const);
5315 try writer.writeAll(" = {");5318 try writer.writeAll(" = {");
5316 if (union_ty.unionTagTypeSafety()) |tag_ty| {5319 if (union_ty.unionTagTypeSafety()) |tag_ty| {
5320 const layout = union_ty.unionGetLayout(target);
5317 if (layout.tag_size != 0) {5321 if (layout.tag_size != 0) {
5318 const field_index = tag_ty.enumFieldIndex(field_name).?;5322 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" {...@@ -435,7 +435,6 @@ test "global union with single field is correctly initialized" {
435 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;435 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
436 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;436 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
437 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO437 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
438 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
439438
440 glbl = Foo1{439 glbl = Foo1{
441 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },440 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },