-
Notifications
You must be signed in to change notification settings - Fork 13.3k
likely/unlikely intrinsics don't need unsafe #45445
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
Comments
Relates to #45440. If we can get them to propagate properly through inline(always) functions, we would be able to write a "safe" wrapper around them that we can stabilize. |
These are far from the only intrinsics that are safe, but there's currently no way to make intrinsics safe (short of a safe wrapper). I think I saw an issue about this at some point but I can't find it now. |
For safe wrappers for likely/unlikely intrinsics, one can also do macros with #[allow_internal_unsafe]
macro_rules! unlikely {
($e:expr) => {
unsafe {
unlikely($e)
}
};
} You can pair it with |
The snippet by @est31 should be: #[allow_internal_unsafe]
macro_rules! unlikely {
($e:expr) => {
#[allow(unused_unsafe)]
unsafe {
std::intrinsics::unlikely($e)
}
};
} And requires #![feature(core_intrinsics)]
#![feature(allow_internal_unsafe)]
#![feature(stmt_expr_attributes)] In order to work seamlessly as of today's nightly (in the case of likely/unlikely used within unsafe blocks). |
@jbayardo AFAIK you can also attach the |
That did not work for me. I believe this shouldn't be a macro though, the likely/unlikely intrinsics should just be safe; the unsafe {
...
if likely!(unsafe { ... }) {
...
} else {
...
}
} |
You can't just mark intrinsics safe, because all intrinsics are |
As mentioned, there are many other intrinsics that should not require unsafe. This is going to need an RFC around how to expose intrinsics that are safe to call. One previous RFC on this topic was closed rust-lang/rfcs#1248 (comment) but the lang team plans to follow up by merging the idea of "intrinsic" and "lang item", at which point |
Minor paper-cut, but these intrinsics are safe.
The text was updated successfully, but these errors were encountered: