Skip to content

Commit

Permalink
Merge from rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 8, 2024
2 parents c6e53ce + e6c378f commit 05812a0
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 27 deletions.
3 changes: 1 addition & 2 deletions alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,6 @@ impl<T, A: Allocator> BinaryHeap<T, A> {
/// Basic usage:
///
/// ```
/// #![feature(binary_heap_as_slice)]
/// use std::collections::BinaryHeap;
/// use std::io::{self, Write};
///
Expand All @@ -1225,7 +1224,7 @@ impl<T, A: Allocator> BinaryHeap<T, A> {
/// io::sink().write(heap.as_slice()).unwrap();
/// ```
#[must_use]
#[unstable(feature = "binary_heap_as_slice", issue = "83659")]
#[stable(feature = "binary_heap_as_slice", since = "CURRENT_RUSTC_VERSION")]
pub fn as_slice(&self) -> &[T] {
self.data.as_slice()
}
Expand Down
1 change: 0 additions & 1 deletion alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
#![feature(deref_pure_trait)]
#![feature(dispatch_from_dyn)]
#![feature(error_generic_member_access)]
#![feature(error_in_core)]
#![feature(exact_size_is_empty)]
#![feature(extend_one)]
#![feature(fmt_internals)]
Expand Down
1 change: 0 additions & 1 deletion alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_drain_sorted)]
#![feature(slice_ptr_get)]
#![feature(binary_heap_as_slice)]
#![feature(inplace_iteration)]
#![feature(iter_advance_by)]
#![feature(iter_next_chunk)]
Expand Down
2 changes: 1 addition & 1 deletion backtrace
Submodule backtrace updated from 5e05ef to 72265b
16 changes: 3 additions & 13 deletions core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![doc = include_str!("error.md")]
#![unstable(feature = "error_in_core", issue = "103765")]
#![stable(feature = "error_in_core", since = "CURRENT_RUSTC_VERSION")]

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -130,7 +130,6 @@ pub trait Error: Debug + Display {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
/// use core::fmt;
/// use core::error::{request_ref, Request};
///
Expand Down Expand Up @@ -361,8 +360,7 @@ impl dyn Error {
/// Get a string value from an error.
///
/// ```rust
/// # #![feature(error_generic_member_access)]
/// # #![feature(error_in_core)]
/// #![feature(error_generic_member_access)]
/// use std::error::Error;
/// use core::error::request_value;
///
Expand All @@ -385,8 +383,7 @@ where
/// Get a string reference from an error.
///
/// ```rust
/// # #![feature(error_generic_member_access)]
/// # #![feature(error_in_core)]
/// #![feature(error_generic_member_access)]
/// use core::error::Error;
/// use core::error::request_ref;
///
Expand Down Expand Up @@ -458,7 +455,6 @@ where
///
/// ```
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
/// use core::fmt;
/// use core::error::Request;
/// use core::error::request_ref;
Expand Down Expand Up @@ -529,7 +525,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
///
Expand Down Expand Up @@ -564,7 +559,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
///
Expand Down Expand Up @@ -600,7 +594,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
///
Expand Down Expand Up @@ -633,7 +626,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
///
Expand Down Expand Up @@ -700,7 +692,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
/// use core::error::request_value;
Expand Down Expand Up @@ -788,7 +779,6 @@ impl<'a> Request<'a> {
///
/// ```rust
/// #![feature(error_generic_member_access)]
/// #![feature(error_in_core)]
///
/// use core::error::Request;
/// use core::error::request_ref;
Expand Down
33 changes: 33 additions & 0 deletions core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ static DEC_DIGITS_LUT: &[u8; 200] = b"0001020304050607080910111213141516171819\

macro_rules! impl_Display {
($($t:ident),* as $u:ident via $conv_fn:ident named $name:ident) => {
#[cfg(not(feature = "optimize_for_size"))]
fn $name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
Expand Down Expand Up @@ -277,6 +278,38 @@ macro_rules! impl_Display {
f.pad_integral(is_nonnegative, "", buf_slice)
}

#[cfg(feature = "optimize_for_size")]
fn $name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
let mut curr = buf.len();
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);

// SAFETY: To show that it's OK to copy into `buf_ptr`, notice that at the beginning
// `curr == buf.len() == 39 > log(n)` since `n < 2^128 < 10^39`, and at
// each step this is kept the same as `n` is divided. Since `n` is always
// non-negative, this means that `curr > 0` so `buf_ptr[curr..curr + 1]`
// is safe to access.
unsafe {
loop {
curr -= 1;
buf_ptr.add(curr).write((n % 10) as u8 + b'0');
n /= 10;

if n == 0 {
break;
}
}
}

// SAFETY: `curr` > 0 (since we made `buf` large enough), and all the chars are valid UTF-8
let buf_slice = unsafe {
str::from_utf8_unchecked(
slice::from_raw_parts(buf_ptr.add(curr), buf.len() - curr))
};
f.pad_integral(is_nonnegative, "", buf_slice)
}

$(#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for $t {
#[allow(unused_comparisons)]
Expand Down
4 changes: 1 addition & 3 deletions core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,8 +1708,6 @@ impl<T> Option<T> {
/// # Examples
///
/// ```
/// #![feature(option_take_if)]
///
/// let mut x = Some(42);
///
/// let prev = x.take_if(|v| if *v == 42 {
Expand All @@ -1726,7 +1724,7 @@ impl<T> Option<T> {
/// assert_eq!(prev, Some(43));
/// ```
#[inline]
#[unstable(feature = "option_take_if", issue = "98934")]
#[stable(feature = "option_take_if", since = "CURRENT_RUSTC_VERSION")]
pub fn take_if<P>(&mut self, predicate: P) -> Option<T>
where
P: FnOnce(&mut T) -> bool,
Expand Down
2 changes: 1 addition & 1 deletion core/src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
//! requires at least a level of pointer indirection each time a new object is added to the mix
//! (and, practically, a heap allocation).
//!
//! Although there were other reason as well, this issue of expensive composition is the key thing
//! Although there were other reasons as well, this issue of expensive composition is the key thing
//! that drove Rust towards adopting a different model. It is particularly a problem
//! when one considers, for example, the implications of composing together the [`Future`]s which
//! will eventually make up an asynchronous task (including address-sensitive `async fn` state
Expand Down
1 change: 0 additions & 1 deletion core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
#![feature(const_slice_from_ref)]
#![feature(waker_getters)]
#![feature(error_generic_member_access)]
#![feature(error_in_core)]
#![feature(trait_upcasting)]
#![feature(is_ascii_octdigit)]
#![feature(get_many_mut)]
Expand Down
4 changes: 2 additions & 2 deletions std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ addr2line = { version = "0.22.0", optional = true, default-features = false }
libc = { version = "0.2.153", default-features = false, features = ['rustc-dep-of-std'], public = true }

[target.'cfg(all(not(target_os = "aix"), not(all(windows, target_env = "msvc", not(target_vendor = "uwp")))))'.dependencies]
object = { version = "0.32.0", default-features = false, optional = true, features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive'] }
object = { version = "0.36.0", default-features = false, optional = true, features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive'] }

[target.'cfg(target_os = "aix")'.dependencies]
object = { version = "0.32.0", default-features = false, optional = true, features = ['read_core', 'xcoff', 'unaligned', 'archive'] }
object = { version = "0.36.0", default-features = false, optional = true, features = ['read_core', 'xcoff', 'unaligned', 'archive'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
Expand Down
19 changes: 19 additions & 0 deletions std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,25 @@ impl OsString {
unsafe { Box::from_raw(rw) }
}

/// Consumes and leaks the `OsString`, returning a mutable reference to the contents,
/// `&'a mut OsStr`.
///
/// The caller has free choice over the returned lifetime, including 'static.
/// Indeed, this function is ideally used for data that lives for the remainder of
/// the program’s life, as dropping the returned reference will cause a memory leak.
///
/// It does not reallocate or shrink the `OsString`, so the leaked allocation may include
/// unused capacity that is not part of the returned slice. If you want to discard excess
/// capacity, call [`into_boxed_os_str`], and then [`Box::leak`] instead.
/// However, keep in mind that trimming the capacity may result in a reallocation and copy.
///
/// [`into_boxed_os_str`]: Self::into_boxed_os_str
#[unstable(feature = "os_string_pathbuf_leak", issue = "125965")]
#[inline]
pub fn leak<'a>(self) -> &'a mut OsStr {
OsStr::from_inner_mut(self.inner.leak())
}

/// Part of a hack to make PathBuf::push/pop more efficient.
#[inline]
pub(crate) fn as_mut_vec_for_path_buf(&mut self) -> &mut Vec<u8> {
Expand Down
9 changes: 9 additions & 0 deletions std/src/ffi/os_str/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ fn test_os_string_clear() {
assert_eq!(0, os_string.inner.as_inner().len());
}

#[test]
fn test_os_string_leak() {
let os_string = OsString::from("have a cake");
let (len, cap) = (os_string.len(), os_string.capacity());
let leaked = os_string.leak();
assert_eq!(leaked.as_encoded_bytes(), b"have a cake");
unsafe { drop(String::from_raw_parts(leaked as *mut OsStr as _, len, cap)) }
}

#[test]
fn test_os_string_capacity() {
let os_string = OsString::with_capacity(0);
Expand Down
1 change: 0 additions & 1 deletion std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@
#![feature(core_io_borrowed_buf)]
#![feature(duration_constants)]
#![feature(error_generic_member_access)]
#![feature(error_in_core)]
#![feature(error_iter)]
#![feature(exact_size_is_empty)]
#![feature(exclusive_wrapper)]
Expand Down
19 changes: 19 additions & 0 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,25 @@ impl PathBuf {
self
}

/// Consumes and leaks the `PathBuf`, returning a mutable reference to the contents,
/// `&'a mut Path`.
///
/// The caller has free choice over the returned lifetime, including 'static.
/// Indeed, this function is ideally used for data that lives for the remainder of
/// the program’s life, as dropping the returned reference will cause a memory leak.
///
/// It does not reallocate or shrink the `PathBuf`, so the leaked allocation may include
/// unused capacity that is not part of the returned slice. If you want to discard excess
/// capacity, call [`into_boxed_path`], and then [`Box::leak`] instead.
/// However, keep in mind that trimming the capacity may result in a reallocation and copy.
///
/// [`into_boxed_path`]: Self::into_boxed_path
#[unstable(feature = "os_string_pathbuf_leak", issue = "125965")]
#[inline]
pub fn leak<'a>(self) -> &'a mut Path {
Path::from_inner_mut(self.inner.leak())
}

/// Extends `self` with `path`.
///
/// If `path` is absolute, it replaces the current path.
Expand Down
10 changes: 10 additions & 0 deletions std/src/path/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ fn into() {
assert_eq!(static_cow_path, owned_cow_path);
}

#[test]
fn test_pathbuf_leak() {
let string = "/have/a/cake".to_owned();
let (len, cap) = (string.len(), string.capacity());
let buf = PathBuf::from(string);
let leaked = buf.leak();
assert_eq!(leaked.as_os_str().as_encoded_bytes(), b"/have/a/cake");
unsafe { drop(String::from_raw_parts(leaked.as_mut_os_str() as *mut OsStr as _, len, cap)) }
}

#[test]
#[cfg(unix)]
pub fn test_decompositions_unix() {
Expand Down
5 changes: 5 additions & 0 deletions std/src/sys/os_str/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ impl Buf {
self.inner.extend_from_slice(&s.inner)
}

#[inline]
pub fn leak<'a>(self) -> &'a mut Slice {
unsafe { mem::transmute(self.inner.leak()) }
}

#[inline]
pub fn into_box(self) -> Box<Slice> {
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
Expand Down
5 changes: 5 additions & 0 deletions std/src/sys/os_str/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ impl Buf {
self.inner.shrink_to(min_capacity)
}

#[inline]
pub fn leak<'a>(self) -> &'a mut Slice {
unsafe { mem::transmute(self.inner.leak()) }
}

#[inline]
pub fn into_box(self) -> Box<Slice> {
unsafe { mem::transmute(self.inner.into_box()) }
Expand Down
2 changes: 1 addition & 1 deletion std/src/sys/pal/windows/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ be edited manually.

To add bindings, edit `bindings.txt` then regenerate using the following command:

./x run generate-windows-sys && ./x fmt library/std
./x run generate-windows-sys && ./x fmt

If you need to override generated functions or types then add them to
`library/std/src/sys/pal/windows/c.rs`.
5 changes: 5 additions & 0 deletions std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ impl Wtf8Buf {
self.bytes.shrink_to(min_capacity)
}

#[inline]
pub fn leak<'a>(self) -> &'a mut Wtf8 {
unsafe { Wtf8::from_mut_bytes_unchecked(self.bytes.leak()) }
}

/// Returns the number of bytes that this string buffer can hold without reallocating.
#[inline]
pub fn capacity(&self) -> usize {
Expand Down

0 comments on commit 05812a0

Please sign in to comment.