Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert_unsafe_precondition!: assume() the expression #106220

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,22 +2274,27 @@ extern "rust-intrinsic" {
#[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
macro_rules! assert_unsafe_precondition {
($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
if cfg!(debug_assertions) {
{
// allow non_snake_case to allow capturing const generics
#[allow(non_snake_case)]
#[inline(always)]
fn runtime$(<$($tt)*>)?($($i:$ty),*) {
if !$e {
// don't unwind to reduce impact on code size
::core::panicking::panic_nounwind(
concat!("unsafe precondition(s) violated: ", $name)
);
if cfg!(debug_assertions) {
if !$e {
// don't unwind to reduce impact on code size
$crate::panicking::panic_nounwind(
concat!("unsafe precondition(s) violated: ", $name)
);
}
} else {
// SAFETY: the caller must ensure this
unsafe { $crate::intrinsics::assume($e) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro should only be invoked from unsafe contexts, so this explicit unsafe block is superfluous (and indeed potentially harmful, as it could introduce UB if the macro is accidentally invoked from a safe context).

Suggested change
unsafe { $crate::intrinsics::assume($e) };
$crate::intrinsics::assume($e);

}
}
#[allow(non_snake_case)]
const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}

::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
$crate::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
}
};
}
Expand Down