From bf1d83482b581e49499e545cc625155c1043ef0c Mon Sep 17 00:00:00 2001 From: pfg Date: Wed, 2 Sep 2020 22:28:37 -0700 Subject: [PATCH 1/2] "Support" allocating 0 bit types --- lib/std/mem/Allocator.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index bb59de2a7e..f14373970a 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -159,7 +159,7 @@ fn moveBytes( /// Returns a pointer to undefined memory. /// Call `destroy` with the result to free the memory. pub fn create(self: *Allocator, comptime T: type) Error!*T { - if (@sizeOf(T) == 0) return &(T{}); + if (@sizeOf(T) == 0) return @as(*T, undefined); const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress()); return &slice[0]; } From abe672956ed037077b23ac6aa29df3dd99795539 Mon Sep 17 00:00:00 2001 From: pfg Date: Thu, 3 Sep 2020 16:33:47 -0700 Subject: [PATCH 2/2] Test 0 bit allocation --- lib/std/heap.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/std/heap.zig b/lib/std/heap.zig index d6977f2f9c..492eb40656 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -915,6 +915,10 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void { testing.expect(slice.len == 10); allocator.free(slice); + + const zero_bit_ptr = try allocator.create(u0); + zero_bit_ptr.* = 0; + allocator.destroy(zero_bit_ptr); } pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {