diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-07-16 16:32:49 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-07-23 15:40:11 +0300 |
| commit | ff7ec4efb5a6da565b92bc7b129d03680a4a72bd (patch) | |
| tree | 14006dc9e492015837a97328ccb85ac77f3e27e3 /src/codegen/c.zig | |
| parent | 55fe34100f8b516480cf530eb58d00ea8b665765 (diff) | |
| download | zig-ff7ec4efb5a6da565b92bc7b129d03680a4a72bd.tar.gz zig-ff7ec4efb5a6da565b92bc7b129d03680a4a72bd.zip | |
Sema: bad union field access safety
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 1e629d7cb0..98f7792a76 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -504,7 +504,7 @@ pub const DeclGen = struct { if (field_ty.hasRuntimeBitsIgnoreComptime()) { try writer.writeAll("&("); try dg.renderParentPtr(writer, field_ptr.container_ptr, container_ptr_ty); - if (field_ptr.container_ty.tag() == .union_tagged) { + if (field_ptr.container_ty.tag() == .union_tagged or field_ptr.container_ty.tag() == .union_safety_tagged) { try writer.print(")->payload.{ }", .{fmtIdent(field_name)}); } else { try writer.print(")->{ }", .{fmtIdent(field_name)}); @@ -842,7 +842,7 @@ pub const DeclGen = struct { try dg.renderTypecast(writer, ty); try writer.writeAll("){"); - if (ty.unionTagType()) |tag_ty| { + if (ty.unionTagTypeSafety()) |tag_ty| { if (layout.tag_size != 0) { try writer.writeAll(".tag = "); try dg.renderValue(writer, tag_ty, union_obj.tag, location); @@ -858,7 +858,7 @@ pub const DeclGen = struct { try writer.print(".{ } = ", .{fmtIdent(field_name)}); try dg.renderValue(writer, field_ty, union_obj.val, location); } - if (ty.unionTagType()) |_| { + if (ty.unionTagTypeSafety()) |_| { try writer.writeAll("}"); } try writer.writeAll("}"); @@ -1110,7 +1110,7 @@ pub const DeclGen = struct { defer buffer.deinit(); try buffer.appendSlice("typedef "); - if (t.unionTagType()) |tag_ty| { + if (t.unionTagTypeSafety()) |tag_ty| { const name: CValue = .{ .bytes = "tag" }; try buffer.appendSlice("struct {\n "); if (layout.tag_size != 0) { @@ -1134,7 +1134,7 @@ pub const DeclGen = struct { } try buffer.appendSlice("} "); - if (t.unionTagType()) |_| { + if (t.unionTagTypeSafety()) |_| { try buffer.appendSlice("payload;\n} "); } @@ -3368,7 +3368,7 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc field_name = fields.keys()[index]; field_val_ty = fields.values()[index].ty; }, - .@"union", .union_tagged => { + .@"union", .union_safety_tagged, .union_tagged => { const fields = struct_ty.unionFields(); field_name = fields.keys()[index]; field_val_ty = fields.values()[index].ty; @@ -3383,7 +3383,7 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc }, else => unreachable, } - const payload = if (struct_ty.tag() == .union_tagged) "payload." else ""; + const payload = if (struct_ty.tag() == .union_tagged or struct_ty.tag() == .union_safety_tagged) "payload." else ""; const inst_ty = f.air.typeOfIndex(inst); const local = try f.allocLocal(inst_ty, .Const); @@ -3415,7 +3415,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { defer buf.deinit(); const field_name = switch (struct_ty.tag()) { .@"struct" => struct_ty.structFields().keys()[extra.field_index], - .@"union", .union_tagged => struct_ty.unionFields().keys()[extra.field_index], + .@"union", .union_safety_tagged, .union_tagged => struct_ty.unionFields().keys()[extra.field_index], .tuple, .anon_struct => blk: { const tuple = struct_ty.tupleFields(); if (tuple.values[extra.field_index].tag() != .unreachable_value) return CValue.none; @@ -3425,7 +3425,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { }, else => unreachable, }; - const payload = if (struct_ty.tag() == .union_tagged) "payload." else ""; + const payload = if (struct_ty.tag() == .union_tagged or struct_ty.tag() == .union_safety_tagged) "payload." else ""; const inst_ty = f.air.typeOfIndex(inst); const local = try f.allocLocal(inst_ty, .Const); |
