Skip to content

Commit

Permalink
Merge from rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
The Miri Cronjob Bot committed Sep 14, 2024
2 parents 4afc77f + f396849 commit f30a0ad
Show file tree
Hide file tree
Showing 29 changed files with 105 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"

[dependencies]
core = { path = "../core" }
compiler_builtins = { version = "0.1.123", features = ['rustc-dep-of-std'] }
compiler_builtins = { version = "0.1.125", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
Expand Down
13 changes: 13 additions & 0 deletions alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub use std::alloc::Global;
#[stable(feature = "global_alloc", since = "1.28.0")]
#[must_use = "losing the pointer will leak memory"]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
unsafe {
// Make sure we don't accidentally allow omitting the allocator shim in
Expand All @@ -113,6 +114,7 @@ pub unsafe fn alloc(layout: Layout) -> *mut u8 {
/// See [`GlobalAlloc::dealloc`].
#[stable(feature = "global_alloc", since = "1.28.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
}
Expand All @@ -132,6 +134,7 @@ pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
#[stable(feature = "global_alloc", since = "1.28.0")]
#[must_use = "losing the pointer will leak memory"]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
}
Expand Down Expand Up @@ -166,13 +169,15 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8
#[stable(feature = "global_alloc", since = "1.28.0")]
#[must_use = "losing the pointer will leak memory"]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
}

#[cfg(not(test))]
impl Global {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
match layout.size() {
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
Expand All @@ -187,6 +192,7 @@ impl Global {

// SAFETY: Same as `Allocator::grow`
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn grow_impl(
&self,
ptr: NonNull<u8>,
Expand Down Expand Up @@ -237,16 +243,19 @@ impl Global {
#[cfg(not(test))]
unsafe impl Allocator for Global {
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.alloc_impl(layout, false)
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.alloc_impl(layout, true)
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
if layout.size() != 0 {
// SAFETY: `layout` is non-zero in size,
Expand All @@ -256,6 +265,7 @@ unsafe impl Allocator for Global {
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn grow(
&self,
ptr: NonNull<u8>,
Expand All @@ -267,6 +277,7 @@ unsafe impl Allocator for Global {
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
Expand All @@ -278,6 +289,7 @@ unsafe impl Allocator for Global {
}

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
Expand Down Expand Up @@ -325,6 +337,7 @@ unsafe impl Allocator for Global {
#[cfg(all(not(no_global_oom_handling), not(test)))]
#[lang = "exchange_malloc"]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
match Global.allocate(layout) {
Expand Down
1 change: 1 addition & 0 deletions alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl<T> Box<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[rustc_diagnostic_item = "box_new"]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub fn new(x: T) -> Self {
#[rustc_box]
Box::new(x)
Expand Down
4 changes: 2 additions & 2 deletions alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ impl<T> VecDeque<T> {
#[rustc_const_stable(feature = "const_vec_deque_new", since = "1.68.0")]
#[must_use]
pub const fn new() -> VecDeque<T> {
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
VecDeque { head: 0, len: 0, buf: RawVec::NEW }
// FIXME(const-hack): This should just be `VecDeque::new_in(Global)` once that hits stable.
VecDeque { head: 0, len: 0, buf: RawVec::new() }
}

/// Creates an empty deque with space for at least `capacity` elements.
Expand Down
15 changes: 1 addition & 14 deletions alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ struct RawVecInner<A: Allocator = Global> {
}

impl<T> RawVec<T, Global> {
/// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so
/// they cannot call `Self::new()`.
///
/// If you change `RawVec<T>::new` or dependencies, please take care to not introduce anything
/// that would truly const-call something unstable.
pub const NEW: Self = Self::new();

/// Creates the biggest possible `RawVec` (on the system heap)
/// without allocating. If `T` has positive size, then this makes a
/// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a
Expand All @@ -111,7 +104,7 @@ impl<T> RawVec<T, Global> {
#[must_use]
#[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")]
pub const fn new() -> Self {
Self { inner: RawVecInner::new::<T>(), _marker: PhantomData }
Self::new_in(Global)
}

/// Creates a `RawVec` (on the system heap) with exactly the
Expand Down Expand Up @@ -149,12 +142,6 @@ impl<T> RawVec<T, Global> {
}

impl RawVecInner<Global> {
#[must_use]
#[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")]
const fn new<T>() -> Self {
Self::new_in(Global, core::mem::align_of::<T>())
}

#[cfg(not(any(no_global_oom_handling, test)))]
#[must_use]
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion alloc/src/vec/in_place_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const fn in_place_collectible<DEST, SRC>(

const fn needs_realloc<SRC, DEST>(src_cap: usize, dst_cap: usize) -> bool {
if const { mem::align_of::<SRC>() != mem::align_of::<DEST>() } {
// FIXME: use unreachable! once that works in const
// FIXME(const-hack): use unreachable! once that works in const
panic!("in_place_collectible() prevents this");
}

Expand Down
2 changes: 1 addition & 1 deletion alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
// struct and then overwriting &mut self.
// this creates less assembly
self.cap = 0;
self.buf = RawVec::NEW.non_null();
self.buf = RawVec::new().non_null();
self.ptr = self.buf;
self.end = self.buf.as_ptr();

Expand Down
2 changes: 1 addition & 1 deletion alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<T> Vec<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
pub const fn new() -> Self {
Vec { buf: RawVec::NEW, len: 0 }
Vec { buf: RawVec::new(), len: 0 }
}

/// Constructs a new, empty `Vec<T>` with at least the specified capacity.
Expand Down
2 changes: 1 addition & 1 deletion core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ub_checks::assert_unsafe_precondition;
#[must_use]
#[inline]
pub(super) const fn from_u32(i: u32) -> Option<char> {
// FIXME: once Result::ok is const fn, use it here
// FIXME(const-hack): once Result::ok is const fn, use it here
match char_try_from_u32(i) {
Ok(c) => Some(c),
Err(_) => None,
Expand Down
2 changes: 1 addition & 1 deletion core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl char {
// Force the 6th bit to be set to ensure ascii is lower case.
digit = (self as u32 | 0b10_0000).wrapping_sub('a' as u32).saturating_add(10);
}
// FIXME: once then_some is const fn, use it here
// FIXME(const-hack): once then_some is const fn, use it here
if digit < radix { Some(digit) } else { None }
}

Expand Down
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
#![feature(const_size_of_val_raw)]
#![feature(const_slice_from_raw_parts_mut)]
#![feature(const_slice_from_ref)]
#![feature(const_slice_index)]
#![feature(const_slice_split_at_mut)]
#![feature(const_str_from_utf8_unchecked_mut)]
#![feature(const_strict_overflow_ops)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::str::FromStr;
use crate::ub_checks::assert_unsafe_precondition;
use crate::{ascii, intrinsics, mem};

// Used because the `?` operator is not allowed in a const context.
// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
macro_rules! try_opt {
($e:expr) => {
match $e {
Expand Down
22 changes: 6 additions & 16 deletions core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ impl<T> Option<T> {
#[stable(feature = "pin", since = "1.33.0")]
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
pub const fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
// FIXME(const-hack): use `map` once that is possible
match Pin::get_ref(self).as_ref() {
// SAFETY: `x` is guaranteed to be pinned because it comes from `self`
// which is pinned.
Expand All @@ -758,6 +759,7 @@ impl<T> Option<T> {
// SAFETY: `get_unchecked_mut` is never used to move the `Option` inside `self`.
// `x` is guaranteed to be pinned because it comes from `self` which is pinned.
unsafe {
// FIXME(const-hack): use `map` once that is possible
match Pin::get_unchecked_mut(self).as_mut() {
Some(x) => Some(Pin::new_unchecked(x)),
None => None,
Expand Down Expand Up @@ -1290,10 +1292,7 @@ impl<T> Option<T> {
where
T: Deref,
{
match self.as_ref() {
Some(t) => Some(t.deref()),
None => None,
}
self.as_ref().map(|t| t.deref())
}

/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
Expand All @@ -1316,10 +1315,7 @@ impl<T> Option<T> {
where
T: DerefMut,
{
match self.as_mut() {
Some(t) => Some(t.deref_mut()),
None => None,
}
self.as_mut().map(|t| t.deref_mut())
}

/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1632,13 +1628,7 @@ impl<T> Option<T> {
#[inline]
#[stable(feature = "option_entry", since = "1.20.0")]
pub fn get_or_insert(&mut self, value: T) -> &mut T {
if let None = *self {
*self = Some(value);
}

// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
// variant in the code above.
unsafe { self.as_mut().unwrap_unchecked() }
self.get_or_insert_with(|| value)
}

/// Inserts the default value into the option if it is [`None`], then
Expand Down Expand Up @@ -1724,7 +1714,7 @@ impl<T> Option<T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn take(&mut self) -> Option<T> {
// FIXME replace `mem::replace` by `mem::take` when the latter is const ready
// FIXME(const-hack) replace `mem::replace` by `mem::take` when the latter is const ready
mem::replace(self, None)
}

Expand Down
25 changes: 25 additions & 0 deletions core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ pub macro unreachable_2021 {
),
}

/// Invokes a closure, aborting if the closure unwinds.
///
/// When compiled with aborting panics, this function is effectively a no-op.
/// With unwinding panics, an unwind results in another call into the panic
/// hook followed by a process abort.
///
/// # Notes
///
/// Instead of using this function, code should attempt to support unwinding.
/// Implementing [`Drop`] allows you to restore invariants uniformly in both
/// return and unwind paths.
///
/// If an unwind can lead to logical issues but not soundness issues, you
/// should allow the unwind. Opting out of [`UnwindSafe`] indicates to your
/// consumers that they need to consider correctness in the face of unwinds.
///
/// If an unwind would be unsound, then this function should be used in order
/// to prevent unwinds. However, note that `extern "C" fn` will automatically
/// convert unwinds to aborts, so using this function isn't necessary for FFI.
#[unstable(feature = "abort_unwind", issue = "130338")]
#[rustc_nounwind]
pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
f()
}

/// An internal trait used by std to pass data from std to `panic_unwind` and
/// other panic runtimes. Not intended to be stabilized any time soon, do not
/// use.
Expand Down
2 changes: 0 additions & 2 deletions core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ impl From<Alignment> for usize {
}
}

#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl cmp::Ord for Alignment {
#[inline]
Expand All @@ -208,7 +207,6 @@ impl cmp::Ord for Alignment {
}
}

#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl cmp::PartialOrd for Alignment {
#[inline]
Expand Down
Loading

0 comments on commit f30a0ad

Please sign in to comment.