Skip to content

Fix sentinel value not being set in runtime array init #23882

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5340,19 +5340,20 @@ fn zirValidatePtrArrayInit(
else => unreachable,
};

// In case of a sentinel-terminated array, the sentinel will not have been
// populated by any ZIR instructions at comptime; we need to do that here.
if (array_ty.sentinel(zcu)) |sentinel_val| {
const array_len_ref = try pt.intRef(Type.usize, array_len);
const sentinel_ptr = try sema.elemPtrArray(block, init_src, init_src, array_ptr, init_src, array_len_ref, true, true);
const sentinel = Air.internedToRef(sentinel_val.toIntern());
try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store);
}

if (block.isComptime() and
(try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
{
// In this case the comptime machinery will have evaluated the store instructions
// at comptime so we have almost nothing to do here. However, in case of a
// sentinel-terminated array, the sentinel will not have been populated by
// any ZIR instructions at comptime; we need to do that here.
if (array_ty.sentinel(zcu)) |sentinel_val| {
const array_len_ref = try pt.intRef(Type.usize, array_len);
const sentinel_ptr = try sema.elemPtrArray(block, init_src, init_src, array_ptr, init_src, array_len_ref, true, true);
const sentinel = Air.internedToRef(sentinel_val.toIntern());
try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store);
}
// In this case the comptime machinery will have evaluated the store
// instructions at comptime so we have nothing to do here
return;
}

Expand Down
9 changes: 9 additions & 0 deletions test/behavior/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,15 @@ test "runtime initialized sentinel-terminated array literal" {
try std.testing.expect(g[3] == 0x99);
}

test "runtime initialized sentinel-terminated array literal with result type" {
var c: u16 = 300;
_ = &c;
const f: [1: 0x9999]u16 = .{c};
const g: *const [4]u8 = @ptrCast(&f);
try std.testing.expect(g[2] == 0x99);
try std.testing.expect(g[3] == 0x99);
}

test "array of array agregate init" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
Expand Down
Loading