diff --git a/src/Sema.zig b/src/Sema.zig index a6a49d96c94975fc7782c21ffcb99f5fced19774..57dbc08c2a705d636f7a373709b8d7ee38265553 100644 --- 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 index 8506f217412363a63393006327c55f3372b63250..a30469821a3a279766dd6302bbc2272c7dfa826f 100644 --- 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); +}