Skip to content

Commit 065af12

Browse files
authored
Merge branch 'master' into box-alloc
2 parents 6f2bee4 + d5082e6 commit 065af12

Some content is hidden

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

79 files changed

+2333
-2015
lines changed

alloc/src/alloc.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ mod tests;
1919

2020
extern "Rust" {
2121
// These are the magic symbols to call the global allocator. rustc generates
22-
// them from the `#[global_allocator]` attribute if there is one, or uses the
23-
// default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`)
22+
// them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
23+
// (the code expanding that attribute macro generates those functions), or to call
24+
// the default implementations in libstd (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
2425
// otherwise.
2526
#[rustc_allocator]
2627
#[rustc_allocator_nounwind]
@@ -31,8 +32,6 @@ extern "Rust" {
3132
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
3233
#[rustc_allocator_nounwind]
3334
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
34-
#[rustc_allocator_nounwind]
35-
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
3635
}
3736

3837
/// The global memory allocator.
@@ -42,7 +41,7 @@ extern "Rust" {
4241
/// if there is one, or the `std` crate’s default.
4342
///
4443
/// Note: while this type is unstable, the functionality it provides can be
45-
/// accessed through the [free functions in `alloc`](index.html#functions).
44+
/// accessed through the [free functions in `alloc`](self#functions).
4645
#[unstable(feature = "allocator_api", issue = "32838")]
4746
#[derive(Copy, Clone, Default, Debug)]
4847
#[cfg(not(test))]
@@ -334,6 +333,16 @@ pub(crate) unsafe fn box_free<T: ?Sized, A: AllocRef>(ptr: Unique<T>, alloc: A)
334333
}
335334
}
336335

336+
// # Allocation error handler
337+
338+
extern "Rust" {
339+
// This is the magic symbol to call the global alloc error handler. rustc generates
340+
// it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the
341+
// default implementations below (`__rdl_oom`) otherwise.
342+
#[rustc_allocator_nounwind]
343+
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
344+
}
345+
337346
/// Abort on memory allocation error or failure.
338347
///
339348
/// Callers of memory allocation APIs wishing to abort computation
@@ -378,7 +387,7 @@ pub fn handle_alloc_error(layout: Layout) -> ! {
378387
#[doc(hidden)]
379388
#[allow(unused_attributes)]
380389
#[unstable(feature = "alloc_internals", issue = "none")]
381-
pub mod __default_lib_allocator {
390+
pub mod __alloc_error_handler {
382391
use crate::alloc::Layout;
383392

384393
// called via generated `__rust_alloc_error_handler`

alloc/src/boxed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ impl From<Cow<'_, str>> for Box<str> {
10801080

10811081
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
10821082
impl<A: AllocRef> From<Box<str, A>> for Box<[u8], A> {
1083-
/// Converts a `Box<str>>` into a `Box<[u8]>`
1083+
/// Converts a `Box<str>` into a `Box<[u8]>`
10841084
///
10851085
/// This conversion does not allocate on the heap and happens in place.
10861086
///

alloc/src/collections/btree/map.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,10 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
17831783

17841784
/// Implementation of a typical `DrainFilter::size_hint` method.
17851785
pub(super) fn size_hint(&self) -> (usize, Option<usize>) {
1786+
// In most of the btree iterators, `self.length` is the number of elements
1787+
// yet to be visited. Here, it includes elements that were visited and that
1788+
// the predicate decided not to drain. Making this upper bound more accurate
1789+
// requires maintaining an extra field and is not worth while.
17861790
(0, Some(*self.length))
17871791
}
17881792
}

alloc/src/collections/btree/node.rs

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,22 @@ impl<K, V> Root<K, V> {
170170
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData }
171171
}
172172

173+
/// Borrows and returns a mutable reference to the leaf node owned by the root.
174+
/// # Safety
175+
/// The root node is a leaf.
176+
unsafe fn leaf_node_as_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Leaf> {
177+
debug_assert!(self.height == 0);
178+
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData }
179+
}
180+
181+
/// Borrows and returns a mutable reference to the internal node owned by the root.
182+
/// # Safety
183+
/// The root node is not a leaf.
184+
unsafe fn internal_node_as_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
185+
debug_assert!(self.height > 0);
186+
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData }
187+
}
188+
173189
pub fn node_as_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V, marker::LeafOrInternal> {
174190
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData }
175191
}
@@ -188,14 +204,11 @@ impl<K, V> Root<K, V> {
188204
self.node = BoxedNode::from_internal(new_node);
189205
self.height += 1;
190206

191-
let mut ret =
192-
NodeRef { height: self.height, node: self.node.as_ptr(), _marker: PhantomData };
193-
194207
unsafe {
208+
let mut ret = self.internal_node_as_mut();
195209
ret.reborrow_mut().first_edge().correct_parent_link();
210+
ret
196211
}
197-
198-
ret
199212
}
200213

201214
/// Removes the internal root node, using its first child as the new root node.
@@ -212,11 +225,8 @@ impl<K, V> Root<K, V> {
212225

213226
let top = self.node.ptr;
214227

215-
self.node = unsafe {
216-
BoxedNode::from_ptr(
217-
self.node_as_mut().cast_unchecked::<marker::Internal>().first_edge().descend().node,
218-
)
219-
};
228+
let internal_node = unsafe { self.internal_node_as_mut() };
229+
self.node = unsafe { BoxedNode::from_ptr(internal_node.first_edge().descend().node) };
220230
self.height -= 1;
221231
self.node_as_mut().as_leaf_mut().parent = None;
222232

@@ -247,8 +257,13 @@ impl<K, V> Root<K, V> {
247257
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
248258
/// `NodeRef` could be pointing to either type of node.
249259
pub struct NodeRef<BorrowType, K, V, Type> {
250-
/// The number of levels below the node.
260+
/// The number of levels below the node, a property of the node that cannot be
261+
/// entirely described by `Type` and that the node does not store itself either.
262+
/// Unconstrained if `Type` is `LeafOrInternal`, must be zero if `Type` is `Leaf`,
263+
/// and must be non-zero if `Type` is `Internal`.
251264
height: usize,
265+
/// The pointer to the leaf or internal node. The definition of `InternalNode`
266+
/// ensures that the pointer is valid either way.
252267
node: NonNull<LeafNode<K, V>>,
253268
_marker: PhantomData<(BorrowType, Type)>,
254269
}
@@ -305,8 +320,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
305320
unsafe { usize::from((*self.as_leaf_ptr()).len) }
306321
}
307322

308-
/// Returns the height of this node in the whole tree. Zero height denotes the
309-
/// leaf level.
323+
/// Returns the height of this node with respect to the leaf level. Zero height means the
324+
/// node is a leaf itself.
310325
pub fn height(&self) -> usize {
311326
self.height
312327
}
@@ -443,9 +458,9 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
443458
}
444459

445460
impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
446-
/// Unsafely asserts to the compiler some static information about whether this
447-
/// node is a `Leaf` or an `Internal`.
448-
unsafe fn cast_unchecked<NewType>(self) -> NodeRef<marker::Mut<'a>, K, V, NewType> {
461+
/// Unsafely asserts to the compiler the static information that this node is an `Internal`.
462+
unsafe fn cast_to_internal_unchecked(self) -> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
463+
debug_assert!(self.height > 0);
449464
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
450465
}
451466

@@ -574,9 +589,11 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
574589
// to avoid aliasing with outstanding references to other elements,
575590
// in particular, those returned to the caller in earlier iterations.
576591
let leaf = self.node.as_ptr();
592+
let keys = unsafe { &raw const (*leaf).keys };
593+
let vals = unsafe { &raw mut (*leaf).vals };
577594
// We must coerce to unsized array pointers because of Rust issue #74679.
578-
let keys: *const [_] = unsafe { &raw const (*leaf).keys };
579-
let vals: *mut [_] = unsafe { &raw mut (*leaf).vals };
595+
let keys: *const [_] = keys;
596+
let vals: *mut [_] = vals;
580597
// SAFETY: The keys and values of a node must always be initialized up to length.
581598
let key = unsafe { (&*keys.get_unchecked(idx)).assume_init_ref() };
582599
let val = unsafe { (&mut *vals.get_unchecked_mut(idx)).assume_init_mut() };
@@ -807,19 +824,34 @@ impl<BorrowType, K, V, NodeType> Handle<NodeRef<BorrowType, K, V, NodeType>, mar
807824
}
808825
}
809826

827+
impl<BorrowType, K, V, NodeType> NodeRef<BorrowType, K, V, NodeType> {
828+
/// Could be a public implementation of PartialEq, but only used in this module.
829+
fn eq(&self, other: &Self) -> bool {
830+
let Self { node, height, _marker: _ } = self;
831+
if *node == other.node {
832+
debug_assert_eq!(*height, other.height);
833+
true
834+
} else {
835+
false
836+
}
837+
}
838+
}
839+
810840
impl<BorrowType, K, V, NodeType, HandleType> PartialEq
811841
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
812842
{
813843
fn eq(&self, other: &Self) -> bool {
814-
self.node.node == other.node.node && self.idx == other.idx
844+
let Self { node, idx, _marker: _ } = self;
845+
node.eq(&other.node) && *idx == other.idx
815846
}
816847
}
817848

818849
impl<BorrowType, K, V, NodeType, HandleType> PartialOrd
819850
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
820851
{
821852
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
822-
if self.node.node == other.node.node { Some(self.idx.cmp(&other.idx)) } else { None }
853+
let Self { node, idx, _marker: _ } = self;
854+
if node.eq(&other.node) { Some(idx.cmp(&other.idx)) } else { None }
823855
}
824856
}
825857

@@ -943,10 +975,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
943975
Handle::new_edge(left.reborrow_mut(), insert_idx)
944976
},
945977
InsertionPlace::Right(insert_idx) => unsafe {
946-
Handle::new_edge(
947-
right.node_as_mut().cast_unchecked::<marker::Leaf>(),
948-
insert_idx,
949-
)
978+
Handle::new_edge(right.leaf_node_as_mut(), insert_idx)
950979
},
951980
};
952981
let val_ptr = insertion_edge.insert_fit(key, val);
@@ -1006,10 +1035,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
10061035
Handle::new_edge(left.reborrow_mut(), insert_idx)
10071036
},
10081037
InsertionPlace::Right(insert_idx) => unsafe {
1009-
Handle::new_edge(
1010-
right.node_as_mut().cast_unchecked::<marker::Internal>(),
1011-
insert_idx,
1012-
)
1038+
Handle::new_edge(right.internal_node_as_mut(), insert_idx)
10131039
},
10141040
};
10151041
insertion_edge.insert_fit(key, val, edge);
@@ -1205,7 +1231,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12051231

12061232
let mut new_root = Root { node: BoxedNode::from_internal(new_node), height };
12071233

1208-
new_root.node_as_mut().cast_unchecked().correct_childrens_parent_links(0..=new_len);
1234+
new_root.internal_node_as_mut().correct_childrens_parent_links(0..=new_len);
12091235

12101236
(self.node, k, v, new_root)
12111237
}
@@ -1258,8 +1284,8 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12581284
if self.node.height > 1 {
12591285
// SAFETY: the height of the nodes being merged is one below the height
12601286
// of the node of this edge, thus above zero, so they are internal.
1261-
let mut left_node = left_node.cast_unchecked::<marker::Internal>();
1262-
let right_node = right_node.cast_unchecked::<marker::Internal>();
1287+
let mut left_node = left_node.cast_to_internal_unchecked();
1288+
let right_node = right_node.cast_to_internal_unchecked();
12631289
ptr::copy_nonoverlapping(
12641290
right_node.edge_at(0),
12651291
left_node.edges_mut().as_mut_ptr().add(left_len + 1),

alloc/src/collections/btree/node/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use core::cmp::Ordering::*;
23

34
#[test]
45
fn test_splitpoint() {
@@ -24,6 +25,38 @@ fn test_splitpoint() {
2425
}
2526
}
2627

28+
#[test]
29+
fn test_partial_cmp_eq() {
30+
let mut root1: Root<i32, ()> = Root::new_leaf();
31+
let mut leaf1 = unsafe { root1.leaf_node_as_mut() };
32+
leaf1.push(1, ());
33+
root1.push_internal_level();
34+
let root2: Root<i32, ()> = Root::new_leaf();
35+
36+
let leaf_edge_1a = root1.node_as_ref().first_leaf_edge().forget_node_type();
37+
let leaf_edge_1b = root1.node_as_ref().last_leaf_edge().forget_node_type();
38+
let top_edge_1 = root1.node_as_ref().first_edge();
39+
let top_edge_2 = root2.node_as_ref().first_edge();
40+
41+
assert!(leaf_edge_1a == leaf_edge_1a);
42+
assert!(leaf_edge_1a != leaf_edge_1b);
43+
assert!(leaf_edge_1a != top_edge_1);
44+
assert!(leaf_edge_1a != top_edge_2);
45+
assert!(top_edge_1 == top_edge_1);
46+
assert!(top_edge_1 != top_edge_2);
47+
48+
assert_eq!(leaf_edge_1a.partial_cmp(&leaf_edge_1a), Some(Equal));
49+
assert_eq!(leaf_edge_1a.partial_cmp(&leaf_edge_1b), Some(Less));
50+
assert_eq!(leaf_edge_1a.partial_cmp(&top_edge_1), None);
51+
assert_eq!(leaf_edge_1a.partial_cmp(&top_edge_2), None);
52+
assert_eq!(top_edge_1.partial_cmp(&top_edge_1), Some(Equal));
53+
assert_eq!(top_edge_1.partial_cmp(&top_edge_2), None);
54+
55+
root1.pop_internal_level();
56+
unsafe { root1.into_ref().deallocate_and_ascend() };
57+
unsafe { root2.into_ref().deallocate_and_ascend() };
58+
}
59+
2760
#[test]
2861
#[cfg(target_arch = "x86_64")]
2962
fn test_sizes() {

alloc/src/vec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,8 @@ impl<T> Vec<T> {
14761476
/// `'a`. If the type has only static references, or none at all, then this
14771477
/// may be chosen to be `'static`.
14781478
///
1479-
/// This function is similar to the `leak` function on `Box`.
1479+
/// This function is similar to the [`leak`][Box::leak] function on [`Box`]
1480+
/// except that there is no way to recover the leaked memory.
14801481
///
14811482
/// This function is mainly useful for data that lives for the remainder of
14821483
/// the program's life. Dropping the returned reference will cause a memory
@@ -2591,6 +2592,8 @@ __impl_slice_eq1! { [] Vec<A>, &[B], #[stable(feature = "rust1", since = "1.0.0"
25912592
__impl_slice_eq1! { [] Vec<A>, &mut [B], #[stable(feature = "rust1", since = "1.0.0")] }
25922593
__impl_slice_eq1! { [] &[A], Vec<B>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] }
25932594
__impl_slice_eq1! { [] &mut [A], Vec<B>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] }
2595+
__impl_slice_eq1! { [] Vec<A>, [B], #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")] }
2596+
__impl_slice_eq1! { [] [A], Vec<B>, #[stable(feature = "partialeq_vec_for_slice", since = "1.48.0")] }
25942597
__impl_slice_eq1! { [] Cow<'_, [A]>, Vec<B> where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }
25952598
__impl_slice_eq1! { [] Cow<'_, [A]>, &[B] where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }
25962599
__impl_slice_eq1! { [] Cow<'_, [A]>, &mut [B] where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }

alloc/tests/vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ fn partialeq_vec_and_prim() {
17991799
}
18001800

18011801
macro_rules! assert_partial_eq_valid {
1802-
($a2:ident, $a3:ident; $b2:ident, $b3: ident) => {
1802+
($a2:expr, $a3:expr; $b2:expr, $b3: expr) => {
18031803
assert!($a2 == $b2);
18041804
assert!($a2 != $b3);
18051805
assert!($a3 != $b2);
@@ -1831,6 +1831,7 @@ fn partialeq_vec_full() {
18311831
assert_partial_eq_valid!(slicemut2,slicemut3; vec2,vec3);
18321832
assert_partial_eq_valid!(vec2,vec3; array2,array3);
18331833
assert_partial_eq_valid!(vec2,vec3; arrayref2,arrayref3);
1834+
assert_partial_eq_valid!(vec2,vec3; arrayref2[..],arrayref3[..]);
18341835
}
18351836

18361837
#[test]

backtrace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 4083a90168d605b682ba166a0c01f86b3384e474
1+
Subproject commit 893fbb23688e98376e54c26b59432a2966a8cc96

core/src/cell.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ use crate::ptr;
226226
/// assert_eq!(my_struct.special_field.get(), new_value);
227227
/// ```
228228
///
229-
/// See the [module-level documentation](index.html) for more.
229+
/// See the [module-level documentation](self) for more.
230230
#[stable(feature = "rust1", since = "1.0.0")]
231231
#[repr(transparent)]
232232
pub struct Cell<T: ?Sized> {
@@ -566,7 +566,7 @@ impl<T> Cell<[T]> {
566566

567567
/// A mutable memory location with dynamically checked borrow rules
568568
///
569-
/// See the [module-level documentation](index.html) for more.
569+
/// See the [module-level documentation](self) for more.
570570
#[stable(feature = "rust1", since = "1.0.0")]
571571
pub struct RefCell<T: ?Sized> {
572572
borrow: Cell<BorrowFlag>,
@@ -1203,7 +1203,7 @@ impl Clone for BorrowRef<'_> {
12031203
/// Wraps a borrowed reference to a value in a `RefCell` box.
12041204
/// A wrapper type for an immutably borrowed value from a `RefCell<T>`.
12051205
///
1206-
/// See the [module-level documentation](index.html) for more.
1206+
/// See the [module-level documentation](self) for more.
12071207
#[stable(feature = "rust1", since = "1.0.0")]
12081208
pub struct Ref<'b, T: ?Sized + 'b> {
12091209
value: &'b T,
@@ -1493,7 +1493,7 @@ impl<'b> BorrowRefMut<'b> {
14931493

14941494
/// A wrapper type for a mutably borrowed value from a `RefCell<T>`.
14951495
///
1496-
/// See the [module-level documentation](index.html) for more.
1496+
/// See the [module-level documentation](self) for more.
14971497
#[stable(feature = "rust1", since = "1.0.0")]
14981498
pub struct RefMut<'b, T: ?Sized + 'b> {
14991499
value: &'b mut T,

core/src/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'a, 'f: 'a> DerefMut for VaList<'a, 'f> {
280280
// within a private module. Once RFC 2145 has been implemented look into
281281
// improving this.
282282
mod sealed_trait {
283-
/// Trait which permits the allowed types to be used with [VaList::arg].
283+
/// Trait which permits the allowed types to be used with [super::VaListImpl::arg].
284284
#[unstable(
285285
feature = "c_variadic",
286286
reason = "the `c_variadic` feature has not been properly tested on \

0 commit comments

Comments
 (0)