From d5446652fa22db2e771be9af6ab755e8b199b5c9 Mon Sep 17 00:00:00 2001 From: David Rubin Date: Tue, 9 Jun 2026 17:45:19 -0700 Subject: [PATCH] Sema: correctly copy pointer attributes for optional pointer payload --- src/Sema.zig | 10 ++++------ test/behavior/optional.zig | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 57dbc08c2a705d636f7a373709b8d7ee38265553..faa8ab2abade89054e315434d2bc5182328488ba 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -7956,12 +7956,10 @@ fn analyzeOptionalPayloadPtr( } const child_type = opt_type.optionalChild(zcu); - const child_pointer = try pt.ptrType(.{ - .child = child_type.toIntern(), - .flags = .{ - .is_const = optional_ptr_ty.isConstPtr(zcu), - .address_space = optional_ptr_ty.ptrAddressSpace(zcu), - }, + const child_pointer = try pt.ptrType(info: { + var new = optional_ptr_ty.ptrInfo(zcu); + new.child = child_type.toIntern(); + break :info new; }); if (try sema.resolveDefinedValue(block, src, optional_ptr)) |ptr_val| { diff --git a/test/behavior/optional.zig b/test/behavior/optional.zig index c6ee96983a68aba0466984bf0565ff28c1094976..e812cd4c08b655cd2d21ad61cc8c4fd2fffd17d7 100644 --- a/test/behavior/optional.zig +++ b/test/behavior/optional.zig @@ -665,3 +665,14 @@ test "global comptime only optional" { assert(S.void.?.* == void); } } + +test "optional ptr payload alignment" { + const S = struct { + fn doTheTest(p: *align(1) ?u32) !void { + comptime assert(@TypeOf(&p.*.?) == *align(1) u32); + try expect(p.*.? == 10); + } + }; + var x: ?u32 = 10; + try S.doTheTest(&x); +} -- 2.54.0