diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2022-11-02 21:16:06 -0400 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2022-11-02 21:42:40 -0400 |
| commit | 4537c1b8b6575c853ceaa7ab329e8b84d946249d (patch) | |
| tree | e2c82e834dfc8096a300e942d7aca1e60d39da4a /src/codegen/c.zig | |
| parent | fa46f9a3d75e6f3693827bf524244ed3c4902133 (diff) | |
| download | zig-4537c1b8b6575c853ceaa7ab329e8b84d946249d.tar.gz zig-4537c1b8b6575c853ceaa7ab329e8b84d946249d.zip | |
cbe: fix crash rendering union with zero-bit tag
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index e1c7b80c04..4473f680c4 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -704,9 +704,13 @@ pub const DeclGen = struct { try writer.writeByte('{'); if (ty.unionTagTypeSafety()) |tag_ty| { - try writer.writeAll(" .tag = "); - try dg.renderValue(writer, tag_ty, val, .Initializer); - try writer.writeAll(", .payload = {"); + const layout = ty.unionGetLayout(target); + if (layout.tag_size != 0) { + try writer.writeAll(" .tag = "); + try dg.renderValue(writer, tag_ty, val, .Initializer); + try writer.writeByte(','); + } + try writer.writeAll(" .payload = {"); } for (ty.unionFields().values()) |field| { if (!field.ty.hasRuntimeBits()) continue; @@ -1115,7 +1119,6 @@ pub const DeclGen = struct { }, .Union => { const union_obj = val.castTag(.@"union").?.data; - const layout = ty.unionGetLayout(target); if (location != .Initializer) { try writer.writeByte('('); @@ -1125,6 +1128,7 @@ pub const DeclGen = struct { try writer.writeByte('{'); if (ty.unionTagTypeSafety()) |tag_ty| { + const layout = ty.unionGetLayout(target); if (layout.tag_size != 0) { try writer.writeAll(".tag = "); try dg.renderValue(writer, tag_ty, union_obj.tag, .Initializer); @@ -5305,7 +5309,6 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; const union_ty = f.air.typeOfIndex(inst); const target = f.object.dg.module.getTarget(); - const layout = union_ty.unionGetLayout(target); const union_obj = union_ty.cast(Type.Payload.Union).?.data; const field_name = union_obj.fields.keys()[extra.field_index]; const payload = try f.resolveInst(extra.init); @@ -5314,6 +5317,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { const local = try f.allocLocal(union_ty, .Const); try writer.writeAll(" = {"); if (union_ty.unionTagTypeSafety()) |tag_ty| { + const layout = union_ty.unionGetLayout(target); if (layout.tag_size != 0) { const field_index = tag_ty.enumFieldIndex(field_name).?; |
