diff --git a/src/Sema.zig b/src/Sema.zig index 8889b475fd18..56129c678555 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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; } diff --git a/test/behavior/array.zig b/test/behavior/array.zig index a258c49d89da..113f91bb24f4 100644 --- a/test/behavior/array.zig +++ b/test/behavior/array.zig @@ -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