//! Ensure that a box with a custom allocator detects when the pointer is dangling. #![feature(allocator_api)] // This should not need the aliasing model. //@compile-flags: -Zmiri-disable-stacked-borrows use std::alloc::Layout; use std::ptr::NonNull; #[allow(unused)] struct MyAlloc(usize, usize); // make sure `Box` is an `Aggregate` unsafe impl std::alloc::Allocator for MyAlloc { fn allocate(&self, _layout: Layout) -> Result, std::alloc::AllocError> { unimplemented!() } unsafe fn deallocate(&self, _ptr: NonNull, _layout: Layout) { unimplemented!() } } #[repr(C)] struct MyBox { ptr: NonNull, alloc: MyAlloc, } fn main() { let b = MyBox { ptr: NonNull::::dangling(), alloc: MyAlloc(0, 0) }; let _b: Box = unsafe { std::mem::transmute(b) //~ERROR: dangling box }; }