zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 0e3feebb042a538405153e4bc75a3bb73ee2e63c (tree)
parent 52e5c6602550788cab96957d1a177bc7952d7a09
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Sun, 18 Dec 2022 22:09:13 -0500

codegen: fix taking the address of a zero-bit field in a zero-bit struct

Normally when we want a pointer to the end of a struct we just add 1 to
the struct pointer.  However, when it is a zero-bit struct, the pointer
type being used during lowering is often a dummy pointer type that
actually points to a non-zero-bit type, so we actually want to add 0
instead, since a zero-bit struct begins and ends at the same address.

Diffstat:
Msrc/codegen/c.zig | 4+++-
Msrc/codegen/llvm.zig | 9++++-----
Mtest/behavior/struct.zig | 26+++++++++++++++++++++++---
3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/src/codegen/c.zig b/src/codegen/c.zig @@ -5131,7 +5131,9 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc .begin, .end => { try writer.writeByte('('); try f.writeCValue(writer, struct_ptr, .Other); - try writer.print(")[{}]", .{@boolToInt(field_loc == .end)}); + try writer.print(")[{}]", .{ + @boolToInt(field_loc == .end and struct_ty.hasRuntimeBitsIgnoreComptime()), + }); }, .field => |field| if (extra_name != .none) { try f.writeCValueDerefMember(writer, struct_ptr, extra_name); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig @@ -4046,9 +4046,8 @@ pub const DeclGen = struct { break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); } else { bitcast_needed = !parent_ty.eql(ptr_child_ty, dg.module); - const indices: [1]*llvm.Value = .{ - llvm_u32.constInt(1, .False), - }; + const llvm_index = llvm_u32.constInt(@boolToInt(parent_ty.hasRuntimeBitsIgnoreComptime()), .False); + const indices: [1]*llvm.Value = .{llvm_index}; break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len); } }, @@ -9774,8 +9773,8 @@ pub const FuncGen = struct { // end of the struct. Treat our struct pointer as an array of two and get // the index to the element at index `1` to get a pointer to the end of // the struct. - const llvm_usize = try self.dg.lowerType(Type.usize); - const llvm_index = llvm_usize.constInt(1, .False); + const llvm_u32 = self.dg.context.intType(32); + const llvm_index = llvm_u32.constInt(@boolToInt(struct_ty.hasRuntimeBitsIgnoreComptime()), .False); const indices: [1]*llvm.Value = .{llvm_index}; return self.builder.buildInBoundsGEP(struct_llvm_ty, struct_ptr, &indices, indices.len, ""); } diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig @@ -1365,7 +1365,7 @@ test "fieldParentPtr of a zero-bit field" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO const S = struct { - fn testOneType(comptime A: type) !void { + fn testStruct(comptime A: type) !void { { const a = A{ .u = 0 }; const b_ptr = &a.b; @@ -1379,9 +1379,29 @@ test "fieldParentPtr of a zero-bit field" { try std.testing.expectEqual(&a, a_ptr); } } + fn testNestedStruct(comptime A: type) !void { + { + const a = A{ .u = 0 }; + const c_ptr = &a.b.c; + const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr); + try std.testing.expectEqual(&a.b, b_ptr); + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + { + var a = A{ .u = 0 }; + const c_ptr = &a.b.c; + const b_ptr = @fieldParentPtr(@TypeOf(a.b), "c", c_ptr); + try std.testing.expectEqual(&a.b, b_ptr); + const a_ptr = @fieldParentPtr(A, "b", b_ptr); + try std.testing.expectEqual(&a, a_ptr); + } + } fn doTheTest() !void { - try testOneType(struct { b: void = {}, u: u8 }); - try testOneType(struct { u: u8, b: void = {} }); + try testStruct(struct { b: void = {}, u: u8 }); + try testStruct(struct { u: u8, b: void = {} }); + try testNestedStruct(struct { b: struct { c: void = {} } = .{}, u: u8 }); + try testNestedStruct(struct { u: u8, b: struct { c: void = {} } = .{} }); } }; try S.doTheTest();