about summary refs log tree commit diff
path: root/library/std/src/sys/sync
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/src/sys/sync
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/src/sys/sync')
-rw-r--r--library/std/src/sys/sync/condvar/mod.rs26
-rw-r--r--library/std/src/sys/sync/mutex/mod.rs29
-rw-r--r--library/std/src/sys/sync/once/mod.rs14
-rw-r--r--library/std/src/sys/sync/rwlock/mod.rs20
-rw-r--r--library/std/src/sys/sync/thread_parking/mod.rs26
5 files changed, 70 insertions, 45 deletions
diff --git a/library/std/src/sys/sync/condvar/mod.rs b/library/std/src/sys/sync/condvar/mod.rs
index d0c998a5597..cb67d273759 100644
--- a/library/std/src/sys/sync/condvar/mod.rs
+++ b/library/std/src/sys/sync/condvar/mod.rs
@@ -1,5 +1,5 @@
-cfg_if::cfg_if! {
-    if #[cfg(any(
+cfg_select! {
+    any(
         all(target_os = "windows", not(target_vendor="win7")),
         target_os = "linux",
         target_os = "android",
@@ -9,28 +9,34 @@ cfg_if::cfg_if! {
         target_os = "fuchsia",
         all(target_family = "wasm", target_feature = "atomics"),
         target_os = "hermit",
-    ))] {
+    ) => {
         mod futex;
         pub use futex::Condvar;
-    } else if #[cfg(any(
+    }
+    any(
         target_family = "unix",
         target_os = "teeos",
-    ))] {
+    ) => {
         mod pthread;
         pub use pthread::Condvar;
-    } else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
+    }
+    all(target_os = "windows", target_vendor = "win7") => {
         mod windows7;
         pub use windows7::Condvar;
-    } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
+    }
+    all(target_vendor = "fortanix", target_env = "sgx") => {
         mod sgx;
         pub use sgx::Condvar;
-    } else if #[cfg(target_os = "solid_asp3")] {
+    }
+    target_os = "solid_asp3" => {
         mod itron;
         pub use itron::Condvar;
-    } else if #[cfg(target_os = "xous")] {
+    }
+    target_os = "xous" => {
         mod xous;
         pub use xous::Condvar;
-    } else {
+    }
+    _ => {
         mod no_threads;
         pub use no_threads::Condvar;
     }
diff --git a/library/std/src/sys/sync/mutex/mod.rs b/library/std/src/sys/sync/mutex/mod.rs
index 360df3fc4b5..c885b0eabae 100644
--- a/library/std/src/sys/sync/mutex/mod.rs
+++ b/library/std/src/sys/sync/mutex/mod.rs
@@ -1,5 +1,5 @@
-cfg_if::cfg_if! {
-    if #[cfg(any(
+cfg_select! {
+    any(
         all(target_os = "windows", not(target_vendor = "win7")),
         target_os = "linux",
         target_os = "android",
@@ -8,31 +8,38 @@ cfg_if::cfg_if! {
         target_os = "dragonfly",
         all(target_family = "wasm", target_feature = "atomics"),
         target_os = "hermit",
-    ))] {
+    ) => {
         mod futex;
         pub use futex::Mutex;
-    } else if #[cfg(target_os = "fuchsia")] {
+    }
+    target_os = "fuchsia" => {
         mod fuchsia;
         pub use fuchsia::Mutex;
-    } else if #[cfg(any(
+    }
+    any(
         target_family = "unix",
         target_os = "teeos",
-    ))] {
+    ) => {
         mod pthread;
         pub use pthread::Mutex;
-    } else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
+    }
+    all(target_os = "windows", target_vendor = "win7") => {
         mod windows7;
         pub use windows7::{Mutex, raw};
-    } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
+    }
+    all(target_vendor = "fortanix", target_env = "sgx") => {
         mod sgx;
         pub use sgx::Mutex;
-    } else if #[cfg(target_os = "solid_asp3")] {
+    }
+    target_os = "solid_asp3" => {
         mod itron;
         pub use itron::Mutex;
-    } else if #[cfg(target_os = "xous")] {
+    }
+    target_os = "xous" => {
         mod xous;
         pub use xous::Mutex;
-    } else {
+    }
+    _ => {
         mod no_threads;
         pub use no_threads::Mutex;
     }
diff --git a/library/std/src/sys/sync/once/mod.rs b/library/std/src/sys/sync/once/mod.rs
index 0e38937b121..8adeb1f259d 100644
--- a/library/std/src/sys/sync/once/mod.rs
+++ b/library/std/src/sys/sync/once/mod.rs
@@ -7,8 +7,8 @@
 // This also gives us the opportunity to optimize the implementation a bit which
 // should help the fast path on call sites.
 
-cfg_if::cfg_if! {
-    if #[cfg(any(
+cfg_select! {
+    any(
         all(target_os = "windows", not(target_vendor="win7")),
         target_os = "linux",
         target_os = "android",
@@ -18,19 +18,21 @@ cfg_if::cfg_if! {
         target_os = "dragonfly",
         target_os = "fuchsia",
         target_os = "hermit",
-    ))] {
+    ) => {
         mod futex;
         pub use futex::{Once, OnceState};
-    } else if #[cfg(any(
+    }
+    any(
         windows,
         target_family = "unix",
         all(target_vendor = "fortanix", target_env = "sgx"),
         target_os = "solid_asp3",
         target_os = "xous",
-    ))] {
+    ) => {
         mod queue;
         pub use queue::{Once, OnceState};
-    } else {
+    }
+    _ => {
         mod no_threads;
         pub use no_threads::{Once, OnceState};
     }
diff --git a/library/std/src/sys/sync/rwlock/mod.rs b/library/std/src/sys/sync/rwlock/mod.rs
index 70ba6bf38ef..82f1dd18dee 100644
--- a/library/std/src/sys/sync/rwlock/mod.rs
+++ b/library/std/src/sys/sync/rwlock/mod.rs
@@ -1,5 +1,5 @@
-cfg_if::cfg_if! {
-    if #[cfg(any(
+cfg_select! {
+    any(
         all(target_os = "windows", not(target_vendor = "win7")),
         target_os = "linux",
         target_os = "android",
@@ -9,24 +9,28 @@ cfg_if::cfg_if! {
         target_os = "fuchsia",
         all(target_family = "wasm", target_feature = "atomics"),
         target_os = "hermit",
-    ))] {
+    ) => {
         mod futex;
         pub use futex::RwLock;
-    } else if #[cfg(any(
+    }
+    any(
         target_family = "unix",
         all(target_os = "windows", target_vendor = "win7"),
         all(target_vendor = "fortanix", target_env = "sgx"),
         target_os = "xous",
-    ))] {
+    ) => {
         mod queue;
         pub use queue::RwLock;
-    } else if #[cfg(target_os = "solid_asp3")] {
+    }
+    target_os = "solid_asp3" => {
         mod solid;
         pub use solid::RwLock;
-    } else if #[cfg(target_os = "teeos")] {
+    }
+    target_os = "teeos" => {
         mod teeos;
         pub use teeos::RwLock;
-    } else {
+    }
+    _ => {
         mod no_threads;
         pub use no_threads::RwLock;
     }
diff --git a/library/std/src/sys/sync/thread_parking/mod.rs b/library/std/src/sys/sync/thread_parking/mod.rs
index f4d8fa0a58c..b9fb27b4eef 100644
--- a/library/std/src/sys/sync/thread_parking/mod.rs
+++ b/library/std/src/sys/sync/thread_parking/mod.rs
@@ -1,5 +1,5 @@
-cfg_if::cfg_if! {
-    if #[cfg(any(
+cfg_select! {
+    any(
         all(target_os = "windows", not(target_vendor = "win7")),
         target_os = "linux",
         target_os = "android",
@@ -9,30 +9,36 @@ cfg_if::cfg_if! {
         target_os = "dragonfly",
         target_os = "fuchsia",
         target_os = "hermit",
-    ))] {
+    ) => {
         mod futex;
         pub use futex::Parker;
-    } else if #[cfg(any(
+    }
+    any(
         target_os = "netbsd",
         all(target_vendor = "fortanix", target_env = "sgx"),
         target_os = "solid_asp3",
-    ))] {
+    ) => {
         mod id;
         pub use id::Parker;
-    } else if #[cfg(target_vendor = "win7")] {
+    }
+    target_vendor = "win7" => {
         mod windows7;
         pub use windows7::Parker;
-    } else if #[cfg(all(target_vendor = "apple", not(miri)))] {
+    }
+    all(target_vendor = "apple", not(miri)) => {
         // Doesn't work in Miri, see <https://github.com/rust-lang/miri/issues/2589>.
         mod darwin;
         pub use darwin::Parker;
-    } else if #[cfg(target_os = "xous")] {
+    }
+    target_os = "xous" => {
         mod xous;
         pub use xous::Parker;
-    } else if #[cfg(target_family = "unix")] {
+    }
+    target_family = "unix" => {
         mod pthread;
         pub use pthread::Parker;
-    } else {
+    }
+    _ => {
         mod unsupported;
         pub use unsupported::Parker;
     }