Commit b82aec5a15

Jacob Young <jacobly0@users.noreply.github.com>
2022-11-16 05:18:00
cbe: fix indexing with a zero-bit element type
Fixes void dereference warnings on gcc 11.3.0.
1 parent b5b507a
Changed files (1)
src
codegen
src/codegen/c.zig
@@ -2658,12 +2658,14 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
     const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
     const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data;
     const ptr_ty = f.air.typeOf(bin_op.lhs);
+    const child_ty = ptr_ty.childType();
 
     const ptr = try f.resolveInst(bin_op.lhs);
+    if (!child_ty.hasRuntimeBitsIgnoreComptime()) return ptr;
     const index = try f.resolveInst(bin_op.rhs);
+
     const writer = f.object.writer();
     const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const);
-
     try writer.writeAll(" = &(");
     if (ptr_ty.ptrSize() == .One) {
         // It's a pointer to an array, so we need to de-reference.
@@ -2717,15 +2719,23 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
     const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
     const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data;
 
+    const slice_ty = f.air.typeOf(bin_op.lhs);
+    const child_ty = slice_ty.elemType2();
     const slice = try f.resolveInst(bin_op.lhs);
-    const index = try f.resolveInst(bin_op.rhs);
+
     const writer = f.object.writer();
     const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const);
-    try writer.writeAll(" = &");
+    try writer.writeAll(" = ");
+    if (child_ty.hasRuntimeBitsIgnoreComptime()) try writer.writeByte('&');
     try f.writeCValue(writer, slice, .Other);
-    try writer.writeAll(".ptr[");
-    try f.writeCValue(writer, index, .Other);
-    try writer.writeAll("];\n");
+    try writer.writeAll(".ptr");
+    if (child_ty.hasRuntimeBitsIgnoreComptime()) {
+        const index = try f.resolveInst(bin_op.rhs);
+        try writer.writeByte('[');
+        try f.writeCValue(writer, index, .Other);
+        try writer.writeByte(']');
+    }
+    try writer.writeAll(";\n");
     return local;
 }