aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2022-11-15 23:18:00 -0500
committerJacob Young <jacobly0@users.noreply.github.com>2022-11-15 23:33:48 -0500
commitb82aec5a15acd3d10f2efbde52a0bfb9af411891 (patch)
treee39b571ce3628601de2381908e6abbd4a17765f1
parentb5b507a7425d2cf6b09c16db96fa10c3d13f7799 (diff)
downloadzig-b82aec5a15acd3d10f2efbde52a0bfb9af411891.tar.gz
zig-b82aec5a15acd3d10f2efbde52a0bfb9af411891.zip
cbe: fix indexing with a zero-bit element type
Fixes void dereference warnings on gcc 11.3.0.
-rw-r--r--src/codegen/c.zig22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index ad49a83510..9b74a48a1a 100644
--- a/src/codegen/c.zig
+++ b/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;
}