zig

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

commit e6686ae00d8d4b4b104037f3afa6b0b603c7d708 (tree)
parent 2db133b53f80268e4e36ea43a58340bc7853bb61
Author: David Rubin <sinon@vortan.dev>
Date:   Tue,  9 Jun 2026 18:06:38 -0700

Sema: copy alignment flags to pointer of slice field

Diffstat:
Msrc/Sema.zig | 22++--------------------
Mtest/behavior/slice.zig | 12++++++++++++
2 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -26283,17 +26283,7 @@ fn fieldPtr( const attr_ptr_ty = if (is_pointer_to) object_ty else object_ptr_ty; if (field_name.eqlSlice("ptr", ip)) { - const slice_ptr_ty = inner_ty.slicePtrFieldType(zcu); - - const result_ty = try pt.ptrType(.{ - .child = slice_ptr_ty.toIntern(), - .flags = .{ - .is_const = !attr_ptr_ty.ptrIsMutable(zcu), - .is_volatile = attr_ptr_ty.isVolatilePtr(zcu), - .address_space = attr_ptr_ty.ptrAddressSpace(zcu), - }, - }); - + const result_ty = try attr_ptr_ty.fieldPtrType(Value.slice_ptr_index, pt); if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| { return Air.internedToRef((try val.ptrField(Value.slice_ptr_index, pt)).toIntern()); } @@ -26303,15 +26293,7 @@ fn fieldPtr( try sema.checkKnownAllocPtr(block, inner_ptr, field_ptr); return field_ptr; } else if (field_name.eqlSlice("len", ip)) { - const result_ty = try pt.ptrType(.{ - .child = .usize_type, - .flags = .{ - .is_const = !attr_ptr_ty.ptrIsMutable(zcu), - .is_volatile = attr_ptr_ty.isVolatilePtr(zcu), - .address_space = attr_ptr_ty.ptrAddressSpace(zcu), - }, - }); - + const result_ty = try attr_ptr_ty.fieldPtrType(Value.slice_len_index, pt); if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| { return Air.internedToRef((try val.ptrField(Value.slice_len_index, pt)).toIntern()); } diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig @@ -1083,3 +1083,15 @@ test "conditionally return second argument slice" { try expectEqualStrings("", S.foo(false, "false")); try expectEqualStrings("true", S.foo(true, "true")); } + +test "slice field alignment" { + const S = struct { + fn doTheTest(p: *align(1) const []u8) !void { + comptime assert(@TypeOf(&p.ptr) == *align(1) const [*]u8); + comptime assert(@TypeOf(&p.len) == *align(1) const usize); + try expect(p.len == 10); + } + }; + var arr: [10]u8 = @splat(0); + try S.doTheTest(&&arr); +}