Closed
Description
I tried this code:
#![feature(allocator_api)]
#![feature(linked_list_remove)]
use std::{
alloc::{AllocError, Allocator, Layout},
collections::LinkedList,
process::abort,
ptr::NonNull,
};
struct M;
unsafe impl Allocator for M {
fn allocate(&self, layout: std::alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
unsafe {
if layout.align() > std::mem::align_of::<usize>() {
abort();
}
let ptr = libc::malloc(layout.size()) as *mut u8;
if ptr.is_null() {
abort();
}
println!("alloc: {:?}", ptr);
let nonnull = NonNull::new_unchecked(ptr);
Ok(NonNull::slice_from_raw_parts(nonnull, layout.size()))
}
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, _layout: Layout) {
unsafe {
println!("dealloc: {:?}", ptr);
libc::free(ptr.as_ptr() as _);
}
}
}
fn main() {
let alloc = &M;
let mut list = LinkedList::new_in(alloc);
list.push_back(5);
list.remove(0);
}
I expected to see this happen: alloc and dealloc is printed
Instead, this happened: only alloc is printed
Meta
rustc --version --verbose
:
rustc 1.80.0-nightly (f67a1acc0 2024-06-01)
binary: rustc
commit-hash: f67a1acc04c7ecbf05751b17592dd8d245b75256
commit-date: 2024-06-01
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6
LinkedList::remove
forgets that it's been initialized with an allocator, and always deletes the node with the global allocator.
rust/library/alloc/src/collections/linked_list.rs
Line 1708 in 1689a5a
There seems to be one more place where this happens in the same file.