Skip to content

Commit ae1916b

Browse files
Rollup merge of #79058 - dtolnay:likelymacro, r=Mark-Simulacrum
Move likely/unlikely argument outside of invisible unsafe block The previous `likely!`/`unlikely!` macros were unsound because it permits the caller's expr to contain arbitrary unsafe code. ```rust pub fn huh() -> bool { likely!(std::ptr::read(&() as *const () as *const bool)) } ``` **Before:** compiles cleanly. **After:** ```console error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | 70 | likely!(std::ptr::read(&() as *const () as *const bool)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior ```
2 parents bc9ef6c + afb8170 commit ae1916b

File tree

1 file changed

+6
-6
lines changed
  • compiler/rustc_data_structures/src

1 file changed

+6
-6
lines changed

compiler/rustc_data_structures/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ pub fn cold_path<F: FnOnce() -> R, R>(f: F) -> R {
4747
#[macro_export]
4848
macro_rules! likely {
4949
($e:expr) => {
50-
#[allow(unused_unsafe)]
51-
{
52-
unsafe { std::intrinsics::likely($e) }
50+
match $e {
51+
#[allow(unused_unsafe)]
52+
e => unsafe { std::intrinsics::likely(e) },
5353
}
5454
};
5555
}
5656

5757
#[macro_export]
5858
macro_rules! unlikely {
5959
($e:expr) => {
60-
#[allow(unused_unsafe)]
61-
{
62-
unsafe { std::intrinsics::unlikely($e) }
60+
match $e {
61+
#[allow(unused_unsafe)]
62+
e => unsafe { std::intrinsics::unlikely(e) },
6363
}
6464
};
6565
}

0 commit comments

Comments
 (0)