Skip to content

Commit eb443f5

Browse files
committed
Sema: allow .ptr on pointer to array
1 parent 9183701 commit eb443f5

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Sema.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27074,6 +27074,37 @@ fn fieldPtr(
2707427074
if (ip.stringEqlSlice(field_name, "len")) {
2707527075
const int_val = try mod.intValue(Type.usize, inner_ty.arrayLen(mod));
2707627076
return anonDeclRef(sema, int_val.toIntern());
27077+
} else if (ip.stringEqlSlice(field_name, "ptr") and is_pointer_to) {
27078+
const ptr_info = object_ty.ptrInfo(mod);
27079+
const new_ptr_ty = try sema.ptrType(.{
27080+
.child = Type.fromInterned(ptr_info.child).childType(mod).toIntern(),
27081+
.sentinel = if (object_ty.sentinel(mod)) |s| s.toIntern() else .none,
27082+
.flags = .{
27083+
.size = .Many,
27084+
.alignment = ptr_info.flags.alignment,
27085+
.is_const = ptr_info.flags.is_const,
27086+
.is_volatile = ptr_info.flags.is_volatile,
27087+
.is_allowzero = ptr_info.flags.is_allowzero,
27088+
.address_space = ptr_info.flags.address_space,
27089+
.vector_index = ptr_info.flags.vector_index,
27090+
},
27091+
.packed_offset = ptr_info.packed_offset,
27092+
});
27093+
const ptr_ptr_info = object_ptr_ty.ptrInfo(mod);
27094+
const result_ty = try sema.ptrType(.{
27095+
.child = new_ptr_ty.toIntern(),
27096+
.sentinel = if (object_ptr_ty.sentinel(mod)) |s| s.toIntern() else .none,
27097+
.flags = .{
27098+
.alignment = ptr_ptr_info.flags.alignment,
27099+
.is_const = ptr_ptr_info.flags.is_const,
27100+
.is_volatile = ptr_ptr_info.flags.is_volatile,
27101+
.is_allowzero = ptr_ptr_info.flags.is_allowzero,
27102+
.address_space = ptr_ptr_info.flags.address_space,
27103+
.vector_index = ptr_ptr_info.flags.vector_index,
27104+
},
27105+
.packed_offset = ptr_ptr_info.packed_offset,
27106+
});
27107+
return sema.bitCast(block, result_ty, object_ptr, src, null);
2707727108
} else {
2707827109
return sema.fail(
2707927110
block,

test/behavior/array.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ test "pointer to array has ptr field" {
709709
try std.testing.expect(arr.ptr[1] == 20);
710710
try std.testing.expect(arr.ptr[2] == 30);
711711
try std.testing.expect(arr.ptr[3] == 40);
712-
try std.testing.expect(arr.ptr[4] == 50);
712+
try std.testing.expect((&arr.ptr).*[4] == 50);
713713
}
714714

715715
test "discarded array init preserves result location" {

0 commit comments

Comments
 (0)