Skip to content

Commit ac40405

Browse files
authored
Merge branch 'main' into verify/ptr_mut_byte
2 parents 41f23a1 + 2338dad commit ac40405

File tree

170 files changed

+10193
-6658
lines changed

Some content is hidden

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

170 files changed

+10193
-6658
lines changed

library/Cargo.lock

+14-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/alloc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[dependencies]
1212
core = { path = "../core" }
13-
compiler_builtins = { version = "0.1.123", features = ['rustc-dep-of-std'] }
13+
compiler_builtins = { version = "=0.1.138", features = ['rustc-dep-of-std'] }
1414
safety = { path = "../contracts/safety" }
1515

1616
[dev-dependencies]

library/alloc/src/alloc.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub use std::alloc::Global;
6161
/// of the allocator registered with the `#[global_allocator]` attribute
6262
/// if there is one, or the `std` crate’s default.
6363
///
64-
/// This function is expected to be deprecated in favor of the `alloc` method
64+
/// This function is expected to be deprecated in favor of the `allocate` method
6565
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
6666
///
6767
/// # Safety
@@ -106,7 +106,7 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
106106
/// of the allocator registered with the `#[global_allocator]` attribute
107107
/// if there is one, or the `std` crate’s default.
108108
///
109-
/// This function is expected to be deprecated in favor of the `dealloc` method
109+
/// This function is expected to be deprecated in favor of the `deallocate` method
110110
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
111111
///
112112
/// # Safety
@@ -125,7 +125,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
125125
/// of the allocator registered with the `#[global_allocator]` attribute
126126
/// if there is one, or the `std` crate’s default.
127127
///
128-
/// This function is expected to be deprecated in favor of the `realloc` method
128+
/// This function is expected to be deprecated in favor of the `grow` and `shrink` methods
129129
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
130130
///
131131
/// # Safety
@@ -145,7 +145,7 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
145145
/// of the allocator registered with the `#[global_allocator]` attribute
146146
/// if there is one, or the `std` crate’s default.
147147
///
148-
/// This function is expected to be deprecated in favor of the `alloc_zeroed` method
148+
/// This function is expected to be deprecated in favor of the `allocate_zeroed` method
149149
/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
150150
///
151151
/// # Safety
@@ -155,11 +155,14 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
155155
/// # Examples
156156
///
157157
/// ```
158-
/// use std::alloc::{alloc_zeroed, dealloc, Layout};
158+
/// use std::alloc::{alloc_zeroed, dealloc, handle_alloc_error, Layout};
159159
///
160160
/// unsafe {
161161
/// let layout = Layout::new::<u16>();
162162
/// let ptr = alloc_zeroed(layout);
163+
/// if ptr.is_null() {
164+
/// handle_alloc_error(layout);
165+
/// }
163166
///
164167
/// assert_eq!(*(ptr as *mut u16), 0);
165168
///

library/alloc/src/boxed.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ use core::error::{self, Error};
191191
use core::fmt;
192192
use core::future::Future;
193193
use core::hash::{Hash, Hasher};
194+
#[cfg(not(bootstrap))]
195+
use core::marker::PointerLike;
194196
use core::marker::{Tuple, Unsize};
195197
use core::mem::{self, SizedTypeProperties};
196198
use core::ops::{
@@ -225,6 +227,7 @@ pub use thin::ThinBox;
225227
#[fundamental]
226228
#[stable(feature = "rust1", since = "1.0.0")]
227229
#[rustc_insignificant_dtor]
230+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
228231
// The declaration of the `Box` struct must be kept in sync with the
229232
// compiler or ICEs will happen.
230233
pub struct Box<
@@ -1499,6 +1502,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
14991502
/// [`as_ptr`]: Self::as_ptr
15001503
#[unstable(feature = "box_as_ptr", issue = "129090")]
15011504
#[rustc_never_returns_null_ptr]
1505+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
15021506
#[inline]
15031507
pub fn as_mut_ptr(b: &mut Self) -> *mut T {
15041508
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
@@ -1547,6 +1551,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
15471551
/// [`as_ptr`]: Self::as_ptr
15481552
#[unstable(feature = "box_as_ptr", issue = "129090")]
15491553
#[rustc_never_returns_null_ptr]
1554+
#[cfg_attr(not(bootstrap), rustc_as_ptr)]
15501555
#[inline]
15511556
pub fn as_ptr(b: &Self) -> *const T {
15521557
// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
@@ -1734,7 +1739,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
17341739
// Pre-allocate memory to allow writing the cloned value directly.
17351740
let mut boxed = Self::new_uninit_in(self.1.clone());
17361741
unsafe {
1737-
(**self).clone_to_uninit(boxed.as_mut_ptr());
1742+
(**self).clone_to_uninit(boxed.as_mut_ptr().cast());
17381743
boxed.assume_init()
17391744
}
17401745
}
@@ -2128,3 +2133,7 @@ impl<E: Error> Error for Box<E> {
21282133
Error::provide(&**self, request);
21292134
}
21302135
}
2136+
2137+
#[cfg(not(bootstrap))]
2138+
#[unstable(feature = "pointer_like_trait", issue = "none")]
2139+
impl<T> PointerLike for Box<T> {}

library/alloc/src/boxed/convert.rs

+45
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ impl<T: Clone> From<&[T]> for Box<[T]> {
109109
}
110110
}
111111

112+
#[cfg(not(no_global_oom_handling))]
113+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
114+
impl<T: Clone> From<&mut [T]> for Box<[T]> {
115+
/// Converts a `&mut [T]` into a `Box<[T]>`
116+
///
117+
/// This conversion allocates on the heap
118+
/// and performs a copy of `slice` and its contents.
119+
///
120+
/// # Examples
121+
/// ```rust
122+
/// // create a &mut [u8] which will be used to create a Box<[u8]>
123+
/// let mut array = [104, 101, 108, 108, 111];
124+
/// let slice: &mut [u8] = &mut array;
125+
/// let boxed_slice: Box<[u8]> = Box::from(slice);
126+
///
127+
/// println!("{boxed_slice:?}");
128+
/// ```
129+
#[inline]
130+
fn from(slice: &mut [T]) -> Box<[T]> {
131+
Self::from(&*slice)
132+
}
133+
}
134+
112135
#[cfg(not(no_global_oom_handling))]
113136
#[stable(feature = "box_from_cow", since = "1.45.0")]
114137
impl<T: Clone> From<Cow<'_, [T]>> for Box<[T]> {
@@ -147,6 +170,28 @@ impl From<&str> for Box<str> {
147170
}
148171
}
149172

173+
#[cfg(not(no_global_oom_handling))]
174+
#[stable(feature = "box_from_mut_slice", since = "CURRENT_RUSTC_VERSION")]
175+
impl From<&mut str> for Box<str> {
176+
/// Converts a `&mut str` into a `Box<str>`
177+
///
178+
/// This conversion allocates on the heap
179+
/// and performs a copy of `s`.
180+
///
181+
/// # Examples
182+
///
183+
/// ```rust
184+
/// let mut original = String::from("hello");
185+
/// let original: &mut str = &mut original;
186+
/// let boxed: Box<str> = Box::from(original);
187+
/// println!("{boxed}");
188+
/// ```
189+
#[inline]
190+
fn from(s: &mut str) -> Box<str> {
191+
Self::from(&*s)
192+
}
193+
}
194+
150195
#[cfg(not(no_global_oom_handling))]
151196
#[stable(feature = "box_from_cow", since = "1.45.0")]
152197
impl From<Cow<'_, str>> for Box<str> {

library/alloc/src/collections/btree/map.rs

+48-38
Original file line numberDiff line numberDiff line change
@@ -289,40 +289,12 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> {
289289
}
290290
}
291291

292-
impl<K, Q: ?Sized, A: Allocator + Clone> super::Recover<Q> for BTreeMap<K, SetValZST, A>
293-
where
294-
K: Borrow<Q> + Ord,
295-
Q: Ord,
296-
{
297-
type Key = K;
298-
299-
fn get(&self, key: &Q) -> Option<&K> {
300-
let root_node = self.root.as_ref()?.reborrow();
301-
match root_node.search_tree(key) {
302-
Found(handle) => Some(handle.into_kv().0),
303-
GoDown(_) => None,
304-
}
305-
}
306-
307-
fn take(&mut self, key: &Q) -> Option<K> {
308-
let (map, dormant_map) = DormantMutRef::new(self);
309-
let root_node = map.root.as_mut()?.borrow_mut();
310-
match root_node.search_tree(key) {
311-
Found(handle) => Some(
312-
OccupiedEntry {
313-
handle,
314-
dormant_map,
315-
alloc: (*map.alloc).clone(),
316-
_marker: PhantomData,
317-
}
318-
.remove_kv()
319-
.0,
320-
),
321-
GoDown(_) => None,
322-
}
323-
}
324-
325-
fn replace(&mut self, key: K) -> Option<K> {
292+
/// Internal functionality for `BTreeSet`.
293+
impl<K, A: Allocator + Clone> BTreeMap<K, SetValZST, A> {
294+
pub(super) fn replace(&mut self, key: K) -> Option<K>
295+
where
296+
K: Ord,
297+
{
326298
let (map, dormant_map) = DormantMutRef::new(self);
327299
let root_node =
328300
map.root.get_or_insert_with(|| Root::new((*map.alloc).clone())).borrow_mut();
@@ -705,20 +677,58 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
705677
}
706678
}
707679

708-
/// Returns the key-value pair corresponding to the supplied key.
680+
/// Returns the key-value pair corresponding to the supplied key. This is
681+
/// potentially useful:
682+
/// - for key types where non-identical keys can be considered equal;
683+
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
684+
/// - for getting a reference to a key with the same lifetime as the collection.
709685
///
710686
/// The supplied key may be any borrowed form of the map's key type, but the ordering
711687
/// on the borrowed form *must* match the ordering on the key type.
712688
///
713689
/// # Examples
714690
///
715691
/// ```
692+
/// use std::cmp::Ordering;
716693
/// use std::collections::BTreeMap;
717694
///
695+
/// #[derive(Clone, Copy, Debug)]
696+
/// struct S {
697+
/// id: u32,
698+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
699+
/// name: &'static str, // ignored by equality and ordering operations
700+
/// }
701+
///
702+
/// impl PartialEq for S {
703+
/// fn eq(&self, other: &S) -> bool {
704+
/// self.id == other.id
705+
/// }
706+
/// }
707+
///
708+
/// impl Eq for S {}
709+
///
710+
/// impl PartialOrd for S {
711+
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
712+
/// self.id.partial_cmp(&other.id)
713+
/// }
714+
/// }
715+
///
716+
/// impl Ord for S {
717+
/// fn cmp(&self, other: &S) -> Ordering {
718+
/// self.id.cmp(&other.id)
719+
/// }
720+
/// }
721+
///
722+
/// let j_a = S { id: 1, name: "Jessica" };
723+
/// let j_b = S { id: 1, name: "Jess" };
724+
/// let p = S { id: 2, name: "Paul" };
725+
/// assert_eq!(j_a, j_b);
726+
///
718727
/// let mut map = BTreeMap::new();
719-
/// map.insert(1, "a");
720-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
721-
/// assert_eq!(map.get_key_value(&2), None);
728+
/// map.insert(j_a, "Paris");
729+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
730+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
731+
/// assert_eq!(map.get_key_value(&p), None);
722732
/// ```
723733
#[stable(feature = "map_get_key_value", since = "1.40.0")]
724734
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>

0 commit comments

Comments
 (0)