Rollup merge of #146878 - RalfJung:check_language_ub, r=tgross35

assert_unsafe_precondition: fix some incorrect check_language_ub

r? `@tgross35`
This commit is contained in:
Stuart Cook 2025-09-22 20:25:17 +10:00 committed by GitHub
commit 8f80707bc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 10 additions and 9 deletions

View file

@ -515,7 +515,7 @@ impl AsciiChar {
#[track_caller]
pub const unsafe fn digit_unchecked(d: u8) -> Self {
assert_unsafe_precondition!(
check_language_ub,
check_library_ub,
"`ascii::Char::digit_unchecked` input cannot exceed 9.",
(d: u8 = d) => d < 10
);

View file

@ -1460,8 +1460,8 @@ macro_rules! int_impl {
#[inline]
pub const unsafe fn unchecked_exact_shl(self, rhs: u32) -> $SelfT {
assert_unsafe_precondition!(
check_language_ub,
concat!(stringify!($SelfT), "::unchecked_exact_shl cannot shift out non-zero bits"),
check_library_ub,
concat!(stringify!($SelfT), "::unchecked_exact_shl cannot shift out bits that would change the value of the first bit"),
(
zeros: u32 = self.leading_zeros(),
ones: u32 = self.leading_ones(),
@ -1638,7 +1638,7 @@ macro_rules! int_impl {
#[inline]
pub const unsafe fn unchecked_exact_shr(self, rhs: u32) -> $SelfT {
assert_unsafe_precondition!(
check_language_ub,
check_library_ub,
concat!(stringify!($SelfT), "::unchecked_exact_shr cannot shift out non-zero bits"),
(
zeros: u32 = self.trailing_zeros(),

View file

@ -1865,7 +1865,7 @@ macro_rules! uint_impl {
#[inline]
pub const unsafe fn unchecked_exact_shl(self, rhs: u32) -> $SelfT {
assert_unsafe_precondition!(
check_language_ub,
check_library_ub,
concat!(stringify!($SelfT), "::exact_shl_unchecked cannot shift out non-zero bits"),
(
zeros: u32 = self.leading_zeros(),
@ -2037,7 +2037,7 @@ macro_rules! uint_impl {
#[inline]
pub const unsafe fn unchecked_exact_shr(self, rhs: u32) -> $SelfT {
assert_unsafe_precondition!(
check_language_ub,
check_library_ub,
concat!(stringify!($SelfT), "::exact_shr_unchecked cannot shift out non-zero bits"),
(
zeros: u32 = self.trailing_zeros(),

View file

@ -233,7 +233,7 @@ unsafe impl<T> const SliceIndex<[T]> for usize {
#[track_caller]
unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
assert_unsafe_precondition!(
check_language_ub,
check_language_ub, // okay because of the `assume` below
"slice::get_unchecked requires that the index is within the slice",
(this: usize = self, len: usize = slice.len()) => this < len
);

View file

@ -21,8 +21,9 @@ use crate::intrinsics::{self, const_eval_select};
/// slow down const-eval/Miri and we'll get the panic message instead of the interpreter's nice
/// diagnostic, but our ability to detect UB is unchanged.
/// But if `check_language_ub` is used when the check is actually for library UB, the check is
/// omitted in const-eval/Miri and thus if we eventually execute language UB which relies on the
/// library UB, the backtrace Miri reports may be far removed from original cause.
/// omitted in const-eval/Miri and thus UB might occur undetected. Even if we eventually execute
/// language UB which relies on the library UB, the backtrace Miri reports may be far removed from
/// original cause.
///
/// These checks are behind a condition which is evaluated at codegen time, not expansion time like
/// [`debug_assert`]. This means that a standard library built with optimizations and debug