From 576f77814cff61d0ed7e30f2a7d0f0f696001bd5 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 8 Jun 2022 17:40:08 +0900 Subject: [PATCH 1/2] Enable `libc_const_extern_fn` implicitly from Rust 1.62 --- README.md | 4 +++- build.rs | 15 +++++++++++---- src/lib.rs | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a8a3afd9f6cf2..14fa5831c2b29 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ libc = "0.2" This feature derives `Debug`, `Eq`, `Hash`, and `PartialEq`. * `const-extern-fn`: Changes some `extern fn`s into `const extern fn`s. - This feature requires a nightly rustc. + If you use Rust >= 1.62, this feature is implicitly enabled. + Otherwise it requires a nightly rustc. * **deprecated**: `use_std` is deprecated, and is equivalent to `std`. @@ -53,6 +54,7 @@ newer Rust features are only available on newer Rust toolchains: | `core::ffi::c_void` | 1.30.0 | | `repr(packed(N))` | 1.33.0 | | `cfg(target_vendor)` | 1.33.0 | +| `const-extern-fn` | 1.62.0 | ## Platform support diff --git a/build.rs b/build.rs index bc7b77f25e97d..0a43b2a12ea96 100644 --- a/build.rs +++ b/build.rs @@ -97,11 +97,18 @@ fn main() { println!("cargo:rustc-cfg=libc_thread_local"); } - if const_extern_fn_cargo_feature { - if !is_nightly || rustc_minor_ver < 40 { - panic!("const-extern-fn requires a nightly compiler >= 1.40") - } + // Rust >= 1.62.0 allows to use `const_extern_fn` for "Rust" and "C". + if rustc_minor_ver >= 62 { println!("cargo:rustc-cfg=libc_const_extern_fn"); + } else { + // Rust < 1.62.0 requires a crate feature and feature gate. + if const_extern_fn_cargo_feature { + if !is_nightly || rustc_minor_ver < 40 { + panic!("const-extern-fn requires a nightly compiler >= 1.40"); + } + println!("cargo:rustc-cfg=libc_const_extern_fn_unstable"); + println!("cargo:rustc-cfg=libc_const_extern_fn"); + } } } diff --git a/src/lib.rs b/src/lib.rs index c0ef813283a5b..3ad346a429353 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,7 @@ feature = "rustc-dep-of-std", feature(native_link_modifiers, native_link_modifiers_bundle) )] -#![cfg_attr(libc_const_extern_fn, feature(const_extern_fn))] +#![cfg_attr(libc_const_extern_fn_unstable, feature(const_extern_fn))] #[macro_use] mod macros; From 4a84f3487979a5f7d26a7c32c178db5f5fdcffcb Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Wed, 8 Jun 2022 18:57:40 +0900 Subject: [PATCH 2/2] Correctly handle some consts declaration on macOS --- src/unix/bsd/apple/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/unix/bsd/apple/mod.rs b/src/unix/bsd/apple/mod.rs index da75b97a70008..4b237d83fa23b 100644 --- a/src/unix/bsd/apple/mod.rs +++ b/src/unix/bsd/apple/mod.rs @@ -4682,6 +4682,16 @@ cfg_if! { const __DARWIN_ALIGNBYTES32: usize = ::mem::size_of::() - 1; p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32 } + } else { + fn __DARWIN_ALIGN32(p: usize) -> usize { + let __DARWIN_ALIGNBYTES32: usize = ::mem::size_of::() - 1; + p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32 + } + } +} + +cfg_if! { + if #[cfg(libc_const_size_of)] { pub const THREAD_EXTENDED_POLICY_COUNT: mach_msg_type_number_t = (::mem::size_of::() / ::mem::size_of::()) as mach_msg_type_number_t; @@ -4722,10 +4732,6 @@ cfg_if! { (::mem::size_of::() / ::mem::size_of::()) as mach_msg_type_number_t; } else { - fn __DARWIN_ALIGN32(p: usize) -> usize { - let __DARWIN_ALIGNBYTES32: usize = ::mem::size_of::() - 1; - p + __DARWIN_ALIGNBYTES32 & !__DARWIN_ALIGNBYTES32 - } pub const THREAD_EXTENDED_POLICY_COUNT: mach_msg_type_number_t = 1; pub const THREAD_TIME_CONSTRAINT_POLICY_COUNT: mach_msg_type_number_t = 4; pub const THREAD_PRECEDENCE_POLICY_COUNT: mach_msg_type_number_t = 1;