commit f3544a707941269ec3ed9145ee1432df5a01a10a (tree)
parent 92505a7de7f720acc74906a053cd89a33174eb1f
Author: Robbie Lyman <rb.lymn@gmail.com>
Date: Mon, 22 Jun 2026 08:49:46 -0400
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
Diffstat:
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig
@@ -445,7 +445,10 @@ pub fn reallocAdvanced(
/// To free a single item, see `destroy`.
pub fn free(self: Allocator, memory: anytype) void {
const slice_info = @typeInfo(@TypeOf(memory)).pointer;
- comptime assert(slice_info.size == .slice);
+ if (slice_info.size != .slice) {
+ // slicing with comptime-known start and end results in *[len]T, which may be free'd
+ comptime assert(slice_info.size == .one and @typeInfo(slice_info.child) == .array);
+ }
const bytes: []u8 = @ptrCast(@constCast(mem.absorbSentinel(memory)));
if (bytes.len == 0) return;
@memset(bytes, undefined);