about summary refs log tree commit diff
path: root/tests/ui/drop
diff options
context:
space:
mode:
authorbeepster4096 <19316085+beepster4096@users.noreply.github.com>2025-07-08 15:01:54 -0700
committerbeepster4096 <19316085+beepster4096@users.noreply.github.com>2025-07-25 13:19:43 -0700
commitdad982633c935ae70fbc5f9b1c0f4f845606c551 (patch)
tree0ad28e2192dc4c27f1503ab991544edae734b720 /tests/ui/drop
parenta955f1cd09a027363729ceed919952d09f76f28e (diff)
downloadrust-dad982633c935ae70fbc5f9b1c0f4f845606c551.tar.gz
rust-dad982633c935ae70fbc5f9b1c0f4f845606c551.zip
fix box destructor generation
Diffstat (limited to 'tests/ui/drop')
-rw-r--r--tests/ui/drop/box-conditional-drop-allocator.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/ui/drop/box-conditional-drop-allocator.rs b/tests/ui/drop/box-conditional-drop-allocator.rs
new file mode 100644
index 00000000000..8f78da16473
--- /dev/null
+++ b/tests/ui/drop/box-conditional-drop-allocator.rs
@@ -0,0 +1,43 @@
+//@ run-pass
+#![feature(allocator_api)]
+
+// Regression test for #131082.
+// Testing that the allocator of a Box is dropped in conditional drops
+
+use std::alloc::{AllocError, Allocator, Global, Layout};
+use std::cell::Cell;
+use std::ptr::NonNull;
+
+struct DropCheckingAllocator<'a>(&'a Cell<bool>);
+
+unsafe impl Allocator for DropCheckingAllocator<'_> {
+    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
+        Global.allocate(layout)
+    }
+    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
+        Global.deallocate(ptr, layout);
+    }
+}
+impl Drop for DropCheckingAllocator<'_> {
+    fn drop(&mut self) {
+        self.0.set(true);
+    }
+}
+
+struct HasDrop;
+impl Drop for HasDrop {
+    fn drop(&mut self) {}
+}
+
+fn main() {
+    let dropped = Cell::new(false);
+    {
+        let b = Box::new_in(HasDrop, DropCheckingAllocator(&dropped));
+        if true {
+            drop(*b);
+        } else {
+            drop(b);
+        }
+    }
+    assert!(dropped.get());
+}