Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initiate the inner usage of cfg_match (Library) #116342

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6004,7 +6004,6 @@ dependencies = [
name = "unwind"
version = "0.0.0"
dependencies = [
"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 @@ -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(
Expand Down Expand Up @@ -124,33 +124,36 @@ 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;
}
}
}

mod c_int_definition {
cfg_if! {
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
crate::cfg_match! {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All crates but core are using cfg_match! as if it was in the prelude, i.e., no path reference. Maybe because cfg_match! is being re-exported at the root level?

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;
}
}
}

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;
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 @@ -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 )*
};
}
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
25 changes: 15 additions & 10 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 @@ -42,24 +43,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 @@ -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) -> !;
Expand All @@ -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();
}
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,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)
Expand All @@ -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;
}
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 @@ -310,6 +310,7 @@
//
// Library features (core):
// tidy-alphabetical-start
#![feature(cfg_match)]
#![feature(char_internals)]
#![feature(core_intrinsics)]
#![feature(core_io_borrowed_buf)]
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/pal/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
Loading