Merge from rustc

This commit is contained in:
The Miri Cronjob Bot 2025-05-28 05:00:57 +00:00
commit 15f0fb03bb
330 changed files with 3717 additions and 2941 deletions

View file

@ -126,6 +126,7 @@ impl Layout {
#[rustc_const_stable(feature = "const_alloc_layout_unchecked", since = "1.36.0")]
#[must_use]
#[inline]
#[track_caller]
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
assert_unsafe_precondition!(
check_library_ub,

View file

@ -503,6 +503,7 @@ impl AsciiChar {
/// something useful. It might be tightened before stabilization.)
#[unstable(feature = "ascii_char", issue = "110998")]
#[inline]
#[track_caller]
pub const unsafe fn digit_unchecked(d: u8) -> Self {
assert_unsafe_precondition!(
check_language_ub,

View file

@ -22,6 +22,7 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
#[inline]
#[must_use]
#[allow(unnecessary_transmutes)]
#[track_caller]
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
// SAFETY: the caller must guarantee that `i` is a valid char value.
unsafe {

View file

@ -98,7 +98,7 @@ use crate::{intrinsics, ub_checks};
#[inline]
#[stable(feature = "unreachable", since = "1.27.0")]
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unreachable_unchecked() -> ! {
ub_checks::assert_unsafe_precondition!(
check_language_ub,

View file

@ -1,7 +1,10 @@
//! Compiler intrinsics.
//!
//! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
//! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
//! These are the imports making intrinsics available to Rust code. The actual implementations live in the compiler.
//! Some of these intrinsics are lowered to MIR in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_transform/src/lower_intrinsics.rs>.
//! The remaining intrinsics are implemented for the LLVM backend in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs>
//! and <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>,
//! and for const evaluation in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
//!
//! # Const intrinsics
//!
@ -20,28 +23,14 @@
//!
//! The volatile intrinsics provide operations intended to act on I/O
//! memory, which are guaranteed to not be reordered by the compiler
//! across other volatile intrinsics. See the LLVM documentation on
//! [[volatile]].
//!
//! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
//! across other volatile intrinsics. See [`read_volatile`][ptr::read_volatile]
//! and [`write_volatile`][ptr::write_volatile].
//!
//! # Atomics
//!
//! The atomic intrinsics provide common atomic operations on machine
//! words, with multiple possible memory orderings. They obey the same
//! semantics as C++11. See the LLVM documentation on [[atomics]].
//!
//! [atomics]: https://llvm.org/docs/Atomics.html
//!
//! A quick refresher on memory ordering:
//!
//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
//! take place after the barrier.
//! * Release - a barrier for releasing a lock. Preceding reads and writes
//! take place before the barrier.
//! * Sequentially consistent - sequentially consistent operations are
//! guaranteed to happen in order. This is the standard mode for working
//! with atomic types and is equivalent to Java's `volatile`.
//! words, with multiple possible memory orderings. See the
//! [atomic types][crate::sync::atomic] docs for details.
//!
//! # Unwinding
//!
@ -2622,7 +2611,7 @@ pub const fn three_way_compare<T: Copy>(lhs: T, rhss: T) -> crate::cmp::Ordering
#[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")]
#[rustc_nounwind]
#[rustc_intrinsic]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
#[miri::intrinsic_fallback_is_spec] // the fallbacks all `assume` to tell Miri
pub const unsafe fn disjoint_bitor<T: ~const fallback::DisjointBitOr>(a: T, b: T) -> T {
// SAFETY: same preconditions as this function.

View file

@ -555,7 +555,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -705,7 +705,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -855,7 +855,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -1199,7 +1199,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_neg(self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -1327,7 +1327,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -1448,7 +1448,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
assert_unsafe_precondition!(
check_language_ub,

View file

@ -388,6 +388,7 @@ where
#[rustc_const_stable(feature = "nonzero", since = "1.28.0")]
#[must_use]
#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(n: T) -> Self {
match Self::new(n) {
Some(n) => n,
@ -428,6 +429,7 @@ where
#[unstable(feature = "nonzero_from_mut", issue = "106290")]
#[must_use]
#[inline]
#[track_caller]
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
match Self::from_mut(n) {
Some(n) => n,

View file

@ -601,7 +601,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -791,7 +791,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -974,7 +974,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -1588,7 +1588,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
assert_unsafe_precondition!(
check_language_ub,
@ -1709,7 +1709,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
assert_unsafe_precondition!(
check_language_ub,

View file

@ -19,6 +19,7 @@ impl IndexRange {
/// # Safety
/// - `start <= end`
#[inline]
#[track_caller]
pub(crate) const unsafe fn new_unchecked(start: usize, end: usize) -> Self {
ub_checks::assert_unsafe_precondition!(
check_library_ub,

View file

@ -1253,6 +1253,36 @@ impl<T> Option<T> {
}
}
/// Maps an `Option<T>` to a `U` by applying function `f` to the contained
/// value if the option is [`Some`], otherwise if [`None`], returns the
/// [default value] for the type `U`.
///
/// # Examples
///
/// ```
/// #![feature(result_option_map_or_default)]
///
/// let x: Option<&str> = Some("hi");
/// let y: Option<&str> = None;
///
/// assert_eq!(x.map_or_default(|x| x.len()), 2);
/// assert_eq!(y.map_or_default(|y| y.len()), 0);
/// ```
///
/// [default value]: Default::default
#[inline]
#[unstable(feature = "result_option_map_or_default", issue = "138099")]
pub fn map_or_default<U, F>(self, f: F) -> U
where
U: Default,
F: FnOnce(T) -> U,
{
match self {
Some(t) => f(t),
None => U::default(),
}
}
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
///

View file

@ -73,6 +73,7 @@ impl Alignment {
/// It must *not* be zero.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(align: usize) -> Self {
assert_unsafe_precondition!(
check_language_ub,

View file

@ -442,7 +442,7 @@ impl<T: ?Sized> *const T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn offset(self, count: isize) -> *const T
where
T: Sized,
@ -495,7 +495,7 @@ impl<T: ?Sized> *const T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`.
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
@ -794,7 +794,7 @@ impl<T: ?Sized> *const T {
#[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
where
T: Sized,
@ -839,7 +839,7 @@ impl<T: ?Sized> *const T {
#[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *const U) -> usize {
// SAFETY: the caller must uphold the safety contract for `offset_from_unsigned`.
unsafe { self.cast::<u8>().offset_from_unsigned(origin.cast::<u8>()) }
@ -953,7 +953,7 @@ impl<T: ?Sized> *const T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn add(self, count: usize) -> Self
where
T: Sized,
@ -1005,7 +1005,7 @@ impl<T: ?Sized> *const T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`.
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
@ -1059,7 +1059,7 @@ impl<T: ?Sized> *const T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn sub(self, count: usize) -> Self
where
T: Sized,
@ -1117,7 +1117,7 @@ impl<T: ?Sized> *const T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`.
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
@ -1290,7 +1290,7 @@ impl<T: ?Sized> *const T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn read(self) -> T
where
T: Sized,
@ -1311,7 +1311,7 @@ impl<T: ?Sized> *const T {
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub unsafe fn read_volatile(self) -> T
where
T: Sized,
@ -1331,7 +1331,7 @@ impl<T: ?Sized> *const T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn read_unaligned(self) -> T
where
T: Sized,
@ -1351,7 +1351,7 @@ impl<T: ?Sized> *const T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
where
T: Sized,
@ -1371,7 +1371,7 @@ impl<T: ?Sized> *const T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
where
T: Sized,

View file

@ -1377,6 +1377,7 @@ pub const unsafe fn swap<T>(x: *mut T, y: *mut T) {
#[rustc_const_stable(feature = "const_swap_nonoverlapping", since = "1.88.0")]
#[rustc_diagnostic_item = "ptr_swap_nonoverlapping"]
#[rustc_allow_const_fn_unstable(const_eval_select)] // both implementations behave the same
#[track_caller]
pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
ub_checks::assert_unsafe_precondition!(
check_library_ub,
@ -1557,6 +1558,7 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, bytes: NonZero<usize
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_replace", since = "1.83.0")]
#[rustc_diagnostic_item = "ptr_replace"]
#[track_caller]
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
// SAFETY: the caller must guarantee that `dst` is valid to be
// cast to a mutable reference (valid for writes, aligned, initialized),
@ -1684,7 +1686,7 @@ pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
#[rustc_diagnostic_item = "ptr_read"]
pub const unsafe fn read<T>(src: *const T) -> T {
// It would be semantically correct to implement this via `copy_nonoverlapping`
@ -1802,7 +1804,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
#[inline]
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
#[rustc_diagnostic_item = "ptr_read_unaligned"]
pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
let mut tmp = MaybeUninit::<T>::uninit();
@ -1901,7 +1903,7 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[rustc_diagnostic_item = "ptr_write"]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn write<T>(dst: *mut T, src: T) {
// Semantically, it would be fine for this to be implemented as a
// `copy_nonoverlapping` and appropriate drop suppression of `src`.
@ -2005,7 +2007,7 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[rustc_diagnostic_item = "ptr_write_unaligned"]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
// SAFETY: the caller must guarantee that `dst` is valid for writes.
// `dst` cannot overlap `src` because the caller has mutable access
@ -2079,7 +2081,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
/// ```
#[inline]
#[stable(feature = "volatile", since = "1.9.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
#[rustc_diagnostic_item = "ptr_read_volatile"]
pub unsafe fn read_volatile<T>(src: *const T) -> T {
// SAFETY: the caller must uphold the safety contract for `volatile_load`.
@ -2160,7 +2162,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
#[inline]
#[stable(feature = "volatile", since = "1.9.0")]
#[rustc_diagnostic_item = "ptr_write_volatile"]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
unsafe {

View file

@ -438,7 +438,7 @@ impl<T: ?Sized> *mut T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn offset(self, count: isize) -> *mut T
where
T: Sized,
@ -493,7 +493,7 @@ impl<T: ?Sized> *mut T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_offset(self, count: isize) -> Self {
// SAFETY: the caller must uphold the safety contract for `offset`.
unsafe { self.cast::<u8>().offset(count).with_metadata_of(self) }
@ -968,7 +968,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn offset_from_unsigned(self, origin: *const T) -> usize
where
T: Sized,
@ -990,7 +990,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "ptr_sub_ptr", since = "1.87.0")]
#[rustc_const_stable(feature = "const_ptr_sub_ptr", since = "1.87.0")]
#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_offset_from_unsigned<U: ?Sized>(self, origin: *mut U) -> usize {
// SAFETY: the caller must uphold the safety contract for `byte_offset_from_unsigned`.
unsafe { (self as *const T).byte_offset_from_unsigned(origin) }
@ -1044,7 +1044,7 @@ impl<T: ?Sized> *mut T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn add(self, count: usize) -> Self
where
T: Sized,
@ -1096,7 +1096,7 @@ impl<T: ?Sized> *mut T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_add(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `add`.
unsafe { self.cast::<u8>().add(count).with_metadata_of(self) }
@ -1150,7 +1150,7 @@ impl<T: ?Sized> *mut T {
#[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn sub(self, count: usize) -> Self
where
T: Sized,
@ -1208,7 +1208,7 @@ impl<T: ?Sized> *mut T {
#[inline(always)]
#[stable(feature = "pointer_byte_offsets", since = "1.75.0")]
#[rustc_const_stable(feature = "const_pointer_byte_offsets", since = "1.75.0")]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn byte_sub(self, count: usize) -> Self {
// SAFETY: the caller must uphold the safety contract for `sub`.
unsafe { self.cast::<u8>().sub(count).with_metadata_of(self) }
@ -1375,7 +1375,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn read(self) -> T
where
T: Sized,
@ -1396,7 +1396,7 @@ impl<T: ?Sized> *mut T {
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub unsafe fn read_volatile(self) -> T
where
T: Sized,
@ -1416,7 +1416,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_read", since = "1.71.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn read_unaligned(self) -> T
where
T: Sized,
@ -1436,7 +1436,7 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_to(self, dest: *mut T, count: usize)
where
T: Sized,
@ -1456,7 +1456,7 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_to_nonoverlapping(self, dest: *mut T, count: usize)
where
T: Sized,
@ -1476,7 +1476,7 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_from(self, src: *const T, count: usize)
where
T: Sized,
@ -1496,7 +1496,7 @@ impl<T: ?Sized> *mut T {
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn copy_from_nonoverlapping(self, src: *const T, count: usize)
where
T: Sized,
@ -1526,7 +1526,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn write(self, val: T)
where
T: Sized,
@ -1545,7 +1545,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn write_bytes(self, val: u8, count: usize)
where
T: Sized,
@ -1566,7 +1566,7 @@ impl<T: ?Sized> *mut T {
/// [`ptr::write_volatile`]: crate::ptr::write_volatile()
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub unsafe fn write_volatile(self, val: T)
where
T: Sized,
@ -1586,7 +1586,7 @@ impl<T: ?Sized> *mut T {
#[stable(feature = "pointer_methods", since = "1.26.0")]
#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[track_caller]
pub const unsafe fn write_unaligned(self, val: T)
where
T: Sized,

View file

@ -216,6 +216,7 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
#[inline]
#[track_caller]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
// SAFETY: the caller must guarantee that `ptr` is non-null.
unsafe {

View file

@ -858,6 +858,36 @@ impl<T, E> Result<T, E> {
}
}
/// Maps a `Result<T, E>` to a `U` by applying function `f` to the contained
/// value if the result is [`Ok`], otherwise if [`Err`], returns the
/// [default value] for the type `U`.
///
/// # Examples
///
/// ```
/// #![feature(result_option_map_or_default)]
///
/// let x: Result<_, &str> = Ok("foo");
/// let y: Result<&str, _> = Err("bar");
///
/// assert_eq!(x.map_or_default(|x| x.len()), 3);
/// assert_eq!(y.map_or_default(|y| y.len()), 0);
/// ```
///
/// [default value]: Default::default
#[inline]
#[unstable(feature = "result_option_map_or_default", issue = "138099")]
pub fn map_or_default<U, F>(self, f: F) -> U
where
U: Default,
F: FnOnce(T) -> U,
{
match self {
Ok(t) => f(t),
Err(_) => U::default(),
}
}
/// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
/// contained [`Err`] value, leaving an [`Ok`] value untouched.
///

View file

@ -239,6 +239,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
assert_unsafe_precondition!(
check_language_ub,
@ -258,6 +259,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
assert_unsafe_precondition!(
check_library_ub,
@ -307,6 +309,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
assert_unsafe_precondition!(
check_library_ub,
@ -321,6 +324,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
assert_unsafe_precondition!(
check_library_ub,
@ -386,6 +390,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
assert_unsafe_precondition!(
check_library_ub,
@ -410,6 +415,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
}
#[inline]
#[track_caller]
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
assert_unsafe_precondition!(
check_library_ub,

View file

@ -631,6 +631,7 @@ impl<T> [T] {
#[rustc_no_implicit_autorefs]
#[inline]
#[must_use]
#[track_caller]
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where
I: SliceIndex<Self>,
@ -674,6 +675,7 @@ impl<T> [T] {
#[rustc_no_implicit_autorefs]
#[inline]
#[must_use]
#[track_caller]
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where
I: SliceIndex<Self>,
@ -935,6 +937,7 @@ impl<T> [T] {
/// [`swap`]: slice::swap
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[unstable(feature = "slice_swap_unchecked", issue = "88539")]
#[track_caller]
pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
assert_unsafe_precondition!(
check_library_ub,
@ -1307,6 +1310,7 @@ impl<T> [T] {
#[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
assert_unsafe_precondition!(
check_language_ub,
@ -1502,6 +1506,7 @@ impl<T> [T] {
#[rustc_const_stable(feature = "slice_as_chunks", since = "1.88.0")]
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
assert_unsafe_precondition!(
check_language_ub,
@ -2061,6 +2066,7 @@ impl<T> [T] {
#[rustc_const_stable(feature = "const_slice_split_at_unchecked", since = "1.77.0")]
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&[T], &[T]) {
// FIXME(const-hack): the const function `from_raw_parts` is used to make this
// function const; previously the implementation used
@ -2114,6 +2120,7 @@ impl<T> [T] {
#[rustc_const_stable(feature = "const_slice_split_at_mut", since = "1.83.0")]
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut [T], &mut [T]) {
let len = self.len();
let ptr = self.as_mut_ptr();
@ -4642,6 +4649,7 @@ impl<T> [T] {
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
#[stable(feature = "get_many_mut", since = "1.86.0")]
#[inline]
#[track_caller]
pub unsafe fn get_disjoint_unchecked_mut<I, const N: usize>(
&mut self,
indices: [I; N],

View file

@ -120,6 +120,7 @@ use crate::{array, ptr, ub_checks};
#[rustc_const_stable(feature = "const_slice_from_raw_parts", since = "1.64.0")]
#[must_use]
#[rustc_diagnostic_item = "slice_from_raw_parts"]
#[track_caller]
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
unsafe {
@ -174,6 +175,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
#[rustc_const_stable(feature = "const_slice_from_raw_parts_mut", since = "1.83.0")]
#[must_use]
#[rustc_diagnostic_item = "slice_from_raw_parts_mut"]
#[track_caller]
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
unsafe {
@ -270,6 +272,7 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
/// [valid]: ptr#safety
#[unstable(feature = "slice_from_ptr_range", issue = "89792")]
#[rustc_const_unstable(feature = "const_slice_from_ptr_range", issue = "89792")]
#[track_caller]
pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
// SAFETY: the caller must uphold the safety contract for `from_ptr_range`.
unsafe { from_raw_parts(range.start, range.end.offset_from_unsigned(range.start)) }

View file

@ -656,7 +656,7 @@ impl<'a, P: Pattern> SplitInternal<'a, P> {
None
}
#[inline(always)]
#[inline]
fn next(&mut self) -> Option<&'a str> {
if self.finished {
return None;

View file

@ -429,23 +429,8 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
SearchStep::Done
}
}
#[inline(always)]
#[inline]
fn next_match(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
return match self
.haystack
.as_bytes()
.get(self.finger..self.finger_back)?
.iter()
.position(|x| *x == self.utf8_encoded[0])
{
Some(x) => {
self.finger += x + 1;
Some((self.finger - 1, self.finger))
}
None => None,
};
}
loop {
// get the haystack after the last character found
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
@ -513,21 +498,6 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
}
#[inline]
fn next_match_back(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
return match self
.haystack
.get(self.finger..self.finger_back)?
.as_bytes()
.iter()
.rposition(|&x| x == self.utf8_encoded[0])
{
Some(x) => {
self.finger_back = self.finger + x;
Some((self.finger_back, self.finger_back + 1))
}
None => None,
};
}
let haystack = self.haystack.as_bytes();
loop {
// get the haystack up to but not including the last character searched

View file

@ -186,6 +186,7 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
}
}
#[inline]
#[track_caller]
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
let slice = slice as *const [u8];
@ -213,6 +214,7 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
}
}
#[inline]
#[track_caller]
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
let slice = slice as *mut [u8];
@ -288,6 +290,7 @@ unsafe impl SliceIndex<str> for range::Range<usize> {
}
}
#[inline]
#[track_caller]
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
let slice = slice as *const [u8];
@ -315,6 +318,7 @@ unsafe impl SliceIndex<str> for range::Range<usize> {
}
}
#[inline]
#[track_caller]
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
let slice = slice as *mut [u8];

View file

@ -63,11 +63,13 @@ macro_rules! assert_unsafe_precondition {
#[rustc_no_mir_inline]
#[inline]
#[rustc_nounwind]
#[track_caller]
const fn precondition_check($($name:$ty),*) {
if !$e {
::core::panicking::panic_nounwind(concat!("unsafe precondition(s) violated: ", $message,
let msg = concat!("unsafe precondition(s) violated: ", $message,
"\n\nThis indicates a bug in the program. \
This Undefined Behavior check is optional, and cannot be relied on for safety."));
This Undefined Behavior check is optional, and cannot be relied on for safety.");
::core::panicking::panic_nounwind_fmt(::core::fmt::Arguments::new_const(&[msg]), false);
}
}

View file

@ -391,6 +391,16 @@ impl fmt::Display for TryLockError {
}
}
#[unstable(feature = "file_lock", issue = "130994")]
impl From<TryLockError> for io::Error {
fn from(err: TryLockError) -> io::Error {
match err {
TryLockError::Error(err) => err,
TryLockError::WouldBlock => io::ErrorKind::WouldBlock.into(),
}
}
}
impl File {
/// Attempts to open a file in read-only mode.
///
@ -820,11 +830,14 @@ impl File {
///
/// fn main() -> std::io::Result<()> {
/// let f = File::create("foo.txt")?;
/// // Explicit handling of the WouldBlock error
/// match f.try_lock() {
/// Ok(_) => (),
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
/// Err(TryLockError::Error(err)) => return Err(err),
/// }
/// // Alternately, propagate the error as an io::Error
/// f.try_lock()?;
/// Ok(())
/// }
/// ```
@ -881,11 +894,14 @@ impl File {
///
/// fn main() -> std::io::Result<()> {
/// let f = File::open("foo.txt")?;
/// // Explicit handling of the WouldBlock error
/// match f.try_lock_shared() {
/// Ok(_) => (),
/// Err(TryLockError::WouldBlock) => (), // Lock not acquired
/// Err(TryLockError::Error(err)) => return Err(err),
/// }
/// // Alternately, propagate the error as an io::Error
/// f.try_lock_shared()?;
///
/// Ok(())
/// }

View file

@ -366,6 +366,28 @@ fn file_lock_blocking_async() {
t.join().unwrap();
}
#[test]
#[cfg(windows)]
fn file_try_lock_async() {
const FILE_FLAG_OVERLAPPED: u32 = 0x40000000;
let tmpdir = tmpdir();
let filename = &tmpdir.join("file_try_lock_async.txt");
let f1 = check!(File::create(filename));
let f2 =
check!(OpenOptions::new().custom_flags(FILE_FLAG_OVERLAPPED).write(true).open(filename));
// Check that shared locks block exclusive locks
check!(f1.lock_shared());
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
check!(f1.unlock());
// Check that exclusive locks block all locks
check!(f1.lock());
assert_matches!(f2.try_lock(), Err(TryLockError::WouldBlock));
assert_matches!(f2.try_lock_shared(), Err(TryLockError::WouldBlock));
}
#[test]
fn file_test_io_seek_shakedown() {
// 01234567890123

View file

@ -415,10 +415,7 @@ impl File {
match result {
Ok(_) => Ok(()),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
Err(TryLockError::WouldBlock)
}
Err(err) => Err(TryLockError::Error(err)),
@ -440,10 +437,7 @@ impl File {
match result {
Ok(_) => Ok(()),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
Err(err) if err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) => {
Err(TryLockError::WouldBlock)
}
Err(err) => Err(TryLockError::Error(err)),