diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 0ce2beb63d6..5cf190423e3 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -170,6 +170,13 @@ impl RawVec { fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self { if mem::size_of::() == 0 { Self::new_in(alloc) + } else if capacity == 0 { + // Don't allocate here because `Drop` will not deallocate when `capacity` is 0. + Self { + ptr: unsafe { Unique::new_unchecked(NonNull::dangling().as_ptr()) }, + cap: capacity, + alloc, + } } else { // We avoid `unwrap_or_else` here because it bloats the amount of // LLVM IR generated.