Skip to content

Commit 9eb4a26

Browse files
committed
Auto merge of #145489 - joshtriplett:cfg-if-not, r=Amanieu
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.
2 parents aaf87a6 + c44c1bb commit 9eb4a26

File tree

67 files changed

+1009
-767
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1009
-767
lines changed

library/Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ name = "std_detect"
337337
version = "0.1.5"
338338
dependencies = [
339339
"alloc",
340-
"cfg-if",
341340
"core",
342341
"libc",
343342
]
@@ -376,7 +375,6 @@ dependencies = [
376375
name = "unwind"
377376
version = "0.0.0"
378377
dependencies = [
379-
"cfg-if",
380378
"libc",
381379
"rustc-std-workspace-core",
382380
"unwinding",

library/std/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ crate-type = ["dylib", "rlib"]
1414

1515
[dependencies]
1616
alloc = { path = "../alloc", public = true }
17+
# std no longer uses cfg-if directly, but the included copy of backtrace does.
1718
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
1819
panic_unwind = { path = "../panic_unwind", optional = true }
1920
panic_abort = { path = "../panic_abort" }

library/std/src/io/copy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ where
6363
R: Read,
6464
W: Write,
6565
{
66-
cfg_if::cfg_if! {
67-
if #[cfg(any(target_os = "linux", target_os = "android"))] {
66+
cfg_select! {
67+
any(target_os = "linux", target_os = "android") => {
6868
crate::sys::kernel_copy::copy_spec(reader, writer)
69-
} else {
69+
}
70+
_ => {
7071
generic_copy(reader, writer)
7172
}
7273
}

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@
330330
#![feature(bstr)]
331331
#![feature(bstr_internals)]
332332
#![feature(cast_maybe_uninit)]
333+
#![feature(cfg_select)]
333334
#![feature(char_internals)]
334335
#![feature(clone_to_uninit)]
335336
#![feature(const_cmp)]

library/std/src/os/unix/net/stream.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
cfg_if::cfg_if! {
2-
if #[cfg(any(
1+
cfg_select! {
2+
any(
33
target_os = "linux", target_os = "android",
44
target_os = "hurd",
55
target_os = "dragonfly", target_os = "freebsd",
66
target_os = "openbsd", target_os = "netbsd",
77
target_os = "solaris", target_os = "illumos",
88
target_os = "haiku", target_os = "nto",
9-
target_os = "cygwin"))] {
9+
target_os = "cygwin",
10+
) => {
1011
use libc::MSG_NOSIGNAL;
11-
} else {
12+
}
13+
_ => {
1214
const MSG_NOSIGNAL: core::ffi::c_int = 0x0;
1315
}
1416
}

library/std/src/os/unix/process.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44
55
#![stable(feature = "rust1", since = "1.0.0")]
66

7-
use cfg_if::cfg_if;
8-
97
use crate::ffi::OsStr;
108
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
119
use crate::path::Path;
1210
use crate::sealed::Sealed;
1311
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
1412
use crate::{io, process, sys};
1513

16-
cfg_if! {
17-
if #[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita"))] {
14+
cfg_select! {
15+
any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita") => {
1816
type UserId = u16;
1917
type GroupId = u16;
20-
} else if #[cfg(target_os = "nto")] {
18+
}
19+
target_os = "nto" => {
2120
// Both IDs are signed, see `sys/target_nto.h` of the QNX Neutrino SDP.
2221
// Only positive values should be used, see e.g.
2322
// https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/setuid.html
2423
type UserId = i32;
2524
type GroupId = i32;
26-
} else {
25+
}
26+
_ => {
2727
type UserId = u32;
2828
type GroupId = u32;
2929
}

library/std/src/sync/reentrant_lock.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use cfg_if::cfg_if;
2-
31
use crate::cell::UnsafeCell;
42
use crate::fmt;
53
use crate::ops::Deref;
@@ -87,8 +85,8 @@ pub struct ReentrantLock<T: ?Sized> {
8785
data: T,
8886
}
8987

90-
cfg_if!(
91-
if #[cfg(target_has_atomic = "64")] {
88+
cfg_select!(
89+
target_has_atomic = "64" => {
9290
use crate::sync::atomic::{Atomic, AtomicU64, Ordering::Relaxed};
9391

9492
struct Tid(Atomic<u64>);
@@ -110,7 +108,8 @@ cfg_if!(
110108
self.0.store(value, Relaxed);
111109
}
112110
}
113-
} else {
111+
}
112+
_ => {
114113
/// Returns the address of a TLS variable. This is guaranteed to
115114
/// be unique across all currently alive threads.
116115
fn tls_addr() -> usize {

library/std/src/sys/alloc/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,37 @@ unsafe fn realloc_fallback(
6868
}
6969
}
7070

71-
cfg_if::cfg_if! {
72-
if #[cfg(any(
71+
cfg_select! {
72+
any(
7373
target_family = "unix",
7474
target_os = "wasi",
7575
target_os = "teeos",
7676
target_os = "trusty",
77-
))] {
77+
) => {
7878
mod unix;
79-
} else if #[cfg(target_os = "windows")] {
79+
}
80+
target_os = "windows" => {
8081
mod windows;
81-
} else if #[cfg(target_os = "hermit")] {
82+
}
83+
target_os = "hermit" => {
8284
mod hermit;
83-
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
85+
}
86+
all(target_vendor = "fortanix", target_env = "sgx") => {
8487
mod sgx;
85-
} else if #[cfg(target_os = "solid_asp3")] {
88+
}
89+
target_os = "solid_asp3" => {
8690
mod solid;
87-
} else if #[cfg(target_os = "uefi")] {
91+
}
92+
target_os = "uefi" => {
8893
mod uefi;
89-
} else if #[cfg(target_family = "wasm")] {
94+
}
95+
target_family = "wasm" => {
9096
mod wasm;
91-
} else if #[cfg(target_os = "xous")] {
97+
}
98+
target_os = "xous" => {
9299
mod xous;
93-
} else if #[cfg(target_os = "zkvm")] {
100+
}
101+
target_os = "zkvm" => {
94102
mod zkvm;
95103
}
96104
}

library/std/src/sys/alloc/unix.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,16 @@ unsafe impl GlobalAlloc for System {
5858
}
5959
}
6060

61-
cfg_if::cfg_if! {
61+
cfg_select! {
6262
// We use posix_memalign wherever possible, but some targets have very incomplete POSIX coverage
6363
// so we need a fallback for those.
64-
if #[cfg(any(
65-
target_os = "horizon",
66-
target_os = "vita",
67-
))] {
64+
any(target_os = "horizon", target_os = "vita") => {
6865
#[inline]
6966
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
7067
unsafe { libc::memalign(layout.align(), layout.size()) as *mut u8 }
7168
}
72-
} else {
69+
}
70+
_ => {
7371
#[inline]
7472
#[cfg_attr(target_os = "vxworks", allow(unused_unsafe))]
7573
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {

library/std/src/sys/anonymous_pipe/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22

3-
cfg_if::cfg_if! {
4-
if #[cfg(unix)] {
3+
cfg_select! {
4+
unix => {
55
mod unix;
66
pub use unix::{AnonPipe, pipe};
7-
} else if #[cfg(windows)] {
7+
}
8+
windows => {
89
mod windows;
910
pub use windows::{AnonPipe, pipe};
10-
} else {
11+
}
12+
_ => {
1113
mod unsupported;
1214
pub use unsupported::{AnonPipe, pipe};
1315
}

0 commit comments

Comments
 (0)