aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/pointers.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/behavior/pointers.zig')
-rw-r--r--test/behavior/pointers.zig36
1 files changed, 36 insertions, 0 deletions
diff --git a/test/behavior/pointers.zig b/test/behavior/pointers.zig
index 2539dd886d..27c6403e77 100644
--- a/test/behavior/pointers.zig
+++ b/test/behavior/pointers.zig
@@ -621,3 +621,39 @@ test "cast pointers with zero sized elements" {
const d: []u8 = c;
_ = d;
}
+
+test "comptime pointer equality through distinct fields with well-defined layout" {
+ const A = extern struct {
+ x: u32,
+ z: u16,
+ };
+ const B = extern struct {
+ x: u16,
+ y: u16,
+ z: u16,
+ };
+
+ const a: A = .{
+ .x = undefined,
+ .z = 123,
+ };
+
+ const ap: *const A = &a;
+ const bp: *const B = @ptrCast(ap);
+
+ comptime assert(&ap.z == &bp.z);
+ comptime assert(ap.z == 123);
+ comptime assert(bp.z == 123);
+}
+
+test "comptime pointer equality through distinct elements with well-defined layout" {
+ const buf: [2]u32 = .{ 123, 456 };
+
+ const ptr: *const [2]u32 = &buf;
+ const byte_ptr: *align(4) const [8]u8 = @ptrCast(ptr);
+ const second_elem: *const u32 = @ptrCast(byte_ptr[4..8]);
+
+ comptime assert(&buf[1] == second_elem);
+ comptime assert(buf[1] == 456);
+ comptime assert(second_elem.* == 456);
+}