Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ pub struct BTreeMap<
#[stable(feature = "btree_drop", since = "1.7.0")]
unsafe impl<#[may_dangle] K, #[may_dangle] V, A: Allocator + Clone> Drop for BTreeMap<K, V, A> {
fn drop(&mut self) {
drop(unsafe { ptr::read(self) }.into_iter())
if self.root.is_some() {
drop(unsafe { ptr::read(self) }.into_iter())
} else {
unsafe { ManuallyDrop::drop(&mut self.alloc) }
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/codegen-llvm/btree-empty-drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Regression test for https://github.com/rust-lang/rust/issues/161375
// Checking that an empty BTree's drop is optimized away.
//@ compile-flags: -Copt-level=3

#![crate_type = "lib"]

use std::collections::BTreeMap;

// CHECK-LABEL: @drop_btree
// CHECK-NOT: dying_next

@clarfonthey clarfonthey Aug 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would rather this check for calls to dealloc rather than dying_next, since I this test could easily become worthless if this method is renamed.

View changes since the review

// CHECK: ret void
#[no_mangle]
pub fn drop_btree() {
let _ = BTreeMap::<(), ()>::new();
}
Loading