Skip to content

Commit 377b0d5

Browse files
committed
Merge from rustc
2 parents af9fc8f + 4fb5a27 commit 377b0d5

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
}

alloc/src/vec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ impl<T, A: Allocator> Vec<T, A> {
25262526
/// assert_eq!(vec, [1, 2, 3]);
25272527
/// assert_eq!(vec.pop_if(pred), None);
25282528
/// ```
2529-
#[stable(feature = "vec_pop_if", since = "CURRENT_RUSTC_VERSION")]
2529+
#[stable(feature = "vec_pop_if", since = "1.86.0")]
25302530
pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T> {
25312531
let last = self.last_mut()?;
25322532
if predicate(last) { self.pop() } else { None }

alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(iter_array_chunks)]
44
#![feature(assert_matches)]
55
#![feature(btree_extract_if)]
6+
#![feature(char_max_len)]
67
#![feature(cow_is_borrowed)]
78
#![feature(core_intrinsics)]
89
#![feature(downcast_unchecked)]

alloc/tests/str.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::assert_matches::assert_matches;
44
use std::borrow::Cow;
5+
use std::char::MAX_LEN_UTF8;
56
use std::cmp::Ordering::{Equal, Greater, Less};
67
use std::str::{from_utf8, from_utf8_unchecked};
78

@@ -1231,7 +1232,7 @@ fn test_to_uppercase_rev_iterator() {
12311232
#[test]
12321233
#[cfg_attr(miri, ignore)] // Miri is too slow
12331234
fn test_chars_decoding() {
1234-
let mut bytes = [0; 4];
1235+
let mut bytes = [0; MAX_LEN_UTF8];
12351236
for c in (0..0x110000).filter_map(std::char::from_u32) {
12361237
let s = c.encode_utf8(&mut bytes);
12371238
if Some(c) != s.chars().next() {
@@ -1243,7 +1244,7 @@ fn test_chars_decoding() {
12431244
#[test]
12441245
#[cfg_attr(miri, ignore)] // Miri is too slow
12451246
fn test_chars_rev_decoding() {
1246-
let mut bytes = [0; 4];
1247+
let mut bytes = [0; MAX_LEN_UTF8];
12471248
for c in (0..0x110000).filter_map(std::char::from_u32) {
12481249
let s = c.encode_utf8(&mut bytes);
12491250
if Some(c) != s.chars().rev().next() {

core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ optimize_for_size = []
2323
# Make `RefCell` store additional debugging information, which is printed out when
2424
# a borrow error occurs
2525
debug_refcell = []
26+
# Make `TypeId` store a reference to the name of the type, so that it can print that name.
27+
debug_typeid = []
2628

2729
[lints.rust.unexpected_cfgs]
2830
level = "warn"
2931
check-cfg = [
3032
'cfg(bootstrap)',
3133
'cfg(no_fp_fmt_parse)',
3234
'cfg(stdarch_intel_sde)',
33-
'cfg(target_arch, values("xtensa"))',
3435
# core use #[path] imports to portable-simd `core_simd` crate
3536
# and to stdarch `core_arch` crate which messes-up with Cargo list
3637
# of declared features, we therefor expect any feature cfg

core/src/any.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ pub struct TypeId {
711711
// We avoid using `u128` because that imposes higher alignment requirements on many platforms.
712712
// See issue #115620 for more information.
713713
t: (u64, u64),
714+
#[cfg(feature = "debug_typeid")]
715+
name: &'static str,
714716
}
715717

716718
#[stable(feature = "rust1", since = "1.0.0")]
@@ -741,10 +743,14 @@ impl TypeId {
741743
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
742744
pub const fn of<T: ?Sized + 'static>() -> TypeId {
743745
let t: u128 = intrinsics::type_id::<T>();
744-
745746
let t1 = (t >> 64) as u64;
746747
let t2 = t as u64;
747-
TypeId { t: (t1, t2) }
748+
749+
TypeId {
750+
t: (t1, t2),
751+
#[cfg(feature = "debug_typeid")]
752+
name: type_name::<T>(),
753+
}
748754
}
749755

750756
fn as_u128(self) -> u128 {
@@ -775,7 +781,15 @@ impl hash::Hash for TypeId {
775781
#[stable(feature = "rust1", since = "1.0.0")]
776782
impl fmt::Debug for TypeId {
777783
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
778-
write!(f, "TypeId({:#034x})", self.as_u128())
784+
#[cfg(feature = "debug_typeid")]
785+
{
786+
write!(f, "TypeId({:#034x} = {})", self.as_u128(), self.name)?;
787+
}
788+
#[cfg(not(feature = "debug_typeid"))]
789+
{
790+
write!(f, "TypeId({:#034x})", self.as_u128())?;
791+
}
792+
Ok(())
779793
}
780794
}
781795

core/src/char/methods.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ impl char {
7171
#[stable(feature = "assoc_char_consts", since = "1.52.0")]
7272
pub const MAX: char = '\u{10FFFF}';
7373

74+
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
75+
/// UTF-8 encoding.
76+
#[unstable(feature = "char_max_len", issue = "121714")]
77+
pub const MAX_LEN_UTF8: usize = 4;
78+
79+
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
80+
/// to UTF-16 encoding.
81+
#[unstable(feature = "char_max_len", issue = "121714")]
82+
pub const MAX_LEN_UTF16: usize = 2;
83+
7484
/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a
7585
/// decoding error.
7686
///

core/src/char/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ const MAX_THREE_B: u32 = 0x10000;
9595
#[stable(feature = "rust1", since = "1.0.0")]
9696
pub const MAX: char = char::MAX;
9797

98+
/// The maximum number of bytes required to [encode](char::encode_utf8) a `char` to
99+
/// UTF-8 encoding.
100+
#[unstable(feature = "char_max_len", issue = "121714")]
101+
pub const MAX_LEN_UTF8: usize = char::MAX_LEN_UTF8;
102+
103+
/// The maximum number of two-byte units required to [encode](char::encode_utf16) a `char`
104+
/// to UTF-16 encoding.
105+
#[unstable(feature = "char_max_len", issue = "121714")]
106+
pub const MAX_LEN_UTF16: usize = char::MAX_LEN_UTF16;
107+
98108
/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a
99109
/// decoding error. Use [`char::REPLACEMENT_CHARACTER`] instead.
100110
#[stable(feature = "decode_utf16", since = "1.9.0")]

core/src/contracts.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Unstable module containing the unstable contracts lang items and attribute macros.
2-
#![cfg(not(bootstrap))]
32
43
pub use crate::macros::builtin::{contracts_ensures as ensures, contracts_requires as requires};
54

core/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,5 +1099,5 @@ impl Error for crate::time::TryFromFloatSecsError {}
10991099
#[stable(feature = "cstr_from_bytes_until_nul", since = "1.69.0")]
11001100
impl Error for crate::ffi::FromBytesUntilNulError {}
11011101

1102-
#[stable(feature = "get_many_mut", since = "CURRENT_RUSTC_VERSION")]
1102+
#[stable(feature = "get_many_mut", since = "1.86.0")]
11031103
impl Error for crate::slice::GetDisjointMutError {}

core/src/fmt/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![stable(feature = "rust1", since = "1.0.0")]
44

55
use crate::cell::{Cell, Ref, RefCell, RefMut, SyncUnsafeCell, UnsafeCell};
6-
use crate::char::EscapeDebugExtArgs;
6+
use crate::char::{EscapeDebugExtArgs, MAX_LEN_UTF8};
77
use crate::marker::PhantomData;
88
use crate::num::fmt as numfmt;
99
use crate::ops::Deref;
@@ -187,7 +187,7 @@ pub trait Write {
187187
/// ```
188188
#[stable(feature = "fmt_write_char", since = "1.1.0")]
189189
fn write_char(&mut self, c: char) -> Result {
190-
self.write_str(c.encode_utf8(&mut [0; 4]))
190+
self.write_str(c.encode_utf8(&mut [0; MAX_LEN_UTF8]))
191191
}
192192

193193
/// Glue for usage of the [`write!`] macro with implementors of this trait.
@@ -2768,7 +2768,7 @@ impl Display for char {
27682768
if f.options.width.is_none() && f.options.precision.is_none() {
27692769
f.write_char(*self)
27702770
} else {
2771-
f.pad(self.encode_utf8(&mut [0; 4]))
2771+
f.pad(self.encode_utf8(&mut [0; MAX_LEN_UTF8]))
27722772
}
27732773
}
27742774
}

core/src/hint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ pub fn spin_loop() {
472472
/// During constant evaluation, `black_box` is treated as a no-op.
473473
#[inline]
474474
#[stable(feature = "bench_black_box", since = "1.66.0")]
475-
#[rustc_const_stable(feature = "const_black_box", since = "CURRENT_RUSTC_VERSION")]
475+
#[rustc_const_stable(feature = "const_black_box", since = "1.86.0")]
476476
pub const fn black_box<T>(dummy: T) -> T {
477477
crate::intrinsics::black_box(dummy)
478478
}

0 commit comments

Comments
 (0)