authorgravatar for rb.lymn@gmail.comRobbie Lyman <rb.lymn@gmail.com> 2026-06-22 08:49:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-25 02:09:34+02:00
logf3544a707941269ec3ed9145ee1432df5a01a10a
tree364cc3f69d47552d7cacf086ababc054e043979e
parent92505a7de7f720acc74906a053cd89a33174eb1f

fix: Allocator contract should allow free on single-pointer-to-array

Currently, when calling `free` a slice created by slicing a pointer with comptime-known start and end, a compile-time assertion is hit, because the slicing results in `*[len]T` rather than the expected `[]T`. This commit allows `Allocator.free` to be called on such pointers as well. closes #35712

1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/mem/Allocator.zig+4-1
...@@ -445,7 +445,10 @@ pub fn reallocAdvanced(...@@ -445,7 +445,10 @@ pub fn reallocAdvanced(
445/// To free a single item, see `destroy`.445/// To free a single item, see `destroy`.
446pub fn free(self: Allocator, memory: anytype) void {446pub fn free(self: Allocator, memory: anytype) void {
447 const slice_info = @typeInfo(@TypeOf(memory)).pointer;447 const slice_info = @typeInfo(@TypeOf(memory)).pointer;
448 comptime assert(slice_info.size == .slice);448 if (slice_info.size != .slice) {
449 // slicing with comptime-known start and end results in *[len]T, which may be free'd
450 comptime assert(slice_info.size == .one and @typeInfo(slice_info.child) == .array);
451 }
449 const bytes: []u8 = @ptrCast(@constCast(mem.absorbSentinel(memory)));452 const bytes: []u8 = @ptrCast(@constCast(mem.absorbSentinel(memory)));
450 if (bytes.len == 0) return;453 if (bytes.len == 0) return;
451 @memset(bytes, undefined);454 @memset(bytes, undefined);