Skip to content

library: Migrate from cfg_if to cfg_select #145489

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

Merged
merged 3 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ name = "std_detect"
version = "0.1.5"
dependencies = [
"alloc",
"cfg-if",
"core",
"libc",
]
Expand Down Expand Up @@ -376,7 +375,6 @@ dependencies = [
name = "unwind"
version = "0.0.0"
dependencies = [
"cfg-if",
"libc",
"rustc-std-workspace-core",
"unwinding",
Expand Down
1 change: 1 addition & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ crate-type = ["dylib", "rlib"]

[dependencies]
alloc = { path = "../alloc", public = true }
# std no longer uses cfg-if directly, but the included copy of backtrace does.
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
panic_unwind = { path = "../panic_unwind", optional = true }
panic_abort = { path = "../panic_abort" }
Expand Down
7 changes: 4 additions & 3 deletions library/std/src/io/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ where
R: Read,
W: Write,
{
cfg_if::cfg_if! {
if #[cfg(any(target_os = "linux", target_os = "android"))] {
cfg_select! {
any(target_os = "linux", target_os = "android") => {
crate::sys::kernel_copy::copy_spec(reader, writer)
} else {
}
_ => {
generic_copy(reader, writer)
}
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(cast_maybe_uninit)]
#![feature(cfg_select)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
#![feature(const_cmp)]
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
cfg_if::cfg_if! {
if #[cfg(any(
cfg_select! {
any(
target_os = "linux", target_os = "android",
target_os = "hurd",
target_os = "dragonfly", target_os = "freebsd",
target_os = "openbsd", target_os = "netbsd",
target_os = "solaris", target_os = "illumos",
target_os = "haiku", target_os = "nto",
target_os = "cygwin"))] {
target_os = "cygwin",
) => {
use libc::MSG_NOSIGNAL;
} else {
}
_ => {
const MSG_NOSIGNAL: core::ffi::c_int = 0x0;
}
}
Expand Down
12 changes: 6 additions & 6 deletions library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

#![stable(feature = "rust1", since = "1.0.0")]

use cfg_if::cfg_if;

use crate::ffi::OsStr;
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use crate::path::Path;
use crate::sealed::Sealed;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::{io, process, sys};

cfg_if! {
if #[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita"))] {
cfg_select! {
any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita") => {
type UserId = u16;
type GroupId = u16;
} else if #[cfg(target_os = "nto")] {
}
target_os = "nto" => {
// Both IDs are signed, see `sys/target_nto.h` of the QNX Neutrino SDP.
// Only positive values should be used, see e.g.
// https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/setuid.html
type UserId = i32;
type GroupId = i32;
} else {
}
_ => {
type UserId = u32;
type GroupId = u32;
}
Expand Down
9 changes: 4 additions & 5 deletions library/std/src/sync/reentrant_lock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cfg_if::cfg_if;

use crate::cell::UnsafeCell;
use crate::fmt;
use crate::ops::Deref;
Expand Down Expand Up @@ -87,8 +85,8 @@ pub struct ReentrantLock<T: ?Sized> {
data: T,
}

cfg_if!(
if #[cfg(target_has_atomic = "64")] {
cfg_select!(
target_has_atomic = "64" => {
use crate::sync::atomic::{Atomic, AtomicU64, Ordering::Relaxed};

struct Tid(Atomic<u64>);
Expand All @@ -110,7 +108,8 @@ cfg_if!(
self.0.store(value, Relaxed);
}
}
} else {
}
_ => {
/// Returns the address of a TLS variable. This is guaranteed to
/// be unique across all currently alive threads.
fn tls_addr() -> usize {
Expand Down
30 changes: 19 additions & 11 deletions library/std/src/sys/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,37 @@ unsafe fn realloc_fallback(
}
}

cfg_if::cfg_if! {
if #[cfg(any(
cfg_select! {
any(
target_family = "unix",
target_os = "wasi",
target_os = "teeos",
target_os = "trusty",
))] {
) => {
mod unix;
} else if #[cfg(target_os = "windows")] {
}
target_os = "windows" => {
mod windows;
} else if #[cfg(target_os = "hermit")] {
}
target_os = "hermit" => {
mod hermit;
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
}
all(target_vendor = "fortanix", target_env = "sgx") => {
mod sgx;
} else if #[cfg(target_os = "solid_asp3")] {
}
target_os = "solid_asp3" => {
mod solid;
} else if #[cfg(target_os = "uefi")] {
}
target_os = "uefi" => {
mod uefi;
} else if #[cfg(target_family = "wasm")] {
}
target_family = "wasm" => {
mod wasm;
} else if #[cfg(target_os = "xous")] {
}
target_os = "xous" => {
mod xous;
} else if #[cfg(target_os = "zkvm")] {
}
target_os = "zkvm" => {
mod zkvm;
}
}
10 changes: 4 additions & 6 deletions library/std/src/sys/alloc/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,16 @@ unsafe impl GlobalAlloc for System {
}
}

cfg_if::cfg_if! {
cfg_select! {
// We use posix_memalign wherever possible, but some targets have very incomplete POSIX coverage
// so we need a fallback for those.
if #[cfg(any(
target_os = "horizon",
target_os = "vita",
))] {
any(target_os = "horizon", target_os = "vita") => {
#[inline]
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 }
}
} else {
}
_ => {
#[inline]
#[cfg_attr(target_os = "vxworks", allow(unused_unsafe))]
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/sys/anonymous_pipe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![forbid(unsafe_op_in_unsafe_fn)]

cfg_if::cfg_if! {
if #[cfg(unix)] {
cfg_select! {
unix => {
mod unix;
pub use unix::{AnonPipe, pipe};
} else if #[cfg(windows)] {
}
windows => {
mod windows;
pub use windows::{AnonPipe, pipe};
} else {
}
_ => {
mod unsupported;
pub use unsupported::{AnonPipe, pipe};
}
Expand Down
27 changes: 17 additions & 10 deletions library/std/src/sys/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,39 @@
))]
mod common;

cfg_if::cfg_if! {
if #[cfg(any(
cfg_select! {
any(
all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))),
target_os = "hermit",
))] {
) => {
mod unix;
pub use unix::*;
} else if #[cfg(target_family = "windows")] {
}
target_family = "windows" => {
mod windows;
pub use windows::*;
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
}
all(target_vendor = "fortanix", target_env = "sgx") => {
mod sgx;
pub use sgx::*;
} else if #[cfg(target_os = "uefi")] {
}
target_os = "uefi" => {
mod uefi;
pub use uefi::*;
} else if #[cfg(target_os = "wasi")] {
}
target_os = "wasi" => {
mod wasi;
pub use wasi::*;
} else if #[cfg(target_os = "xous")] {
}
target_os = "xous" => {
mod xous;
pub use xous::*;
} else if #[cfg(target_os = "zkvm")] {
}
target_os = "zkvm" => {
mod zkvm;
pub use zkvm::*;
} else {
}
_ => {
mod unsupported;
pub use unsupported::*;
}
Expand Down
111 changes: 56 additions & 55 deletions library/std/src/sys/cmath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,69 +45,70 @@ unsafe extern "C" {
pub safe fn lgammaf128_r(n: f128, s: &mut i32) -> f128;
pub safe fn erff128(n: f128) -> f128;
pub safe fn erfcf128(n: f128) -> f128;

cfg_if::cfg_if! {
if #[cfg(not(all(target_os = "windows", target_env = "msvc", target_arch = "x86")))] {
pub safe fn acosf(n: f32) -> f32;
pub safe fn asinf(n: f32) -> f32;
pub safe fn atan2f(a: f32, b: f32) -> f32;
pub safe fn atanf(n: f32) -> f32;
pub safe fn coshf(n: f32) -> f32;
pub safe fn sinhf(n: f32) -> f32;
pub safe fn tanf(n: f32) -> f32;
pub safe fn tanhf(n: f32) -> f32;
}}
}

// On AIX, we don't have lgammaf_r only the f64 version, so we can
// use the f64 version lgamma_r
#[cfg(target_os = "aix")]
pub fn lgammaf_r(n: f32, s: &mut i32) -> f32 {
lgamma_r(n.into(), s) as f32
}
cfg_select! {
all(target_os = "windows", target_env = "msvc", target_arch = "x86") => {
// On 32-bit x86 MSVC these functions aren't defined, so we just define shims
// which promote everything to f64, perform the calculation, and then demote
// back to f32. While not precisely correct should be "correct enough" for now.
#[inline]
pub fn acosf(n: f32) -> f32 {
f64::acos(n as f64) as f32
}

// On 32-bit x86 MSVC these functions aren't defined, so we just define shims
// which promote everything to f64, perform the calculation, and then demote
// back to f32. While not precisely correct should be "correct enough" for now.
cfg_if::cfg_if! {
if #[cfg(all(target_os = "windows", target_env = "msvc", target_arch = "x86"))] {
#[inline]
pub fn acosf(n: f32) -> f32 {
f64::acos(n as f64) as f32
}
#[inline]
pub fn asinf(n: f32) -> f32 {
f64::asin(n as f64) as f32
}

#[inline]
pub fn asinf(n: f32) -> f32 {
f64::asin(n as f64) as f32
}
#[inline]
pub fn atan2f(n: f32, b: f32) -> f32 {
f64::atan2(n as f64, b as f64) as f32
}

#[inline]
pub fn atan2f(n: f32, b: f32) -> f32 {
f64::atan2(n as f64, b as f64) as f32
}
#[inline]
pub fn atanf(n: f32) -> f32 {
f64::atan(n as f64) as f32
}

#[inline]
pub fn atanf(n: f32) -> f32 {
f64::atan(n as f64) as f32
}
#[inline]
pub fn coshf(n: f32) -> f32 {
f64::cosh(n as f64) as f32
}

#[inline]
pub fn coshf(n: f32) -> f32 {
f64::cosh(n as f64) as f32
}
#[inline]
pub fn sinhf(n: f32) -> f32 {
f64::sinh(n as f64) as f32
}

#[inline]
pub fn sinhf(n: f32) -> f32 {
f64::sinh(n as f64) as f32
}
#[inline]
pub fn tanf(n: f32) -> f32 {
f64::tan(n as f64) as f32
}

#[inline]
pub fn tanf(n: f32) -> f32 {
f64::tan(n as f64) as f32
#[inline]
pub fn tanhf(n: f32) -> f32 {
f64::tanh(n as f64) as f32
}
}

#[inline]
pub fn tanhf(n: f32) -> f32 {
f64::tanh(n as f64) as f32
_ => {
unsafe extern "C" {
pub safe fn acosf(n: f32) -> f32;
pub safe fn asinf(n: f32) -> f32;
pub safe fn atan2f(a: f32, b: f32) -> f32;
pub safe fn atanf(n: f32) -> f32;
pub safe fn coshf(n: f32) -> f32;
pub safe fn sinhf(n: f32) -> f32;
pub safe fn tanf(n: f32) -> f32;
pub safe fn tanhf(n: f32) -> f32;
}
}
}}
}

// On AIX, we don't have lgammaf_r only the f64 version, so we can
// use the f64 version lgamma_r
#[cfg(target_os = "aix")]
pub fn lgammaf_r(n: f32, s: &mut i32) -> f32 {
lgamma_r(n.into(), s) as f32
}
Loading
Loading