Commit 7d8a556ba9

Alex Rønne Petersen <alex@alexrp.com>
2025-03-25 13:45:10
Merge pull request #23220 from samy-00007/bytesAsSlice-fix
Minor fix for `Allocator.remap` and `mem.bytesAsSlice` for zero-sized types
1 parent 1aca3dd
Changed files (3)
lib/std/json/static_test.zig
@@ -925,3 +925,19 @@ test "parse at comptime" {
     };
     comptime testing.expectEqual(@as(u64, 9999), config.uptime) catch unreachable;
 }
+
+test "parse with zero-bit field" {
+    const str =
+        \\{
+        \\    "a": ["a", "a"],
+        \\    "b": "a"
+        \\}
+    ;
+    const ZeroSizedEnum = enum { a };
+    try testing.expectEqual(0, @sizeOf(ZeroSizedEnum));
+
+    const Inner = struct { a: []const ZeroSizedEnum, b: ZeroSizedEnum };
+    const expected: Inner = .{ .a = &.{ .a, .a }, .b = .a };
+
+    try testAllParseFunctions(Inner, expected, str);
+}
lib/std/mem/Allocator.zig
@@ -312,12 +312,15 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool {
 /// unless `new_len` is also 0, in which case `allocation` is returned.
 ///
 /// `new_len` may be zero, in which case the allocation is freed.
+///
+/// If the allocation's elements' type is zero bytes sized, `allocation.len` is set to `new_len`.
 pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: {
     const Slice = @typeInfo(@TypeOf(allocation)).pointer;
     break :t ?[]align(Slice.alignment) Slice.child;
 } {
     const Slice = @typeInfo(@TypeOf(allocation)).pointer;
     const T = Slice.child;
+
     const alignment = Slice.alignment;
     if (new_len == 0) {
         self.free(allocation);
@@ -326,6 +329,11 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: {
     if (allocation.len == 0) {
         return null;
     }
+    if (@sizeOf(T) == 0) {
+        var new_memory = allocation;
+        new_memory.len = new_len;
+        return new_memory;
+    }
     const old_memory = mem.sliceAsBytes(allocation);
     // I would like to use saturating multiplication here, but LLVM cannot lower it
     // on WebAssembly: https://github.com/ziglang/zig/issues/9660
lib/std/mem.zig
@@ -228,6 +228,18 @@ test "Allocator.resize" {
     }
 }
 
+test "Allocator alloc and remap with zero-bit type" {
+    var values = try testing.allocator.alloc(void, 10);
+    defer testing.allocator.free(values);
+
+    try testing.expectEqual(10, values.len);
+    const remaped = testing.allocator.remap(values, 200);
+    try testing.expect(remaped != null);
+
+    values = remaped.?;
+    try testing.expectEqual(200, values.len);
+}
+
 /// Copy all of source into dest at position 0.
 /// dest.len must be >= source.len.
 /// If the slices overlap, dest.ptr must be <= src.ptr.
@@ -4207,10 +4219,11 @@ fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type {
 
 /// Given a slice of bytes, returns a slice of the specified type
 /// backed by those bytes, preserving pointer attributes.
+/// If `T` is zero-bytes sized, the returned slice has a len of zero.
 pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T, @TypeOf(bytes)) {
     // let's not give an undefined pointer to @ptrCast
     // it may be equal to zero and fail a null check
-    if (bytes.len == 0) {
+    if (bytes.len == 0 or @sizeOf(T) == 0) {
         return &[0]T{};
     }
 
@@ -4288,6 +4301,19 @@ test "bytesAsSlice preserves pointer attributes" {
     try testing.expectEqual(in.alignment, out.alignment);
 }
 
+test "bytesAsSlice with zero-bit element type" {
+    {
+        const bytes = [_]u8{};
+        const slice = bytesAsSlice(void, &bytes);
+        try testing.expectEqual(0, slice.len);
+    }
+    {
+        const bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 };
+        const slice = bytesAsSlice(u0, &bytes);
+        try testing.expectEqual(0, slice.len);
+    }
+}
+
 fn SliceAsBytesReturnType(comptime Slice: type) type {
     return CopyPtrAttrs(Slice, .slice, u8);
 }