authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-18 13:49:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-18 13:51:15-07:00
log6ed0910d6d974750fe65990983d5347ceacc0186
tree995cb7403d93934044c9135ff9b466a1c16e1b25
parent40ed6ae8469fd599f0524d294f38365c3bb8a825

Revert "llvm: fix lowering pointer to final zero-width field of a comptime value"

This reverts commit e0bc5f65b98d154b4318027d56f780b55605e33c. Caused an assertion failure when running the behavior tests: ``` zig: llvm/lib/IR/Type.cpp:729: static llvm::PointerType* llvm::PointerType::get(llvm::Type*, unsigned int): Assertion `isValidElementType(EltTy) && "Invalid type for pointer element!"' failed. Aborted (core dumped) ```

2 files changed, 20 insertions(+), 38 deletions(-)

src/codegen/llvm.zig+7-15
...@@ -4033,24 +4033,16 @@ pub const DeclGen = struct {...@@ -4033,24 +4033,16 @@ pub const DeclGen = struct {
4033 const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);4033 const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);
4034 break :blk field_addr.constIntToPtr(final_llvm_ty);4034 break :blk field_addr.constIntToPtr(final_llvm_ty);
4035 }4035 }
4036 bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module);
40364037
4037 var ty_buf: Type.Payload.Pointer = undefined;4038 var ty_buf: Type.Payload.Pointer = undefined;
40384039 const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?;
4040 const indices: [2]*llvm.Value = .{
4041 llvm_u32.constInt(0, .False),
4042 llvm_u32.constInt(llvm_field_index, .False),
4043 };
4039 const parent_llvm_ty = try dg.lowerType(parent_ty);4044 const parent_llvm_ty = try dg.lowerType(parent_ty);
4040 if (llvmFieldIndex(parent_ty, field_index, target, &ty_buf)) |llvm_field_index| {4045 break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len);
4041 bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module);
4042 const indices: [2]*llvm.Value = .{
4043 llvm_u32.constInt(0, .False),
4044 llvm_u32.constInt(llvm_field_index, .False),
4045 };
4046 break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len);
4047 } else {
4048 bitcast_needed = !parent_ty.eql(ptr_child_ty, dg.module);
4049 const indices: [1]*llvm.Value = .{
4050 llvm_u32.constInt(1, .False),
4051 };
4052 break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len);
4053 }
4054 },4046 },
4055 .Pointer => {4047 .Pointer => {
4056 assert(parent_ty.isSlice());4048 assert(parent_ty.isSlice());
test/behavior/struct.zig+13-23
...@@ -1359,33 +1359,23 @@ test "under-aligned struct field" {...@@ -1359,33 +1359,23 @@ test "under-aligned struct field" {
1359 try expect(result == 1234);1359 try expect(result == 1234);
1360}1360}
13611361
1362test "fieldParentPtr of a zero-bit field" {1362test "address of zero-bit field is equal to address of only field" {
1363 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO1363 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1364 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO1364 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1365 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO1365 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
13661366
1367 const S = struct {1367 {
1368 fn testOneType(comptime A: type) !void {1368 const A = struct { b: void = {}, u: u8 };
1369 {1369 var a = A{ .u = 0 };
1370 const a = A{ .u = 0 };1370 const a_ptr = @fieldParentPtr(A, "b", &a.b);
1371 const b_ptr = &a.b;1371 try std.testing.expectEqual(&a, a_ptr);
1372 const a_ptr = @fieldParentPtr(A, "b", b_ptr);1372 }
1373 try std.testing.expectEqual(&a, a_ptr);1373 {
1374 }1374 const A = struct { u: u8, b: void = {} };
1375 {1375 var a = A{ .u = 0 };
1376 var a = A{ .u = 0 };1376 const a_ptr = @fieldParentPtr(A, "b", &a.b);
1377 const b_ptr = &a.b;1377 try std.testing.expectEqual(&a, a_ptr);
1378 const a_ptr = @fieldParentPtr(A, "b", b_ptr);1378 }
1379 try std.testing.expectEqual(&a, a_ptr);
1380 }
1381 }
1382 fn doTheTest() !void {
1383 try testOneType(struct { b: void = {}, u: u8 });
1384 try testOneType(struct { u: u8, b: void = {} });
1385 }
1386 };
1387 try S.doTheTest();
1388 comptime try S.doTheTest();
1389}1379}
13901380
1391test "struct field has a pointer to an aligned version of itself" {1381test "struct field has a pointer to an aligned version of itself" {