about summary refs log tree commit diff
path: root/library/std_detect/src
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2025-08-10 14:25:43 -0700
committerJosh Triplett <josh@joshtriplett.org>2025-08-16 05:28:31 -0700
commit1ae4a0cc3487f28b29f36afe8056535afad21e7b (patch)
treea408ec3e10388ca37142ab6456a8ec7b83b141c6 /library/std_detect/src
parent3507a749b365aae4eefa96ab700a9315d3280ee7 (diff)
downloadrust-1ae4a0cc3487f28b29f36afe8056535afad21e7b.tar.gz
rust-1ae4a0cc3487f28b29f36afe8056535afad21e7b.zip
library: Migrate from `cfg_if` to `cfg_select`
Migrate the standard library from using the external `cfg_if` crate to
using the now-built-in `cfg_select` macro.

This does not yet eliminate the dependency from
`library/std/Cargo.toml`, because while the standard library itself no
longer uses `cfg_if`, it also incorporates the `backtrace` crate, which
does.

Migration assisted by the following vim command (after selecting the
full `cfg_if!` invocation):

```
'<,'>s/\(cfg_if::\)\?cfg_if/cfg_select/ | '<,'>s/^\( *\)} else {/\1}\r\1_ => {/c | '<,'>s/^\( *\)} else if #\[cfg(\(.*\))\] /\1}\r\1\2 => /e | '<,'>s/if #\[cfg(\(.*\))\] {/\1 => {/e
```

This is imperfect, but substantially accelerated the process. This
prompts for confirmation on the `} else {` since that can also appear
inside one of the arms. This also requires manual intervention to handle
any multi-line conditions.
Diffstat (limited to 'library/std_detect/src')
-rw-r--r--library/std_detect/src/detect/arch/mod.rs36
-rw-r--r--library/std_detect/src/detect/cache.rs14
-rw-r--r--library/std_detect/src/detect/mod.rs36
-rw-r--r--library/std_detect/src/detect/os/freebsd/mod.rs13
-rw-r--r--library/std_detect/src/detect/os/linux/auxvec.rs9
-rw-r--r--library/std_detect/src/detect/os/linux/auxvec/tests.rs8
-rw-r--r--library/std_detect/src/detect/os/linux/mod.rs25
-rw-r--r--library/std_detect/src/lib.rs2
8 files changed, 85 insertions, 58 deletions
diff --git a/library/std_detect/src/detect/arch/mod.rs b/library/std_detect/src/detect/arch/mod.rs
index b0be554ed89..c066b9cc681 100644
--- a/library/std_detect/src/detect/arch/mod.rs
+++ b/library/std_detect/src/detect/arch/mod.rs
@@ -1,7 +1,5 @@
 #![allow(dead_code)]
 
-use cfg_if::cfg_if;
-
 // Export the macros for all supported architectures.
 #[macro_use]
 mod x86;
@@ -24,38 +22,48 @@ mod loongarch;
 #[macro_use]
 mod s390x;
 
-cfg_if! {
-    if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
+cfg_select! {
+    any(target_arch = "x86", target_arch = "x86_64") => {
         #[stable(feature = "simd_x86", since = "1.27.0")]
         pub use x86::*;
-    } else if #[cfg(target_arch = "arm")] {
+    }
+    target_arch = "arm" => {
         #[unstable(feature = "stdarch_arm_feature_detection", issue = "111190")]
         pub use arm::*;
-    } else if #[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))] {
+    }
+    any(target_arch = "aarch64", target_arch = "arm64ec") => {
         #[stable(feature = "simd_aarch64", since = "1.60.0")]
         pub use aarch64::*;
-    } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
+    }
+    any(target_arch = "riscv32", target_arch = "riscv64") => {
         #[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")]
         pub use riscv::*;
-    } else if #[cfg(target_arch = "powerpc")] {
+    }
+    target_arch = "powerpc" => {
         #[unstable(feature = "stdarch_powerpc_feature_detection", issue = "111191")]
         pub use powerpc::*;
-    } else if #[cfg(target_arch = "powerpc64")] {
+    }
+    target_arch = "powerpc64" => {
         #[unstable(feature = "stdarch_powerpc_feature_detection", issue = "111191")]
         pub use powerpc64::*;
-    } else if #[cfg(target_arch = "mips")] {
+    }
+    target_arch = "mips" => {
         #[unstable(feature = "stdarch_mips_feature_detection", issue = "111188")]
         pub use mips::*;
-    } else if #[cfg(target_arch = "mips64")] {
+    }
+    target_arch = "mips64" => {
         #[unstable(feature = "stdarch_mips_feature_detection", issue = "111188")]
         pub use mips64::*;
-    } else if #[cfg(any(target_arch = "loongarch32", target_arch = "loongarch64"))] {
+    }
+    any(target_arch = "loongarch32", target_arch = "loongarch64") => {
         #[stable(feature = "stdarch_loongarch_feature", since = "1.89.0")]
         pub use loongarch::*;
-    } else if #[cfg(target_arch = "s390x")] {
+    }
+    target_arch = "s390x" => {
         #[unstable(feature = "stdarch_s390x_feature_detection", issue = "135413")]
         pub use s390x::*;
-    } else {
+    }
+    _ => {
         // Unimplemented architecture:
         #[doc(hidden)]
         pub(crate) enum Feature {
diff --git a/library/std_detect/src/detect/cache.rs b/library/std_detect/src/detect/cache.rs
index 1a42e091463..c0c0b7b7f86 100644
--- a/library/std_detect/src/detect/cache.rs
+++ b/library/std_detect/src/detect/cache.rs
@@ -101,8 +101,8 @@ impl Cache {
     }
 }
 
-cfg_if::cfg_if! {
-    if #[cfg(feature = "std_detect_env_override")] {
+cfg_select! {
+    feature = "std_detect_env_override" => {
         #[inline]
         fn disable_features(disable: &[u8], value: &mut Initializer) {
             if let Ok(disable) = core::str::from_utf8(disable) {
@@ -116,8 +116,8 @@ cfg_if::cfg_if! {
         fn initialize(mut value: Initializer) -> Initializer {
             use core::ffi::CStr;
             const RUST_STD_DETECT_UNSTABLE: &CStr = c"RUST_STD_DETECT_UNSTABLE";
-            cfg_if::cfg_if! {
-                if #[cfg(windows)] {
+            cfg_select! {
+                windows => {
                     use alloc::vec;
                     #[link(name = "kernel32")]
                     unsafe extern "system" {
@@ -132,7 +132,8 @@ cfg_if::cfg_if! {
                             disable_features(&env[..len as usize], &mut value);
                         }
                     }
-                } else {
+                }
+                _ => {
                     let env = unsafe {
                         libc::getenv(RUST_STD_DETECT_UNSTABLE.as_ptr())
                     };
@@ -146,7 +147,8 @@ cfg_if::cfg_if! {
             do_initialize(value);
             value
         }
-    } else {
+    }
+    _ => {
         #[inline]
         fn initialize(value: Initializer) -> Initializer {
             do_initialize(value);
diff --git a/library/std_detect/src/detect/mod.rs b/library/std_detect/src/detect/mod.rs
index f936a5a1345..2bc6e9a24db 100644
--- a/library/std_detect/src/detect/mod.rs
+++ b/library/std_detect/src/detect/mod.rs
@@ -17,8 +17,6 @@
 //! due to security concerns (x86 is the big exception). These functions are
 //! implemented in the `os/{target_os}.rs` modules.
 
-use cfg_if::cfg_if;
-
 #[macro_use]
 mod macros;
 
@@ -34,8 +32,8 @@ pub(crate) use self::arch::Feature;
 mod bit;
 mod cache;
 
-cfg_if! {
-    if #[cfg(miri)] {
+cfg_select! {
+    miri => {
         // When running under miri all target-features that are not enabled at
         // compile-time are reported as disabled at run-time.
         //
@@ -43,35 +41,42 @@ cfg_if! {
         // this run-time detection logic is never called.
         #[path = "os/other.rs"]
         mod os;
-    } else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
+    }
+    any(target_arch = "x86", target_arch = "x86_64") => {
         // On x86/x86_64 no OS specific functionality is required.
         #[path = "os/x86.rs"]
         mod os;
-    } else if #[cfg(all(any(target_os = "linux", target_os = "android"), feature = "libc"))] {
+    }
+    all(any(target_os = "linux", target_os = "android"), feature = "libc") => {
         #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
         #[path = "os/riscv.rs"]
         mod riscv;
         #[path = "os/linux/mod.rs"]
         mod os;
-    } else if #[cfg(all(target_os = "freebsd", feature = "libc"))] {
+    }
+    all(target_os = "freebsd", feature = "libc") => {
         #[cfg(target_arch = "aarch64")]
         #[path = "os/aarch64.rs"]
         mod aarch64;
         #[path = "os/freebsd/mod.rs"]
         mod os;
-    } else if #[cfg(all(target_os = "openbsd", target_arch = "aarch64", feature = "libc"))] {
+    }
+    all(target_os = "openbsd", target_arch = "aarch64", feature = "libc") => {
         #[allow(dead_code)] // we don't use code that calls the mrs instruction.
         #[path = "os/aarch64.rs"]
         mod aarch64;
         #[path = "os/openbsd/aarch64.rs"]
         mod os;
-    } else if #[cfg(all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")))] {
+    }
+    all(target_os = "windows", any(target_arch = "aarch64", target_arch = "arm64ec")) => {
         #[path = "os/windows/aarch64.rs"]
         mod os;
-    } else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64", feature = "libc"))] {
+    }
+    all(target_vendor = "apple", target_arch = "aarch64", feature = "libc") => {
         #[path = "os/darwin/aarch64.rs"]
         mod os;
-    } else {
+    }
+    _ => {
         #[path = "os/other.rs"]
         mod os;
     }
@@ -89,8 +94,8 @@ fn check_for(x: Feature) -> bool {
 /// is `true` if the feature is supported by the host and `false` otherwise.
 #[unstable(feature = "stdarch_internal", issue = "none")]
 pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
-    cfg_if! {
-        if #[cfg(any(
+    cfg_select! {
+        any(
             target_arch = "x86",
             target_arch = "x86_64",
             target_arch = "arm",
@@ -105,7 +110,7 @@ pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
             target_arch = "loongarch32",
             target_arch = "loongarch64",
             target_arch = "s390x",
-        ))] {
+        ) => {
             (0_u8..Feature::_last as u8).map(|discriminant: u8| {
                 #[allow(bindings_with_variant_name)] // RISC-V has Feature::f
                 let f: Feature = unsafe { core::mem::transmute(discriminant) };
@@ -113,8 +118,7 @@ pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
                 let enabled: bool = check_for(f);
                 (name, enabled)
             })
-        } else {
-            None.into_iter()
         }
+        _ => None.into_iter(),
     }
 }
diff --git a/library/std_detect/src/detect/os/freebsd/mod.rs b/library/std_detect/src/detect/os/freebsd/mod.rs
index ade7fb6269d..7de9250e835 100644
--- a/library/std_detect/src/detect/os/freebsd/mod.rs
+++ b/library/std_detect/src/detect/os/freebsd/mod.rs
@@ -2,17 +2,20 @@
 
 mod auxvec;
 
-cfg_if::cfg_if! {
-    if #[cfg(target_arch = "aarch64")] {
+cfg_select! {
+    target_arch = "aarch64" => {
         mod aarch64;
         pub(crate) use self::aarch64::detect_features;
-    } else if #[cfg(target_arch = "arm")] {
+    }
+    target_arch = "arm" => {
         mod arm;
         pub(crate) use self::arm::detect_features;
-    } else if #[cfg(target_arch = "powerpc64")] {
+    }
+    target_arch = "powerpc64" => {
         mod powerpc;
         pub(crate) use self::powerpc::detect_features;
-    } else {
+    }
+    _ => {
         use crate::detect::cache;
         /// Performs run-time feature detection.
         pub(crate) fn detect_features() -> cache::Initializer {
diff --git a/library/std_detect/src/detect/os/linux/auxvec.rs b/library/std_detect/src/detect/os/linux/auxvec.rs
index 443caaaa186..75e01bdc449 100644
--- a/library/std_detect/src/detect/os/linux/auxvec.rs
+++ b/library/std_detect/src/detect/os/linux/auxvec.rs
@@ -131,15 +131,15 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
 /// `getauxval` function. If the function is not linked, this function return `Err`.
 fn getauxval(key: usize) -> Result<usize, ()> {
     type F = unsafe extern "C" fn(libc::c_ulong) -> libc::c_ulong;
-    cfg_if::cfg_if! {
-        if #[cfg(all(
+    cfg_select! {
+        all(
             feature = "std_detect_dlsym_getauxval",
             not(all(
                 target_os = "linux",
                 any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
             )),
             not(target_os = "android"),
-        ))] {
+        ) => {
             let ffi_getauxval: F = unsafe {
                 let ptr = libc::dlsym(libc::RTLD_DEFAULT, c"getauxval".as_ptr());
                 if ptr.is_null() {
@@ -147,7 +147,8 @@ fn getauxval(key: usize) -> Result<usize, ()> {
                 }
                 core::mem::transmute(ptr)
             };
-        } else {
+        }
+        _ => {
             let ffi_getauxval: F = libc::getauxval;
         }
     }
diff --git a/library/std_detect/src/detect/os/linux/auxvec/tests.rs b/library/std_detect/src/detect/os/linux/auxvec/tests.rs
index 536615fa272..631a3e5e9ef 100644
--- a/library/std_detect/src/detect/os/linux/auxvec/tests.rs
+++ b/library/std_detect/src/detect/os/linux/auxvec/tests.rs
@@ -42,8 +42,8 @@ fn auxv_dump() {
 }
 
 #[cfg(feature = "std_detect_file_io")]
-cfg_if::cfg_if! {
-    if #[cfg(target_arch = "arm")] {
+cfg_select! {
+    target_arch = "arm" => {
         // The tests below can be executed under qemu, where we do not have access to the test
         // files on disk, so we need to embed them with `include_bytes!`.
         #[test]
@@ -62,7 +62,8 @@ cfg_if::cfg_if! {
             assert_eq!(v.hwcap, 126614527);
             assert_eq!(v.hwcap2, 0);
         }
-    } else if #[cfg(target_arch = "aarch64")] {
+    }
+    target_arch = "aarch64" => {
         #[cfg(target_endian = "little")]
         #[test]
         fn linux_artificial_aarch64() {
@@ -81,6 +82,7 @@ cfg_if::cfg_if! {
             assert_eq!(v.hwcap2, 0);
         }
     }
+    _ => {}
 }
 
 #[test]
diff --git a/library/std_detect/src/detect/os/linux/mod.rs b/library/std_detect/src/detect/os/linux/mod.rs
index 5ae2aaeab5b..5273c16c089 100644
--- a/library/std_detect/src/detect/os/linux/mod.rs
+++ b/library/std_detect/src/detect/os/linux/mod.rs
@@ -37,29 +37,36 @@ fn read_file(orig_path: &str) -> Result<Vec<u8>, alloc::string::String> {
     }
 }
 
-cfg_if::cfg_if! {
-    if #[cfg(target_arch = "aarch64")] {
+cfg_select! {
+    target_arch = "aarch64" => {
         mod aarch64;
         pub(crate) use self::aarch64::detect_features;
-    } else if #[cfg(target_arch = "arm")] {
+    }
+    target_arch = "arm" => {
         mod arm;
         pub(crate) use self::arm::detect_features;
-    } else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
+    }
+    any(target_arch = "riscv32", target_arch = "riscv64") => {
         mod riscv;
         pub(crate) use self::riscv::detect_features;
-    } else if #[cfg(any(target_arch = "mips", target_arch = "mips64"))] {
+    }
+    any(target_arch = "mips", target_arch = "mips64") => {
         mod mips;
         pub(crate) use self::mips::detect_features;
-    } else if #[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] {
+    }
+    any(target_arch = "powerpc", target_arch = "powerpc64") => {
         mod powerpc;
         pub(crate) use self::powerpc::detect_features;
-    } else if #[cfg(any(target_arch = "loongarch32", target_arch = "loongarch64"))] {
+    }
+    any(target_arch = "loongarch32", target_arch = "loongarch64") => {
         mod loongarch;
         pub(crate) use self::loongarch::detect_features;
-    } else if #[cfg(target_arch = "s390x")] {
+    }
+    target_arch = "s390x" => {
         mod s390x;
         pub(crate) use self::s390x::detect_features;
-    } else {
+    }
+    _ => {
         use crate::detect::cache;
         /// Performs run-time feature detection.
         pub(crate) fn detect_features() -> cache::Initializer {
diff --git a/library/std_detect/src/lib.rs b/library/std_detect/src/lib.rs
index ab1b77bad5b..73e2f5dd964 100644
--- a/library/std_detect/src/lib.rs
+++ b/library/std_detect/src/lib.rs
@@ -15,7 +15,7 @@
 //! * `s390x`: [`is_s390x_feature_detected`]
 
 #![unstable(feature = "stdarch_internal", issue = "none")]
-#![feature(staged_api, doc_cfg, allow_internal_unstable)]
+#![feature(staged_api, cfg_select, doc_cfg, allow_internal_unstable)]
 #![deny(rust_2018_idioms)]
 #![allow(clippy::shadow_reuse)]
 #![cfg_attr(test, allow(unused_imports))]