mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
llvm: fix lowering pointer to final zero-width field of a comptime value
* Handle a `null` return from `llvmFieldIndex`. * Add a behavior test to test this code path. * Reword this test name, which incorrectly described how pointers to zero-bit fields behave, and instead describe the actual test.
This commit is contained in:
parent
0ccdc511ce
commit
e0bc5f65b9
@ -4033,16 +4033,24 @@ pub const DeclGen = struct {
|
|||||||
const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);
|
const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);
|
||||||
break :blk field_addr.constIntToPtr(final_llvm_ty);
|
break :blk field_addr.constIntToPtr(final_llvm_ty);
|
||||||
}
|
}
|
||||||
bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module);
|
|
||||||
|
|
||||||
var ty_buf: Type.Payload.Pointer = undefined;
|
var ty_buf: Type.Payload.Pointer = undefined;
|
||||||
const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?;
|
|
||||||
|
const parent_llvm_ty = try dg.lowerType(parent_ty);
|
||||||
|
if (llvmFieldIndex(parent_ty, field_index, target, &ty_buf)) |llvm_field_index| {
|
||||||
|
bitcast_needed = !field_ty.eql(ptr_child_ty, dg.module);
|
||||||
const indices: [2]*llvm.Value = .{
|
const indices: [2]*llvm.Value = .{
|
||||||
llvm_u32.constInt(0, .False),
|
llvm_u32.constInt(0, .False),
|
||||||
llvm_u32.constInt(llvm_field_index, .False),
|
llvm_u32.constInt(llvm_field_index, .False),
|
||||||
};
|
};
|
||||||
const parent_llvm_ty = try dg.lowerType(parent_ty);
|
|
||||||
break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len);
|
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),
|
||||||
|
};
|
||||||
|
break :blk parent_llvm_ty.constInBoundsGEP(parent_llvm_ptr, &indices, indices.len);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
.Pointer => {
|
.Pointer => {
|
||||||
assert(parent_ty.isSlice());
|
assert(parent_ty.isSlice());
|
||||||
|
|||||||
@ -1359,24 +1359,34 @@ test "under-aligned struct field" {
|
|||||||
try expect(result == 1234);
|
try expect(result == 1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "address of zero-bit field is equal to address of only field" {
|
test "fieldParentPtr of a zero-bit field" {
|
||||||
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
||||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
||||||
|
|
||||||
|
const S = struct {
|
||||||
|
fn testOneType(comptime A: type) !void {
|
||||||
{
|
{
|
||||||
const A = struct { b: void = {}, u: u8 };
|
const a = A{ .u = 0 };
|
||||||
var a = A{ .u = 0 };
|
const b_ptr = &a.b;
|
||||||
const a_ptr = @fieldParentPtr(A, "b", &a.b);
|
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||||
try std.testing.expectEqual(&a, a_ptr);
|
try std.testing.expectEqual(&a, a_ptr);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const A = struct { u: u8, b: void = {} };
|
|
||||||
var a = A{ .u = 0 };
|
var a = A{ .u = 0 };
|
||||||
const a_ptr = @fieldParentPtr(A, "b", &a.b);
|
const b_ptr = &a.b;
|
||||||
|
const a_ptr = @fieldParentPtr(A, "b", b_ptr);
|
||||||
try std.testing.expectEqual(&a, a_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 S.doTheTest();
|
||||||
|
comptime try S.doTheTest();
|
||||||
|
}
|
||||||
|
|
||||||
test "struct field has a pointer to an aligned version of itself" {
|
test "struct field has a pointer to an aligned version of itself" {
|
||||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user