Skip to content
Closed
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