diff --git a/Cargo.lock b/Cargo.lock index 61f9c130e38d7..f8bcf9aaf84a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5964,7 +5964,6 @@ dependencies = [ name = "unwind" version = "0.0.0" dependencies = [ - "cfg-if", "compiler_builtins", "core", "libc", diff --git a/library/core/src/ffi/mod.rs b/library/core/src/ffi/mod.rs index 44200926a32eb..20d26334d1365 100644 --- a/library/core/src/ffi/mod.rs +++ b/library/core/src/ffi/mod.rs @@ -71,9 +71,9 @@ pub type c_ptrdiff_t = isize; pub type c_ssize_t = isize; mod c_char_definition { - cfg_if! { + crate::cfg_match! { // These are the targets on which c_char is unsigned. - if #[cfg(any( + cfg(any( all( target_os = "linux", any( @@ -124,9 +124,10 @@ mod c_char_definition { ), all(target_os = "nto", target_arch = "aarch64"), target_os = "horizon" - ))] { + )) => { pub type c_char = u8; - } else { + } + _ => { // On every other target, c_char is signed. pub type c_char = i8; } @@ -134,11 +135,12 @@ mod c_char_definition { } mod c_int_definition { - cfg_if! { - if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] { + crate::cfg_match! { + cfg(any(target_arch = "avr", target_arch = "msp430")) => { pub type c_int = i16; pub type c_uint = u16; - } else { + } + _ => { pub type c_int = i32; pub type c_uint = u32; } @@ -146,11 +148,12 @@ mod c_int_definition { } mod c_long_definition { - cfg_if! { - if #[cfg(all(target_pointer_width = "64", not(windows)))] { + crate::cfg_match! { + cfg(all(target_pointer_width = "64", not(windows))) => { pub type c_long = i64; pub type c_ulong = u64; - } else { + } + _ =>{ // The minimal size of `long` in the C standard is 32 bits pub type c_long = i32; pub type c_ulong = u32; diff --git a/library/core/src/internal_macros.rs b/library/core/src/internal_macros.rs index bf53b2245ac59..238825e175520 100644 --- a/library/core/src/internal_macros.rs +++ b/library/core/src/internal_macros.rs @@ -120,80 +120,3 @@ macro_rules! impl_fn_for_zst { )+ } } - -/// A macro for defining `#[cfg]` if-else statements. -/// -/// `cfg_if` is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade -/// of `#[cfg]` cases, emitting the implementation which matches first. -/// -/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code without having to -/// rewrite each clause multiple times. -/// -/// # Example -/// -/// ```ignore(cannot-test-this-because-non-exported-macro) -/// cfg_if! { -/// if #[cfg(unix)] { -/// fn foo() { /* unix specific functionality */ } -/// } else if #[cfg(target_pointer_width = "32")] { -/// fn foo() { /* non-unix, 32-bit functionality */ } -/// } else { -/// fn foo() { /* fallback implementation */ } -/// } -/// } -/// -/// # fn main() {} -/// ``` -// This is a copy of `cfg_if!` from the `cfg_if` crate. -// The recursive invocations should use $crate if this is ever exported. -macro_rules! cfg_if { - // match if/else chains with a final `else` - ( - $( - if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* } - ) else+ - else { $( $e_tokens:tt )* } - ) => { - cfg_if! { - @__items () ; - $( - (( $i_meta ) ( $( $i_tokens )* )) , - )+ - (() ( $( $e_tokens )* )) , - } - }; - - // Internal and recursive macro to emit all the items - // - // Collects all the previous cfgs in a list at the beginning, so they can be - // negated. After the semicolon is all the remaining items. - (@__items ( $( $_:meta , )* ) ; ) => {}; - ( - @__items ( $( $no:meta , )* ) ; - (( $( $yes:meta )? ) ( $( $tokens:tt )* )) , - $( $rest:tt , )* - ) => { - // Emit all items within one block, applying an appropriate #[cfg]. The - // #[cfg] will require all `$yes` matchers specified and must also negate - // all previous matchers. - #[cfg(all( - $( $yes , )? - not(any( $( $no ),* )) - ))] - cfg_if! { @__identity $( $tokens )* } - - // Recurse to emit all other items in `$rest`, and when we do so add all - // our `$yes` matchers to the list of `$no` matchers as future emissions - // will have to negate everything we just matched as well. - cfg_if! { - @__items ( $( $no , )* $( $yes , )? ) ; - $( $rest , )* - } - }; - - // Internal macro to make __apply work out right for different match types, - // because of how macros match/expand stuff. - (@__identity $( $tokens:tt )* ) => { - $( $tokens )* - }; -} diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index b54680a61b4d8..1ead126b83b04 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -111,6 +111,7 @@ // // Library features: // tidy-alphabetical-start +#![feature(cfg_match)] #![feature(char_indices_offset)] #![feature(const_align_of_val)] #![feature(const_align_of_val_raw)] diff --git a/library/panic_abort/src/lib.rs b/library/panic_abort/src/lib.rs index c44f23eea8094..f1606edc1407d 100644 --- a/library/panic_abort/src/lib.rs +++ b/library/panic_abort/src/lib.rs @@ -8,6 +8,7 @@ #![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")] #![panic_runtime] #![allow(unused_features)] +#![feature(cfg_match)] #![feature(core_intrinsics)] #![feature(panic_runtime)] #![feature(std_internals)] @@ -42,16 +43,17 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 { abort(); - cfg_if::cfg_if! { - if #[cfg(any(unix, target_os = "solid_asp3"))] { + cfg_match! { + cfg(any(unix, target_os = "solid_asp3")) => { unsafe fn abort() -> ! { libc::abort(); } - } else if #[cfg(any(target_os = "hermit", - all(target_vendor = "fortanix", target_env = "sgx"), - target_os = "xous", - target_os = "uefi", - ))] { + } + cfg(any(target_os = "hermit", + all(target_vendor = "fortanix", target_env = "sgx"), + target_os = "xous", + target_os = "uefi", + )) => { unsafe fn abort() -> ! { // call std::sys::abort_internal extern "C" { @@ -59,7 +61,8 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 { } __rust_abort(); } - } else if #[cfg(all(windows, not(miri)))] { + } + cfg(all(windows, not(miri))) => { // On Windows, use the processor-specific __fastfail mechanism. In Windows 8 // and later, this will terminate the process immediately without running any // in-process exception handlers. In earlier versions of Windows, this @@ -86,7 +89,8 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 { } core::intrinsics::unreachable(); } - } else if #[cfg(target_os = "teeos")] { + } + cfg(target_os = "teeos") => { mod teeos { extern "C" { pub fn TEE_Panic(code: u32) -> !; @@ -96,7 +100,8 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 { unsafe fn abort() -> ! { teeos::TEE_Panic(1); } - } else { + } + _ => { unsafe fn abort() -> ! { core::intrinsics::abort(); } diff --git a/library/panic_unwind/src/lib.rs b/library/panic_unwind/src/lib.rs index 7a0bae34642d6..1c72bbe594e33 100644 --- a/library/panic_unwind/src/lib.rs +++ b/library/panic_unwind/src/lib.rs @@ -14,6 +14,7 @@ #![no_std] #![unstable(feature = "panic_unwind", issue = "32837")] #![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")] +#![feature(cfg_match)] #![feature(core_intrinsics)] #![feature(lang_items)] #![feature(panic_unwind)] @@ -31,32 +32,37 @@ use alloc::boxed::Box; use core::any::Any; use core::panic::PanicPayload; -cfg_if::cfg_if! { - if #[cfg(target_os = "emscripten")] { +cfg_match! { + cfg(target_os = "emscripten") => { #[path = "emcc.rs"] mod real_imp; - } else if #[cfg(target_os = "hermit")] { + } + cfg(target_os = "hermit") => { #[path = "hermit.rs"] mod real_imp; - } else if #[cfg(target_os = "l4re")] { + } + cfg(target_os = "l4re") => { // L4Re is unix family but does not yet support unwinding. #[path = "dummy.rs"] mod real_imp; - } else if #[cfg(all(target_env = "msvc", not(target_arch = "arm")))] { + } + cfg(all(target_env = "msvc", not(target_arch = "arm"))) => { // LLVM does not support unwinding on 32 bit ARM msvc (thumbv7a-pc-windows-msvc) #[path = "seh.rs"] mod real_imp; - } else if #[cfg(any( + } + cfg(any( all(target_family = "windows", target_env = "gnu"), target_os = "psp", target_os = "xous", target_os = "solid_asp3", all(target_family = "unix", not(target_os = "espidf")), all(target_vendor = "fortanix", target_env = "sgx"), - ))] { + )) => { #[path = "gcc.rs"] mod real_imp; - } else { + } + _ => { // Targets that don't support unwinding. // - family=wasm // - os=none ("bare metal" targets) @@ -69,14 +75,15 @@ cfg_if::cfg_if! { } } -cfg_if::cfg_if! { - if #[cfg(miri)] { +cfg_match! { + cfg(miri) => { // Use the Miri runtime. // We still need to also load the normal runtime above, as rustc expects certain lang // items from there to be defined. #[path = "miri.rs"] mod imp; - } else { + } + _ => { // Use the real runtime. use real_imp as imp; } diff --git a/library/panic_unwind/src/seh.rs b/library/panic_unwind/src/seh.rs index c4f4d2fbec915..80f57793b27b6 100644 --- a/library/panic_unwind/src/seh.rs +++ b/library/panic_unwind/src/seh.rs @@ -253,10 +253,11 @@ macro_rules! define_cleanup { } } } -cfg_if::cfg_if! { - if #[cfg(target_arch = "x86")] { +cfg_match! { + cfg(target_arch = "x86") => { define_cleanup!("thiscall" "thiscall-unwind"); - } else { + } + _ => { define_cleanup!("C" "C-unwind"); } } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index c6cd2c6786ad6..d65458c8ba689 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -314,6 +314,7 @@ // // Library features (core): // tidy-alphabetical-start +#![feature(cfg_match)] #![feature(char_internals)] #![feature(core_intrinsics)] #![feature(core_io_borrowed_buf)] diff --git a/library/std/src/os/unix/process.rs b/library/std/src/os/unix/process.rs index e45457b2e42b4..69e7853ca5c29 100644 --- a/library/std/src/os/unix/process.rs +++ b/library/std/src/os/unix/process.rs @@ -12,19 +12,19 @@ use crate::sealed::Sealed; use crate::sys; use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; -use cfg_if::cfg_if; - -cfg_if! { - if #[cfg(any(target_os = "vxworks", target_os = "espidf", target_os = "horizon", target_os = "vita"))] { +cfg_match! { + cfg(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")] { + } + cfg(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; } diff --git a/library/std/src/sys/pal/common/thread_local/mod.rs b/library/std/src/sys/pal/common/thread_local/mod.rs index 8b2c839f837d4..b33ac8618d26f 100644 --- a/library/std/src/sys/pal/common/thread_local/mod.rs +++ b/library/std/src/sys/pal/common/thread_local/mod.rs @@ -5,18 +5,20 @@ // "fast" key type is accessed via code generated via LLVM, where TLS keys are set up by the linker. // "static" is for single-threaded platforms where a global static is sufficient. -cfg_if::cfg_if! { - if #[cfg(any(all(target_family = "wasm", not(target_feature = "atomics")), target_os = "uefi"))] { +cfg_match! { + cfg(any(all(target_family = "wasm", not(target_feature = "atomics")), target_os = "uefi")) => { #[doc(hidden)] mod static_local; #[doc(hidden)] pub use static_local::{Key, thread_local_inner}; - } else if #[cfg(target_thread_local)] { + } + cfg(target_thread_local) => { #[doc(hidden)] mod fast_local; #[doc(hidden)] pub use fast_local::{Key, thread_local_inner}; - } else { + } + _ => { #[doc(hidden)] mod os_local; #[doc(hidden)] diff --git a/library/std/src/sys/pal/unix/alloc.rs b/library/std/src/sys/pal/unix/alloc.rs index af0089978ecbb..c289115891446 100644 --- a/library/std/src/sys/pal/unix/alloc.rs +++ b/library/std/src/sys/pal/unix/alloc.rs @@ -52,8 +52,8 @@ unsafe impl GlobalAlloc for System { } } -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "android", target_os = "illumos", target_os = "redox", @@ -61,7 +61,7 @@ cfg_if::cfg_if! { target_os = "espidf", target_os = "horizon", target_os = "vita", - ))] { + )) => { #[inline] unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { // On android we currently target API level 9 which unfortunately @@ -83,7 +83,8 @@ cfg_if::cfg_if! { // /memory/aligned_memory.cc libc::memalign(layout.align(), layout.size()) as *mut u8 } - } else if #[cfg(target_os = "wasi")] { + } + cfg(target_os = "wasi") => { #[inline] unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { // C11 aligned_alloc requires that the size be a multiple of the alignment. @@ -92,7 +93,8 @@ cfg_if::cfg_if! { let size = layout.size().next_multiple_of(align); libc::aligned_alloc(align, size) as *mut u8 } - } else { + } + _ => { #[inline] unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { let mut out = ptr::null_mut(); diff --git a/library/std/src/sys/pal/unix/locks/mod.rs b/library/std/src/sys/pal/unix/locks/mod.rs index a49247310b54c..eb07601e5541e 100644 --- a/library/std/src/sys/pal/unix/locks/mod.rs +++ b/library/std/src/sys/pal/unix/locks/mod.rs @@ -1,26 +1,28 @@ -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "linux", target_os = "android", all(target_os = "emscripten", target_feature = "atomics"), target_os = "freebsd", target_os = "openbsd", target_os = "dragonfly", - ))] { + )) => { mod futex_mutex; mod futex_rwlock; mod futex_condvar; pub(crate) use futex_mutex::Mutex; pub(crate) use futex_rwlock::RwLock; pub(crate) use futex_condvar::Condvar; - } else if #[cfg(target_os = "fuchsia")] { + } + cfg(target_os = "fuchsia") => { mod fuchsia_mutex; mod futex_rwlock; mod futex_condvar; pub(crate) use fuchsia_mutex::Mutex; pub(crate) use futex_rwlock::RwLock; pub(crate) use futex_condvar::Condvar; - } else { + } + _ => { mod pthread_mutex; mod pthread_condvar; mod queue_rwlock; diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs index 976a437c17ff9..efd876ee894d2 100644 --- a/library/std/src/sys/pal/unix/mod.rs +++ b/library/std/src/sys/pal/unix/mod.rs @@ -371,31 +371,36 @@ pub fn abort_internal() -> ! { unsafe { libc::abort() } } -cfg_if::cfg_if! { - if #[cfg(target_os = "android")] { +cfg_match! { + cfg(target_os = "android") => { #[link(name = "dl", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))] #[link(name = "dl", cfg(not(target_feature = "crt-static")))] #[link(name = "log", cfg(not(target_feature = "crt-static")))] extern "C" {} - } else if #[cfg(target_os = "freebsd")] { + } + cfg(target_os = "freebsd") => { #[link(name = "execinfo")] #[link(name = "pthread")] extern "C" {} - } else if #[cfg(target_os = "netbsd")] { + } + cfg(target_os = "netbsd") => { #[link(name = "pthread")] #[link(name = "rt")] extern "C" {} - } else if #[cfg(any(target_os = "dragonfly", target_os = "openbsd"))] { + } + cfg(any(target_os = "dragonfly", target_os = "openbsd")) => { #[link(name = "pthread")] extern "C" {} - } else if #[cfg(target_os = "solaris")] { + } + cfg(target_os = "solaris") => { #[link(name = "socket")] #[link(name = "posix4")] #[link(name = "pthread")] #[link(name = "resolv")] extern "C" {} - } else if #[cfg(target_os = "illumos")] { + } + cfg(target_os = "illumos") => { #[link(name = "socket")] #[link(name = "posix4")] #[link(name = "pthread")] @@ -404,22 +409,27 @@ cfg_if::cfg_if! { // Use libumem for the (malloc-compatible) allocator #[link(name = "umem")] extern "C" {} - } else if #[cfg(target_os = "macos")] { + } + cfg(target_os = "macos") => { #[link(name = "System")] extern "C" {} - } else if #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos"))] { + } + cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos")) => { #[link(name = "System")] #[link(name = "objc")] #[link(name = "Foundation", kind = "framework")] extern "C" {} - } else if #[cfg(target_os = "fuchsia")] { + } + cfg(target_os = "fuchsia") => { #[link(name = "zircon")] #[link(name = "fdio")] extern "C" {} - } else if #[cfg(all(target_os = "linux", target_env = "uclibc"))] { + } + cfg(all(target_os = "linux", target_env = "uclibc")) => { #[link(name = "dl")] extern "C" {} - } else if #[cfg(target_os = "vita")] { + } + cfg(target_os = "vita") => { #[link(name = "pthread", kind = "static", modifiers = "-bundle")] extern "C" {} } diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index af2b9db46854c..eb980d880f07e 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -31,10 +31,11 @@ use libc::{c_char, c_int, c_void}; const TMPBUF_SZ: usize = 128; -cfg_if::cfg_if! { - if #[cfg(target_os = "redox")] { +cfg_match! { + cfg(target_os = "redox") => { const PATH_SEPARATOR: u8 = b';'; - } else { + } + _ => { const PATH_SEPARATOR: u8 = b':'; } } diff --git a/library/std/src/sys/pal/unix/process/mod.rs b/library/std/src/sys/pal/unix/process/mod.rs index 074f0a105e329..c7457e7b2c2b0 100644 --- a/library/std/src/sys/pal/unix/process/mod.rs +++ b/library/std/src/sys/pal/unix/process/mod.rs @@ -8,19 +8,22 @@ mod process_common; #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))] mod process_unsupported; -cfg_if::cfg_if! { - if #[cfg(target_os = "fuchsia")] { +cfg_match! { + cfg(target_os = "fuchsia") => { #[path = "process_fuchsia.rs"] mod process_inner; mod zircon; - } else if #[cfg(target_os = "vxworks")] { + } + cfg(target_os = "vxworks") => { #[path = "process_vxworks.rs"] mod process_inner; - } else if #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))] { + } + cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita")) => { mod process_inner { pub use super::process_unsupported::*; } - } else { + } + _ => { #[path = "process_unix.rs"] mod process_inner; } diff --git a/library/std/src/sys/pal/unix/process/process_common.rs b/library/std/src/sys/pal/unix/process/process_common.rs index f615e8086dccf..9fcc9df2e8102 100644 --- a/library/std/src/sys/pal/unix/process/process_common.rs +++ b/library/std/src/sys/pal/unix/process/process_common.rs @@ -20,14 +20,17 @@ use crate::sys::fs::OpenOptions; use libc::{c_char, c_int, gid_t, pid_t, uid_t, EXIT_FAILURE, EXIT_SUCCESS}; -cfg_if::cfg_if! { - if #[cfg(target_os = "fuchsia")] { +cfg_match! { + cfg(target_os = "fuchsia") => { // fuchsia doesn't have /dev/null - } else if #[cfg(target_os = "redox")] { + } + cfg(target_os = "redox") => { const DEV_NULL: &CStr = c"null:"; - } else if #[cfg(target_os = "vxworks")] { + } + cfg(target_os = "vxworks") => { const DEV_NULL: &CStr = c"/null"; - } else { + } + _ => { const DEV_NULL: &CStr = c"/dev/null"; } } @@ -37,8 +40,8 @@ cfg_if::cfg_if! { // to support older Android version (independent of libc version). // The following implementations are based on // https://github.com/aosp-mirror/platform_bionic/blob/ad8dcd6023294b646e5a8288c0ed431b0845da49/libc/include/android/legacy_signal_inlines.h -cfg_if::cfg_if! { - if #[cfg(target_os = "android")] { +cfg_match! { + cfg(target_os = "android") => { #[allow(dead_code)] pub unsafe fn sigemptyset(set: *mut libc::sigset_t) -> libc::c_int { set.write_bytes(0u8, 1); @@ -74,8 +77,8 @@ cfg_if::cfg_if! { raw[bit / LONG_BIT] |= 1 << (bit % LONG_BIT); return 0; } - } else { - #[allow(unused_imports)] + } + _ => { pub use libc::{sigemptyset, sigaddset}; } } diff --git a/library/std/src/sys/pal/unix/process/process_unix.rs b/library/std/src/sys/pal/unix/process/process_unix.rs index d5a77085725c5..9207dc5a0298f 100644 --- a/library/std/src/sys/pal/unix/process/process_unix.rs +++ b/library/std/src/sys/pal/unix/process/process_unix.rs @@ -36,9 +36,8 @@ use libc::{c_int, pid_t}; )))] use libc::{gid_t, uid_t}; -cfg_if::cfg_if! { - if #[cfg(all(target_os = "nto", target_env = "nto71"))] { - use crate::thread; +cfg_match! { + cfg(all(target_os = "nto", target_env = "nto71")) => { use libc::{c_char, posix_spawn_file_actions_t, posix_spawnattr_t}; use crate::time::Duration; use crate::sync::LazyLock; diff --git a/library/std/src/sys/pal/unix/thread_parking/mod.rs b/library/std/src/sys/pal/unix/thread_parking/mod.rs index 185333c072f49..b3c9b69d1a87f 100644 --- a/library/std/src/sys/pal/unix/thread_parking/mod.rs +++ b/library/std/src/sys/pal/unix/thread_parking/mod.rs @@ -10,8 +10,8 @@ target_os = "fuchsia", )))] -cfg_if::cfg_if! { - if #[cfg(all( +cfg_match! { + cfg(all( any( target_os = "macos", target_os = "ios", @@ -19,13 +19,15 @@ cfg_if::cfg_if! { target_os = "tvos", ), not(miri), - ))] { + )) => { mod darwin; pub use darwin::Parker; - } else if #[cfg(target_os = "netbsd")] { + } + cfg(target_os = "netbsd") => { mod netbsd; pub use netbsd::{current, park, park_timeout, unpark, ThreadId}; - } else { + } + _ => { mod pthread; pub use pthread::Parker; } diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs index 116878ee99681..6b737332f1f93 100644 --- a/library/std/src/sys/pal/wasi/mod.rs +++ b/library/std/src/sys/pal/wasi/mod.rs @@ -42,8 +42,8 @@ pub mod thread_local_dtor; pub mod thread_local_key; pub mod time; -cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { +cfg_match! { + cfg(target_feature = "atomics") => { #[path = "../unix/locks"] pub mod locks { #![allow(unsafe_op_in_unsafe_fn)] @@ -54,7 +54,8 @@ cfg_if::cfg_if! { pub(crate) use futex_mutex::Mutex; pub(crate) use futex_rwlock::RwLock; } - } else { + } + _ => { #[path = "../unsupported/locks/mod.rs"] pub mod locks; #[path = "../unsupported/once.rs"] diff --git a/library/std/src/sys/pal/wasi/os.rs b/library/std/src/sys/pal/wasi/os.rs index d62ff8a2f1822..c6e9a6a97535d 100644 --- a/library/std/src/sys/pal/wasi/os.rs +++ b/library/std/src/sys/pal/wasi/os.rs @@ -25,8 +25,8 @@ mod libc { } } -cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { +cfg_match! { + cfg(target_feature = "atomics") => { // Access to the environment must be protected by a lock in multi-threaded scenarios. use crate::sync::{PoisonError, RwLock}; static ENV_LOCK: RwLock<()> = RwLock::new(()); @@ -36,7 +36,8 @@ cfg_if::cfg_if! { pub fn env_write_lock() -> impl Drop { ENV_LOCK.write().unwrap_or_else(PoisonError::into_inner) } - } else { + } + _ => { // No need for a lock if we are single-threaded. pub fn env_read_lock() -> impl Drop { Box::new(()) diff --git a/library/std/src/sys/pal/wasi/thread.rs b/library/std/src/sys/pal/wasi/thread.rs index 77d8b4378e7d5..342d81b03e5f4 100644 --- a/library/std/src/sys/pal/wasi/thread.rs +++ b/library/std/src/sys/pal/wasi/thread.rs @@ -5,8 +5,8 @@ use crate::num::NonZero; use crate::sys::unsupported; use crate::time::Duration; -cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { +cfg_match! { + cfg(target_feature = "atomics") => { use crate::cmp; use crate::ptr; use crate::sys::os; @@ -61,7 +61,8 @@ cfg_if::cfg_if! { debug_assert_eq!(ret, 0); } } - } else { + } + _ => { pub struct Thread(!); } } @@ -70,8 +71,8 @@ pub const DEFAULT_MIN_STACK_SIZE: usize = 4096; impl Thread { // unsafe: see thread::Builder::spawn_unchecked for safety requirements - cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { + cfg_match! { + cfg(target_feature = "atomics") => { pub unsafe fn new(stack: usize, p: Box) -> io::Result { let p = Box::into_raw(Box::new(p)); let mut native: libc::pthread_t = mem::zeroed(); @@ -118,7 +119,8 @@ impl Thread { ptr::null_mut() } } - } else { + } + _ => { pub unsafe fn new(_stack: usize, _p: Box) -> io::Result { unsupported() } diff --git a/library/std/src/sys/pal/wasm/mod.rs b/library/std/src/sys/pal/wasm/mod.rs index 567555118d707..f722744714a8e 100644 --- a/library/std/src/sys/pal/wasm/mod.rs +++ b/library/std/src/sys/pal/wasm/mod.rs @@ -41,8 +41,8 @@ pub mod thread_local_key; #[path = "../unsupported/time.rs"] pub mod time; -cfg_if::cfg_if! { - if #[cfg(target_feature = "atomics")] { +cfg_match! { + cfg(target_feature = "atomics") => { #[path = "../unix/locks"] pub mod locks { #![allow(unsafe_op_in_unsafe_fn)] @@ -57,7 +57,8 @@ cfg_if::cfg_if! { pub mod futex; #[path = "atomics/thread.rs"] pub mod thread; - } else { + } + _ => { #[path = "../unsupported/locks/mod.rs"] pub mod locks; #[path = "../unsupported/once.rs"] diff --git a/library/std/src/sys/pal/windows/c.rs b/library/std/src/sys/pal/windows/c.rs index 1a59ac9a9cadf..cd8a7d72e9e18 100644 --- a/library/std/src/sys/pal/windows/c.rs +++ b/library/std/src/sys/pal/windows/c.rs @@ -196,11 +196,8 @@ pub struct in6_addr { } // Desktop specific functions & types -cfg_if::cfg_if! { -if #[cfg(not(target_vendor = "uwp"))] { - pub const EXCEPTION_CONTINUE_SEARCH: i32 = 0; -} -} +#[cfg(not(target_vendor = "uwp"))] +pub const EXCEPTION_CONTINUE_SEARCH: i32 = 0; pub unsafe extern "system" fn WriteFileEx( hFile: BorrowedHandle<'_>, @@ -270,8 +267,8 @@ pub unsafe fn getaddrinfo( windows_sys::getaddrinfo(node.cast::(), service.cast::(), hints, res) } -cfg_if::cfg_if! { -if #[cfg(not(target_vendor = "uwp"))] { +cfg_match! { +cfg(not(target_vendor = "uwp")) => { pub unsafe fn NtReadFile( filehandle: BorrowedHandle<'_>, event: HANDLE, @@ -442,26 +439,27 @@ compat_fn_with_fallback! { // are not included in the win32 metadata. We work around that by defining them here. // // Where possible, these definitions should be kept in sync with https://docs.rs/windows-sys -cfg_if::cfg_if! { -if #[cfg(not(target_vendor = "uwp"))] { - #[link(name = "kernel32")] - extern "system" { - pub fn AddVectoredExceptionHandler( - first: u32, - handler: PVECTORED_EXCEPTION_HANDLER, - ) -> *mut c_void; - } - pub type PVECTORED_EXCEPTION_HANDLER = Option< - unsafe extern "system" fn(exceptioninfo: *mut EXCEPTION_POINTERS) -> i32, - >; - #[repr(C)] - pub struct EXCEPTION_POINTERS { - pub ExceptionRecord: *mut EXCEPTION_RECORD, - pub ContextRecord: *mut CONTEXT, +cfg_match! { + cfg(not(target_vendor = "uwp")) => { + #[link(name = "kernel32")] + extern "system" { + pub fn AddVectoredExceptionHandler( + first: u32, + handler: PVECTORED_EXCEPTION_HANDLER, + ) -> *mut c_void; + } + pub type PVECTORED_EXCEPTION_HANDLER = Option< + unsafe extern "system" fn(exceptioninfo: *mut EXCEPTION_POINTERS) -> i32, + >; + #[repr(C)] + pub struct EXCEPTION_POINTERS { + pub ExceptionRecord: *mut EXCEPTION_RECORD, + pub ContextRecord: *mut CONTEXT, + } + #[cfg(target_arch = "arm")] + pub enum CONTEXT {} } - #[cfg(target_arch = "arm")] - pub enum CONTEXT {} -}} +} #[link(name = "ws2_32")] extern "system" { diff --git a/library/std/src/sys/pal/windows/mod.rs b/library/std/src/sys/pal/windows/mod.rs index 726a4509f280f..6ddb87334524b 100644 --- a/library/std/src/sys/pal/windows/mod.rs +++ b/library/std/src/sys/pal/windows/mod.rs @@ -32,10 +32,11 @@ pub mod thread_local_dtor; pub mod thread_local_key; pub mod thread_parking; pub mod time; -cfg_if::cfg_if! { - if #[cfg(not(target_vendor = "uwp"))] { +cfg_match! { + cfg(not(target_vendor = "uwp")) => { pub mod stack_overflow; - } else { + } + _ => { pub mod stack_overflow_uwp; pub use self::stack_overflow_uwp as stack_overflow; } diff --git a/library/std/src/sys/personality/gcc.rs b/library/std/src/sys/personality/gcc.rs index 6f317131145ae..49e85c2623e12 100644 --- a/library/std/src/sys/personality/gcc.rs +++ b/library/std/src/sys/personality/gcc.rs @@ -91,8 +91,8 @@ const UNWIND_DATA_REG: (i32, i32) = (4, 5); // a0, a1 // https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/libsupc++/eh_personality.cc // https://github.com/gcc-mirror/gcc/blob/trunk/libgcc/unwind-c.c -cfg_if::cfg_if! { - if #[cfg(all(target_arch = "arm", not(target_os = "ios"), not(target_os = "tvos"), not(target_os = "watchos"), not(target_os = "netbsd")))] { +cfg_match! { + cfg(all(target_arch = "arm", not(target_os = "ios"), not(target_os = "tvos"), not(target_os = "watchos"), not(target_os = "netbsd"))) => { // ARM EHABI personality routine. // https://web.archive.org/web/20190728160938/https://infocenter.arm.com/help/topic/com.arm.doc.ihi0038b/IHI0038B_ehabi.pdf // @@ -189,7 +189,8 @@ cfg_if::cfg_if! { ) -> uw::_Unwind_Reason_Code; } } - } else { + } + _ => { // Default personality routine, which is used directly on most targets // and indirectly on Windows x86_64 via SEH. unsafe extern "C" fn rust_eh_personality_impl( @@ -232,8 +233,8 @@ cfg_if::cfg_if! { } } - cfg_if::cfg_if! { - if #[cfg(all(windows, any(target_arch = "aarch64", target_arch = "x86_64"), target_env = "gnu"))] { + cfg_match! { + cfg(all(windows, any(target_arch = "aarch64", target_arch = "x86_64"), target_env = "gnu")) => { // On x86_64 MinGW targets, the unwinding mechanism is SEH however the unwind // handler data (aka LSDA) uses GCC-compatible encoding. #[lang = "eh_personality"] @@ -252,7 +253,8 @@ cfg_if::cfg_if! { rust_eh_personality_impl, ) } - } else { + } + _ => { // The personality routine for most of our targets. #[lang = "eh_personality"] unsafe extern "C" fn rust_eh_personality( diff --git a/library/std/src/sys/personality/mod.rs b/library/std/src/sys/personality/mod.rs index d37b8ce634654..6bfb67307f43b 100644 --- a/library/std/src/sys/personality/mod.rs +++ b/library/std/src/sys/personality/mod.rs @@ -13,10 +13,11 @@ mod dwarf; #[cfg(not(any(test, doctest)))] -cfg_if::cfg_if! { - if #[cfg(target_os = "emscripten")] { +cfg_match! { + cfg(target_os = "emscripten") => { mod emcc; - } else if #[cfg(target_env = "msvc")] { + } + cfg(target_env = "msvc") => { // This is required by the compiler to exist (e.g., it's a lang item), // but it's never actually called by the compiler because // _CxxFrameHandler3 is the personality function that is always used. @@ -25,16 +26,18 @@ cfg_if::cfg_if! { fn rust_eh_personality() { core::intrinsics::abort() } - } else if #[cfg(any( + } + cfg(any( all(target_family = "windows", target_env = "gnu"), target_os = "psp", target_os = "xous", target_os = "solid_asp3", all(target_family = "unix", not(target_os = "espidf"), not(target_os = "l4re")), all(target_vendor = "fortanix", target_env = "sgx"), - ))] { + )) => { mod gcc; - } else { + } + _ => { // Targets that don't support unwinding. // - family=wasm // - os=none ("bare metal" targets) diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs index 01f83ecb41452..198998ad112c4 100644 --- a/library/std/src/sys_common/mod.rs +++ b/library/std/src/sys_common/mod.rs @@ -34,23 +34,25 @@ pub mod thread_parking; pub mod wstr; pub mod wtf8; -cfg_if::cfg_if! { - if #[cfg(target_os = "windows")] { +cfg_match! { + cfg(target_os = "windows") => { pub use crate::sys::thread_local_key; - } else { + } + _ => { pub mod thread_local_key; } } -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( all(unix, not(target_os = "l4re")), windows, target_os = "hermit", target_os = "solid_asp3" - ))] { + )) => { pub mod net; - } else { + } + _ => { pub use crate::sys::net; } } diff --git a/library/std/src/sys_common/net.rs b/library/std/src/sys_common/net.rs index de7d31baaaf8c..218f1593dc296 100644 --- a/library/std/src/sys_common/net.rs +++ b/library/std/src/sys_common/net.rs @@ -15,42 +15,46 @@ use crate::time::Duration; use crate::ffi::{c_int, c_void}; -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "tvos", target_os = "macos", target_os = "watchos", target_os = "openbsd", target_os = "netbsd", target_os = "illumos", - target_os = "solaris", target_os = "haiku", target_os = "l4re", target_os = "nto"))] { + target_os = "solaris", target_os = "haiku", target_os = "l4re", target_os = "nto")) => { use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP; use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP; - } else { + } + _ => { use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP; use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP; } } -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "linux", target_os = "android", target_os = "hurd", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", - target_os = "haiku", target_os = "nto"))] { + target_os = "haiku", target_os = "nto") + ) => { use libc::MSG_NOSIGNAL; - } else { + } + _ => { const MSG_NOSIGNAL: c_int = 0x0; } } -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "solaris", target_os = "illumos", - target_os = "nto"))] { + target_os = "nto")) => { use crate::ffi::c_uchar; type IpV4MultiCastType = c_uchar; - } else { + } + _ => { type IpV4MultiCastType = c_int; } } diff --git a/library/std/src/sys_common/once/mod.rs b/library/std/src/sys_common/once/mod.rs index ec57568c54c4f..8702ba6c876ef 100644 --- a/library/std/src/sys_common/once/mod.rs +++ b/library/std/src/sys_common/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_match! { + cfg(any( target_os = "linux", target_os = "android", all(target_arch = "wasm32", target_feature = "atomics"), @@ -17,19 +17,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( + } + cfg(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 { + } + _ => { pub use crate::sys::once::{Once, OnceState}; } } diff --git a/library/std/src/sys_common/thread_parking/mod.rs b/library/std/src/sys_common/thread_parking/mod.rs index c4d3f9ea2f427..ea3c4cfbab2cc 100644 --- a/library/std/src/sys_common/thread_parking/mod.rs +++ b/library/std/src/sys_common/thread_parking/mod.rs @@ -1,5 +1,5 @@ -cfg_if::cfg_if! { - if #[cfg(any( +cfg_match! { + cfg(any( target_os = "linux", target_os = "android", all(target_arch = "wasm32", target_feature = "atomics"), @@ -8,17 +8,19 @@ cfg_if::cfg_if! { target_os = "dragonfly", target_os = "fuchsia", target_os = "hermit", - ))] { + )) => { mod futex; pub use futex::Parker; - } else if #[cfg(any( + } + cfg(any( target_os = "netbsd", all(target_vendor = "fortanix", target_env = "sgx"), target_os = "solid_asp3", - ))] { + )) => { mod id; pub use id::Parker; - } else { + } + _ => { pub use crate::sys::thread_parking::Parker; } } diff --git a/library/unwind/Cargo.toml b/library/unwind/Cargo.toml index b7418d1189c77..3157e525ee5c3 100644 --- a/library/unwind/Cargo.toml +++ b/library/unwind/Cargo.toml @@ -17,7 +17,6 @@ doc = false core = { path = "../core" } libc = { version = "0.2.140", features = ['rustc-dep-of-std'], default-features = false } compiler_builtins = "0.1.0" -cfg-if = "1.0" [target.'cfg(target_os = "xous")'.dependencies] unwinding = { version = "0.2.1", features = ['rustc-dep-of-std', 'unwinder', 'fde-custom'], default-features = false } diff --git a/library/unwind/src/lib.rs b/library/unwind/src/lib.rs index f5988a4df1364..a582b8738eb1f 100644 --- a/library/unwind/src/lib.rs +++ b/library/unwind/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![unstable(feature = "panic_unwind", issue = "32837")] +#![feature(cfg_match)] #![feature(link_cfg)] #![feature(staged_api)] #![feature(c_unwind)] @@ -8,28 +9,32 @@ #![cfg_attr(not(target_env = "msvc"), feature(libc))] #![allow(internal_features)] -cfg_if::cfg_if! { - if #[cfg(target_env = "msvc")] { +cfg_match! { + cfg(target_env = "msvc") => { // Windows MSVC no extra unwinder support needed - } else if #[cfg(any( + } + cfg(any( target_os = "l4re", target_os = "none", target_os = "espidf", - ))] { + )) => { // These "unix" family members do not have unwinder. - } else if #[cfg(any( + } + cfg(any( unix, windows, target_os = "psp", target_os = "solid_asp3", all(target_vendor = "fortanix", target_env = "sgx"), - ))] { + )) => { mod libunwind; pub use libunwind::*; - } else if #[cfg(target_os = "xous")] { + } + cfg(target_os = "xous") => { mod unwinding; pub use unwinding::*; - } else { + } + _ => { // no unwinder on the system! // - wasm32 (not emscripten, which is "unix" family) // - os=none ("bare metal" targets) @@ -42,17 +47,20 @@ cfg_if::cfg_if! { } #[cfg(target_env = "musl")] -cfg_if::cfg_if! { - if #[cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind"))] { +cfg_match! { + cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind")) => { compile_error!("`llvm-libunwind` and `system-llvm-libunwind` cannot be enabled at the same time"); - } else if #[cfg(feature = "llvm-libunwind")] { + } + cfg(feature = "llvm-libunwind") => { #[link(name = "unwind", kind = "static", modifiers = "-bundle")] extern "C" {} - } else if #[cfg(feature = "system-llvm-libunwind")] { + } + cfg(feature = "system-llvm-libunwind") => { #[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))] #[link(name = "unwind", cfg(not(target_feature = "crt-static")))] extern "C" {} - } else { + } + _ => { #[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))] #[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))] extern "C" {} @@ -62,13 +70,15 @@ cfg_if::cfg_if! { // This is the same as musl except that we default to using the system libunwind // instead of libgcc. #[cfg(target_env = "ohos")] -cfg_if::cfg_if! { - if #[cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind"))] { +cfg_match! { + cfg(all(feature = "llvm-libunwind", feature = "system-llvm-libunwind")) => { compile_error!("`llvm-libunwind` and `system-llvm-libunwind` cannot be enabled at the same time"); - } else if #[cfg(feature = "llvm-libunwind")] { + } + cfg(feature = "llvm-libunwind") => { #[link(name = "unwind", kind = "static", modifiers = "-bundle")] extern "C" {} - } else { + } + _ => { #[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))] #[link(name = "unwind", cfg(not(target_feature = "crt-static")))] extern "C" {} @@ -76,10 +86,11 @@ cfg_if::cfg_if! { } #[cfg(target_os = "android")] -cfg_if::cfg_if! { - if #[cfg(feature = "llvm-libunwind")] { +cfg_match! { + cfg(feature = "llvm-libunwind") => { compile_error!("`llvm-libunwind` is not supported for Android targets"); - } else { + } + _ => { #[link(name = "unwind", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))] #[link(name = "unwind", cfg(not(target_feature = "crt-static")))] extern "C" {} diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 1b5f6f9dde36c..3cf4e7af0c606 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -122,8 +122,8 @@ extern "C" { pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr; } -cfg_if::cfg_if! { -if #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "netbsd", not(target_arch = "arm")))] { +cfg_match! { +cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", target_os = "netbsd", not(target_arch = "arm"))) => { // Not ARM EHABI #[repr(C)] #[derive(Copy, Clone, PartialEq)] @@ -149,8 +149,8 @@ if #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", targe -> _Unwind_Word; pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void; } - -} else { +} +_ => { // ARM EHABI #[repr(C)] #[derive(Copy, Clone, PartialEq)] @@ -257,8 +257,8 @@ if #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos", targe } } // cfg_if! -cfg_if::cfg_if! { -if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] { +cfg_match! { +cfg(not(all(target_os = "ios", target_arch = "arm"))) => { // Not 32-bit iOS #[cfg_attr( all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux", target_os = "xous")), @@ -276,7 +276,8 @@ if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] { trace_argument: *mut c_void) -> _Unwind_Reason_Code; } -} else { +} +_ => { // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace() extern "C-unwind" { pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code; @@ -286,8 +287,8 @@ if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] { } } // cfg_if! -cfg_if::cfg_if! { -if #[cfg(all(windows, any(target_arch = "aarch64", target_arch = "x86_64"), target_env = "gnu"))] { +cfg_match! { +cfg(all(windows, any(target_arch = "aarch64", target_arch = "x86_64"), target_env = "gnu")) => { // We declare these as opaque types. This is fine since you just need to // pass them to _GCC_specific_handler and forget about them. pub enum EXCEPTION_RECORD {}