Skip to content

Commit 272480e

Browse files
authored
Merge branch 'master' into Issue-107957-black_box_docs
2 parents 7288a48 + c027c9d commit 272480e

File tree

225 files changed

+9814
-7575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+9814
-7575
lines changed

alloc/src/boxed.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl<T> Box<T> {
214214
#[inline(always)]
215215
#[stable(feature = "rust1", since = "1.0.0")]
216216
#[must_use]
217+
#[rustc_diagnostic_item = "box_new"]
217218
pub fn new(x: T) -> Self {
218219
#[rustc_box]
219220
Box::new(x)
@@ -283,9 +284,7 @@ impl<T> Box<T> {
283284
#[must_use]
284285
#[inline(always)]
285286
pub fn pin(x: T) -> Pin<Box<T>> {
286-
(#[rustc_box]
287-
Box::new(x))
288-
.into()
287+
Box::new(x).into()
289288
}
290289

291290
/// Allocates memory on the heap then places `x` into it,
@@ -1242,8 +1241,8 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
12421241
#[stable(feature = "rust1", since = "1.0.0")]
12431242
impl<T: Default> Default for Box<T> {
12441243
/// Creates a `Box<T>`, with the `Default` value for T.
1244+
#[inline]
12451245
fn default() -> Self {
1246-
#[rustc_box]
12471246
Box::new(T::default())
12481247
}
12491248
}
@@ -1252,6 +1251,7 @@ impl<T: Default> Default for Box<T> {
12521251
#[stable(feature = "rust1", since = "1.0.0")]
12531252
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
12541253
impl<T> const Default for Box<[T]> {
1254+
#[inline]
12551255
fn default() -> Self {
12561256
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
12571257
Box(ptr, Global)
@@ -1262,6 +1262,7 @@ impl<T> const Default for Box<[T]> {
12621262
#[stable(feature = "default_box_extra", since = "1.17.0")]
12631263
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
12641264
impl const Default for Box<str> {
1265+
#[inline]
12651266
fn default() -> Self {
12661267
// SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
12671268
let ptr: Unique<str> = unsafe {
@@ -1616,7 +1617,6 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
16161617
/// println!("{boxed:?}");
16171618
/// ```
16181619
fn from(array: [T; N]) -> Box<[T]> {
1619-
#[rustc_box]
16201620
Box::new(array)
16211621
}
16221622
}

alloc/src/collections/binary_heap/mod.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,18 +851,30 @@ impl<T: Ord> BinaryHeap<T> {
851851
where
852852
F: FnMut(&T) -> bool,
853853
{
854-
let mut first_removed = self.len();
854+
struct RebuildOnDrop<'a, T: Ord> {
855+
heap: &'a mut BinaryHeap<T>,
856+
first_removed: usize,
857+
}
858+
859+
let mut guard = RebuildOnDrop { first_removed: self.len(), heap: self };
860+
855861
let mut i = 0;
856-
self.data.retain(|e| {
862+
guard.heap.data.retain(|e| {
857863
let keep = f(e);
858-
if !keep && i < first_removed {
859-
first_removed = i;
864+
if !keep && i < guard.first_removed {
865+
guard.first_removed = i;
860866
}
861867
i += 1;
862868
keep
863869
});
864-
// data[0..first_removed] is untouched, so we only need to rebuild the tail:
865-
self.rebuild_tail(first_removed);
870+
871+
impl<'a, T: Ord> Drop for RebuildOnDrop<'a, T> {
872+
fn drop(&mut self) {
873+
// data[..first_removed] is untouched, so we only need to
874+
// rebuild the tail:
875+
self.heap.rebuild_tail(self.first_removed);
876+
}
877+
}
866878
}
867879
}
868880

@@ -1456,6 +1468,20 @@ impl<T> ExactSizeIterator for IntoIter<T> {
14561468
#[stable(feature = "fused", since = "1.26.0")]
14571469
impl<T> FusedIterator for IntoIter<T> {}
14581470

1471+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1472+
impl<T> Default for IntoIter<T> {
1473+
/// Creates an empty `binary_heap::IntoIter`.
1474+
///
1475+
/// ```
1476+
/// # use std::collections::binary_heap;
1477+
/// let iter: binary_heap::IntoIter<u8> = Default::default();
1478+
/// assert_eq!(iter.len(), 0);
1479+
/// ```
1480+
fn default() -> Self {
1481+
IntoIter { iter: Default::default() }
1482+
}
1483+
}
1484+
14591485
// In addition to the SAFETY invariants of the following three unsafe traits
14601486
// also refer to the vec::in_place_collect module documentation to get an overview
14611487
#[unstable(issue = "none", feature = "inplace_iteration")]

alloc/src/collections/binary_heap/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,25 @@ fn test_retain() {
474474
assert!(a.is_empty());
475475
}
476476

477+
#[test]
478+
fn test_retain_catch_unwind() {
479+
let mut heap = BinaryHeap::from(vec![3, 1, 2]);
480+
481+
// Removes the 3, then unwinds out of retain.
482+
let _ = catch_unwind(AssertUnwindSafe(|| {
483+
heap.retain(|e| {
484+
if *e == 1 {
485+
panic!();
486+
}
487+
false
488+
});
489+
}));
490+
491+
// Naively this would be [1, 2] (an invalid heap) if BinaryHeap delegates to
492+
// Vec's retain impl and then does not rebuild the heap after that unwinds.
493+
assert_eq!(heap.into_vec(), [2, 1]);
494+
}
495+
477496
// old binaryheap failed this test
478497
//
479498
// Integrity means that all elements are present after a comparison panics,

alloc/src/collections/btree/map.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,20 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
362362
}
363363
}
364364

365+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
366+
impl<'a, K: 'a, V: 'a> Default for Iter<'a, K, V> {
367+
/// Creates an empty `btree_map::Iter`.
368+
///
369+
/// ```
370+
/// # use std::collections::btree_map;
371+
/// let iter: btree_map::Iter<'_, u8, u8> = Default::default();
372+
/// assert_eq!(iter.len(), 0);
373+
/// ```
374+
fn default() -> Self {
375+
Iter { range: Default::default(), length: 0 }
376+
}
377+
}
378+
365379
/// A mutable iterator over the entries of a `BTreeMap`.
366380
///
367381
/// This `struct` is created by the [`iter_mut`] method on [`BTreeMap`]. See its
@@ -386,6 +400,20 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
386400
}
387401
}
388402

403+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
404+
impl<'a, K: 'a, V: 'a> Default for IterMut<'a, K, V> {
405+
/// Creates an empty `btree_map::IterMut`.
406+
///
407+
/// ```
408+
/// # use std::collections::btree_map;
409+
/// let iter: btree_map::IterMut<'_, u8, u8> = Default::default();
410+
/// assert_eq!(iter.len(), 0);
411+
/// ```
412+
fn default() -> Self {
413+
IterMut { range: Default::default(), length: 0, _marker: PhantomData {} }
414+
}
415+
}
416+
389417
/// An owning iterator over the entries of a `BTreeMap`.
390418
///
391419
/// This `struct` is created by the [`into_iter`] method on [`BTreeMap`]
@@ -421,6 +449,23 @@ impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for IntoIter<K, V, A> {
421449
}
422450
}
423451

452+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
453+
impl<K, V, A> Default for IntoIter<K, V, A>
454+
where
455+
A: Allocator + Default + Clone,
456+
{
457+
/// Creates an empty `btree_map::IntoIter`.
458+
///
459+
/// ```
460+
/// # use std::collections::btree_map;
461+
/// let iter: btree_map::IntoIter<u8, u8> = Default::default();
462+
/// assert_eq!(iter.len(), 0);
463+
/// ```
464+
fn default() -> Self {
465+
IntoIter { range: Default::default(), length: 0, alloc: Default::default() }
466+
}
467+
}
468+
424469
/// An iterator over the keys of a `BTreeMap`.
425470
///
426471
/// This `struct` is created by the [`keys`] method on [`BTreeMap`]. See its
@@ -1768,6 +1813,20 @@ impl<K, V> Clone for Keys<'_, K, V> {
17681813
}
17691814
}
17701815

1816+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1817+
impl<K, V> Default for Keys<'_, K, V> {
1818+
/// Creates an empty `btree_map::Keys`.
1819+
///
1820+
/// ```
1821+
/// # use std::collections::btree_map;
1822+
/// let iter: btree_map::Keys<'_, u8, u8> = Default::default();
1823+
/// assert_eq!(iter.len(), 0);
1824+
/// ```
1825+
fn default() -> Self {
1826+
Keys { inner: Default::default() }
1827+
}
1828+
}
1829+
17711830
#[stable(feature = "rust1", since = "1.0.0")]
17721831
impl<'a, K, V> Iterator for Values<'a, K, V> {
17731832
type Item = &'a V;
@@ -1809,6 +1868,20 @@ impl<K, V> Clone for Values<'_, K, V> {
18091868
}
18101869
}
18111870

1871+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
1872+
impl<K, V> Default for Values<'_, K, V> {
1873+
/// Creates an empty `btree_map::Values`.
1874+
///
1875+
/// ```
1876+
/// # use std::collections::btree_map;
1877+
/// let iter: btree_map::Values<'_, u8, u8> = Default::default();
1878+
/// assert_eq!(iter.len(), 0);
1879+
/// ```
1880+
fn default() -> Self {
1881+
Values { inner: Default::default() }
1882+
}
1883+
}
1884+
18121885
/// An iterator produced by calling `drain_filter` on BTreeMap.
18131886
#[unstable(feature = "btree_drain_filter", issue = "70530")]
18141887
pub struct DrainFilter<
@@ -1945,6 +2018,20 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
19452018
}
19462019
}
19472020

2021+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
2022+
impl<K, V> Default for Range<'_, K, V> {
2023+
/// Creates an empty `btree_map::Range`.
2024+
///
2025+
/// ```
2026+
/// # use std::collections::btree_map;
2027+
/// let iter: btree_map::Range<'_, u8, u8> = Default::default();
2028+
/// assert_eq!(iter.count(), 0);
2029+
/// ```
2030+
fn default() -> Self {
2031+
Range { inner: Default::default() }
2032+
}
2033+
}
2034+
19482035
#[stable(feature = "map_values_mut", since = "1.10.0")]
19492036
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
19502037
type Item = &'a mut V;
@@ -2021,6 +2108,23 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoKeys<K, V, A> {
20212108
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20222109
impl<K, V, A: Allocator + Clone> FusedIterator for IntoKeys<K, V, A> {}
20232110

2111+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
2112+
impl<K, V, A> Default for IntoKeys<K, V, A>
2113+
where
2114+
A: Allocator + Default + Clone,
2115+
{
2116+
/// Creates an empty `btree_map::IntoKeys`.
2117+
///
2118+
/// ```
2119+
/// # use std::collections::btree_map;
2120+
/// let iter: btree_map::IntoKeys<u8, u8> = Default::default();
2121+
/// assert_eq!(iter.len(), 0);
2122+
/// ```
2123+
fn default() -> Self {
2124+
IntoKeys { inner: Default::default() }
2125+
}
2126+
}
2127+
20242128
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20252129
impl<K, V, A: Allocator + Clone> Iterator for IntoValues<K, V, A> {
20262130
type Item = V;
@@ -2055,6 +2159,23 @@ impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoValues<K, V, A> {
20552159
#[stable(feature = "map_into_keys_values", since = "1.54.0")]
20562160
impl<K, V, A: Allocator + Clone> FusedIterator for IntoValues<K, V, A> {}
20572161

2162+
#[stable(feature = "default_iters", since = "CURRENT_RUSTC_VERSION")]
2163+
impl<K, V, A> Default for IntoValues<K, V, A>
2164+
where
2165+
A: Allocator + Default + Clone,
2166+
{
2167+
/// Creates an empty `btree_map::IntoValues`.
2168+
///
2169+
/// ```
2170+
/// # use std::collections::btree_map;
2171+
/// let iter: btree_map::IntoValues<u8, u8> = Default::default();
2172+
/// assert_eq!(iter.len(), 0);
2173+
/// ```
2174+
fn default() -> Self {
2175+
IntoValues { inner: Default::default() }
2176+
}
2177+
}
2178+
20582179
#[stable(feature = "btree_range", since = "1.17.0")]
20592180
impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
20602181
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {

alloc/src/collections/btree/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub mod set;
1313
mod set_val;
1414
mod split;
1515

16-
#[doc(hidden)]
1716
trait Recover<Q: ?Sized> {
1817
type Key;
1918

alloc/src/collections/btree/navigate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ impl<'a, K: 'a, V: 'a> Clone for LeafRange<marker::Immut<'a>, K, V> {
1919
}
2020
}
2121

22+
impl<B, K, V> Default for LeafRange<B, K, V> {
23+
fn default() -> Self {
24+
LeafRange { front: None, back: None }
25+
}
26+
}
27+
2228
impl<BorrowType, K, V> LeafRange<BorrowType, K, V> {
2329
pub fn none() -> Self {
2430
LeafRange { front: None, back: None }
@@ -124,6 +130,12 @@ pub struct LazyLeafRange<BorrowType, K, V> {
124130
back: Option<LazyLeafHandle<BorrowType, K, V>>,
125131
}
126132

133+
impl<B, K, V> Default for LazyLeafRange<B, K, V> {
134+
fn default() -> Self {
135+
LazyLeafRange { front: None, back: None }
136+
}
137+
}
138+
127139
impl<'a, K: 'a, V: 'a> Clone for LazyLeafRange<marker::Immut<'a>, K, V> {
128140
fn clone(&self) -> Self {
129141
LazyLeafRange { front: self.front.clone(), back: self.back.clone() }

0 commit comments

Comments
 (0)