Skip to content

Allow .ptr on pointer to array with ref result location #19416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27196,6 +27196,37 @@ fn fieldPtr(
if (ip.stringEqlSlice(field_name, "len")) {
const int_val = try mod.intValue(Type.usize, inner_ty.arrayLen(mod));
return anonDeclRef(sema, int_val.toIntern());
} else if (ip.stringEqlSlice(field_name, "ptr") and is_pointer_to) {
const ptr_info = object_ty.ptrInfo(mod);
const new_ptr_ty = try sema.ptrType(.{
.child = Type.fromInterned(ptr_info.child).childType(mod).toIntern(),
.sentinel = if (object_ty.sentinel(mod)) |s| s.toIntern() else .none,
.flags = .{
.size = .Many,
.alignment = ptr_info.flags.alignment,
.is_const = ptr_info.flags.is_const,
.is_volatile = ptr_info.flags.is_volatile,
.is_allowzero = ptr_info.flags.is_allowzero,
.address_space = ptr_info.flags.address_space,
.vector_index = ptr_info.flags.vector_index,
},
.packed_offset = ptr_info.packed_offset,
});
const ptr_ptr_info = object_ptr_ty.ptrInfo(mod);
const result_ty = try sema.ptrType(.{
.child = new_ptr_ty.toIntern(),
.sentinel = if (object_ptr_ty.sentinel(mod)) |s| s.toIntern() else .none,
.flags = .{
.alignment = ptr_ptr_info.flags.alignment,
.is_const = ptr_ptr_info.flags.is_const,
.is_volatile = ptr_ptr_info.flags.is_volatile,
.is_allowzero = ptr_ptr_info.flags.is_allowzero,
.address_space = ptr_ptr_info.flags.address_space,
.vector_index = ptr_ptr_info.flags.vector_index,
},
.packed_offset = ptr_ptr_info.packed_offset,
});
return sema.bitCast(block, result_ty, object_ptr, src, null);
} else {
return sema.fail(
block,
Expand Down
2 changes: 1 addition & 1 deletion test/behavior/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ test "pointer to array has ptr field" {
try std.testing.expect(arr.ptr[1] == 20);
try std.testing.expect(arr.ptr[2] == 30);
try std.testing.expect(arr.ptr[3] == 40);
try std.testing.expect(arr.ptr[4] == 50);
try std.testing.expect((&arr.ptr).*[4] == 50);
}

test "discarded array init preserves result location" {
Expand Down