From badf016b83583df98ac9c56e4147235fb9bd7f8c Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Mon, 19 Oct 2026 23:25:29 -0700 Subject: [PATCH] InternPool: include sentinel when checking for repeating elems Without including the sentinel in this check, a sentinel terminated array with repeated elements up to the sentinel would get interned as a repeated aggregate, meaning that when the value would get uninterned for mutation, all values including the sentinel would get set to the repeated element, even though the sentinel is *not* repeated. Without this fix, `x.a[2]` in the added test case would end up with the value 50. Closes #37339 --- src/InternPool.zig | 2 +- test/behavior/comptime_memory.zig | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/InternPool.zig b/src/InternPool.zig index 88c454fc2f3b414710886a524c80f4ecc7b09134..7df03f7dca76b86b765068a22f9e150d0f537e7b 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -7901,7 +7901,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, key: switch (aggregate.storage) { .bytes => |bytes| for (bytes.toSlice(len, ip)[1..]) |byte| if (byte != bytes.at(0, ip)) break :repeated, - .elems => |elems| for (elems[1..@intCast(len)]) |elem| + .elems => |elems| for (elems[1..@intCast(len_including_sentinel)]) |elem| if (elem != elems[0]) break :repeated, .repeated_elem => {}, } diff --git a/test/behavior/comptime_memory.zig b/test/behavior/comptime_memory.zig index 63044d026e18e7d77e9e049805867d8dc6749fc2..f72669473778d517cad3200d88fd304d25c4a92a 100644 --- a/test/behavior/comptime_memory.zig +++ b/test/behavior/comptime_memory.zig @@ -606,3 +606,15 @@ test "reinterpret pointer as optional pointer via double-pointer coercion" { comptime assert(@TypeOf(loaded) == ?*anyopaque); comptime assert(@intFromPtr(loaded) == 0x1000); } + +test "comptime store to sentinel-terminated array in an extern struct" { + comptime { + var x: extern struct { + a: [2:0]u8 = @splat(50), + } = .{}; + x.a[0] = 100; + assert(x.a[0] == 100); + assert(x.a[1] == 50); + assert(x.a[2] == 0); + } +} -- 2.54.0