Skip to content

Commit 5bd0747

Browse files
RalfJunggitbot
authored andcommitted
Merge from rustc
2 parents f14cd24 + c7caf81 commit 5bd0747

Some content is hidden

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

99 files changed

+1013
-813
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

alloc/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cargo-features = ["public-dependency"]
2+
13
[package]
24
name = "alloc"
35
version = "0.0.0"
@@ -9,8 +11,8 @@ autobenches = false
911
edition = "2021"
1012

1113
[dependencies]
12-
core = { path = "../core" }
13-
compiler_builtins = { version = "=0.1.146", features = ['rustc-dep-of-std'] }
14+
core = { path = "../core", public = true }
15+
compiler_builtins = { version = "=0.1.147", features = ['rustc-dep-of-std'] }
1416

1517
[dev-dependencies]
1618
rand = { version = "0.9.0", default-features = false, features = ["alloc"] }

alloc/src/boxed.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,23 +237,13 @@ pub struct Box<
237237
/// the newly allocated memory. This is an intrinsic to avoid unnecessary copies.
238238
///
239239
/// This is the surface syntax for `box <expr>` expressions.
240-
#[cfg(not(bootstrap))]
241240
#[rustc_intrinsic]
242241
#[rustc_intrinsic_must_be_overridden]
243242
#[unstable(feature = "liballoc_internals", issue = "none")]
244243
pub fn box_new<T>(_x: T) -> Box<T> {
245244
unreachable!()
246245
}
247246

248-
/// Transition function for the next bootstrap bump.
249-
#[cfg(bootstrap)]
250-
#[unstable(feature = "liballoc_internals", issue = "none")]
251-
#[inline(always)]
252-
pub fn box_new<T>(x: T) -> Box<T> {
253-
#[rustc_box]
254-
Box::new(x)
255-
}
256-
257247
impl<T> Box<T> {
258248
/// Allocates memory on the heap and then places `x` into it.
259249
///
@@ -1689,7 +1679,20 @@ impl<T: Default> Default for Box<T> {
16891679
/// Creates a `Box<T>`, with the `Default` value for T.
16901680
#[inline]
16911681
fn default() -> Self {
1692-
Box::write(Box::new_uninit(), T::default())
1682+
let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit();
1683+
unsafe {
1684+
// SAFETY: `x` is valid for writing and has the same layout as `T`.
1685+
// If `T::default()` panics, dropping `x` will just deallocate the Box as `MaybeUninit<T>`
1686+
// does not have a destructor.
1687+
//
1688+
// We use `ptr::write` as `MaybeUninit::write` creates
1689+
// extra stack copies of `T` in debug mode.
1690+
//
1691+
// See https://github.com/rust-lang/rust/issues/136043 for more context.
1692+
ptr::write(&raw mut *x as *mut T, T::default());
1693+
// SAFETY: `x` was just initialized above.
1694+
x.assume_init()
1695+
}
16931696
}
16941697
}
16951698

alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#![feature(box_uninit_write)]
106106
#![feature(bstr)]
107107
#![feature(bstr_internals)]
108+
#![feature(char_max_len)]
108109
#![feature(clone_to_uninit)]
109110
#![feature(coerce_unsized)]
110111
#![feature(const_eval_select)]
@@ -136,7 +137,6 @@
136137
#![feature(pointer_like_trait)]
137138
#![feature(ptr_internals)]
138139
#![feature(ptr_metadata)]
139-
#![feature(ptr_sub_ptr)]
140140
#![feature(set_ptr_value)]
141141
#![feature(sized_type_properties)]
142142
#![feature(slice_from_ptr_range)]

alloc/src/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use core::slice::ArrayChunksMut;
2727
pub use core::slice::ArrayWindows;
2828
#[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
2929
pub use core::slice::EscapeAscii;
30-
#[stable(feature = "get_many_mut", since = "CURRENT_RUSTC_VERSION")]
30+
#[stable(feature = "get_many_mut", since = "1.86.0")]
3131
pub use core::slice::GetDisjointMutError;
3232
#[stable(feature = "slice_get_slice", since = "1.28.0")]
3333
pub use core::slice::SliceIndex;

alloc/src/string.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,9 @@ impl String {
14191419
pub fn push(&mut self, ch: char) {
14201420
match ch.len_utf8() {
14211421
1 => self.vec.push(ch as u8),
1422-
_ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()),
1422+
_ => {
1423+
self.vec.extend_from_slice(ch.encode_utf8(&mut [0; char::MAX_LEN_UTF8]).as_bytes())
1424+
}
14231425
}
14241426
}
14251427

@@ -1716,7 +1718,7 @@ impl String {
17161718
#[rustc_confusables("set")]
17171719
pub fn insert(&mut self, idx: usize, ch: char) {
17181720
assert!(self.is_char_boundary(idx));
1719-
let mut bits = [0; 4];
1721+
let mut bits = [0; char::MAX_LEN_UTF8];
17201722
let bits = ch.encode_utf8(&mut bits).as_bytes();
17211723

17221724
unsafe {
@@ -2797,7 +2799,7 @@ impl SpecToString for core::ascii::Char {
27972799
impl SpecToString for char {
27982800
#[inline]
27992801
fn spec_to_string(&self) -> String {
2800-
String::from(self.encode_utf8(&mut [0; 4]))
2802+
String::from(self.encode_utf8(&mut [0; char::MAX_LEN_UTF8]))
28012803
}
28022804
}
28032805

@@ -3155,6 +3157,24 @@ impl From<String> for Vec<u8> {
31553157
}
31563158
}
31573159

3160+
#[stable(feature = "try_from_vec_u8_for_string", since = "CURRENT_RUSTC_VERSION")]
3161+
impl TryFrom<Vec<u8>> for String {
3162+
type Error = FromUtf8Error;
3163+
/// Converts the given [`Vec<u8>`] into a [`String`] if it contains valid UTF-8 data.
3164+
///
3165+
/// # Examples
3166+
///
3167+
/// ```
3168+
/// let s1 = b"hello world".to_vec();
3169+
/// let v1 = String::try_from(s1).unwrap();
3170+
/// assert_eq!(v1, "hello world");
3171+
///
3172+
/// ```
3173+
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
3174+
Self::from_utf8(bytes)
3175+
}
3176+
}
3177+
31583178
#[cfg(not(no_global_oom_handling))]
31593179
#[stable(feature = "rust1", since = "1.0.0")]
31603180
impl fmt::Write for String {

alloc/src/vec/drain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
232232
// it from the original vec but also avoid creating a &mut to the front since that could
233233
// invalidate raw pointers to it which some unsafe code might rely on.
234234
let vec_ptr = vec.as_mut().as_mut_ptr();
235-
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
235+
let drop_offset = drop_ptr.offset_from_unsigned(vec_ptr);
236236
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
237237
ptr::drop_in_place(to_drop);
238238
}

alloc/src/vec/in_place_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ where
379379
let sink =
380380
self.try_fold::<_, _, Result<_, !>>(sink, write_in_place_with_drop(end)).into_ok();
381381
// iteration succeeded, don't drop head
382-
unsafe { ManuallyDrop::new(sink).dst.sub_ptr(dst_buf) }
382+
unsafe { ManuallyDrop::new(sink).dst.offset_from_unsigned(dst_buf) }
383383
}
384384
}
385385

alloc/src/vec/in_place_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) struct InPlaceDrop<T> {
1414

1515
impl<T> InPlaceDrop<T> {
1616
fn len(&self) -> usize {
17-
unsafe { self.dst.sub_ptr(self.inner) }
17+
unsafe { self.dst.offset_from_unsigned(self.inner) }
1818
}
1919
}
2020

alloc/src/vec/into_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
179179
// say that they're all at the beginning of the "allocation".
180180
0..this.len()
181181
} else {
182-
this.ptr.sub_ptr(this.buf)..this.end.sub_ptr(buf)
182+
this.ptr.offset_from_unsigned(this.buf)..this.end.offset_from_unsigned(buf)
183183
};
184184
let cap = this.cap;
185185
let alloc = ManuallyDrop::take(&mut this.alloc);
@@ -230,7 +230,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
230230
let exact = if T::IS_ZST {
231231
self.end.addr().wrapping_sub(self.ptr.as_ptr().addr())
232232
} else {
233-
unsafe { non_null!(self.end, T).sub_ptr(self.ptr) }
233+
unsafe { non_null!(self.end, T).offset_from_unsigned(self.ptr) }
234234
};
235235
(exact, Some(exact))
236236
}

0 commit comments

Comments
 (0)