Skip to content

Commit

Permalink
Initiate the inner usage of cfg_match (Library)
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Oct 2, 2023
1 parent 781ebbe commit cc6cee4
Show file tree
Hide file tree
Showing 36 changed files with 324 additions and 314 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5836,7 +5836,6 @@ name = "unwind"
version = "0.0.0"
dependencies = [
"cc",
"cfg-if",
"compiler_builtins",
"core",
"libc",
Expand Down
23 changes: 13 additions & 10 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ pub type c_ptrdiff_t = isize;
pub type c_ssize_t = isize;

mod c_char_definition {
cfg_if! {
cfg_match! {
// These are the targets on which c_char is unsigned.
if #[cfg(any(
cfg(any(
all(
target_os = "linux",
any(
Expand Down Expand Up @@ -150,10 +150,11 @@ mod c_char_definition {
),
all(target_os = "nto", target_arch = "aarch64"),
target_os = "horizon"
))] {
)) => {
pub type c_char = u8;
pub type NonZero_c_char = crate::num::NonZeroU8;
} else {
}
_ => {
// On every other target, c_char is signed.
pub type c_char = i8;
pub type NonZero_c_char = crate::num::NonZeroI8;
Expand All @@ -162,13 +163,14 @@ mod c_char_definition {
}

mod c_int_definition {
cfg_if! {
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
cfg_match! {
cfg(any(target_arch = "avr", target_arch = "msp430")) => {
pub type c_int = i16;
pub type NonZero_c_int = crate::num::NonZeroI16;
pub type c_uint = u16;
pub type NonZero_c_uint = crate::num::NonZeroU16;
} else {
}
_ => {
pub type c_int = i32;
pub type NonZero_c_int = crate::num::NonZeroI32;
pub type c_uint = u32;
Expand All @@ -178,13 +180,14 @@ mod c_int_definition {
}

mod c_long_definition {
cfg_if! {
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
cfg_match! {
cfg(all(target_pointer_width = "64", not(windows))) => {
pub type c_long = i64;
pub type NonZero_c_long = crate::num::NonZeroI64;
pub type c_ulong = u64;
pub type NonZero_c_ulong = crate::num::NonZeroU64;
} else {
}
_ =>{
// The minimal size of `long` in the C standard is 32 bits
pub type c_long = i32;
pub type NonZero_c_long = crate::num::NonZeroI32;
Expand Down
77 changes: 0 additions & 77 deletions library/core/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,80 +116,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 )*
};
}
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(no_coverage))] // rust-lang/rust#84605
#![cfg_attr(not(bootstrap), feature(coverage_attribute))] // rust-lang/rust#84605
#![feature(cfg_match)]
#![feature(char_indices_offset)]
#![feature(const_align_of_val)]
#![feature(const_align_of_val_raw)]
Expand Down
22 changes: 13 additions & 9 deletions library/panic_abort/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -37,24 +38,26 @@ 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" {
pub fn __rust_abort() -> !;
}
__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
Expand All @@ -81,7 +84,8 @@ pub unsafe fn __rust_start_panic(_payload: &mut dyn PanicPayload) -> u32 {
}
core::intrinsics::unreachable();
}
} else {
}
_ => {
unsafe fn abort() -> ! {
core::intrinsics::abort();
}
Expand Down
29 changes: 18 additions & 11 deletions library/panic_unwind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -31,31 +32,36 @@ 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 = "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)
Expand All @@ -68,14 +74,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;
}
Expand Down
7 changes: 4 additions & 3 deletions library/panic_unwind/src/seh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
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 @@ -305,6 +305,7 @@
//
// Library features (core):
// tidy-alphabetical-start
#![feature(cfg_match)]
#![feature(char_internals)]
#![feature(core_intrinsics)]
#![feature(duration_constants)]
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 @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions library/std/src/sys/common/thread_local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Loading

0 comments on commit cc6cee4

Please sign in to comment.