Skip to content

Commit 7a0b15b

Browse files
committed
tests: Use FFI binding static assertions helpers from test-helper
1 parent 5ae0ef5 commit 7a0b15b

File tree

4 files changed

+25
-153
lines changed

4 files changed

+25
-153
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ serde = { version = "1.0.60", optional = true, default-features = false }
8383
critical-section = { version = "1", optional = true }
8484

8585
# [target.'cfg(portable_atomic_test_no_std_static_assert_ffi)'.dependencies] #build:static_assert_ffi
86-
# test-helper = { features = ["sys"], git = "https://github.com/taiki-e/test-helper.git", rev = "93816e3" } #build:static_assert_ffi
86+
# test-helper = { features = ["sys"], git = "https://github.com/taiki-e/test-helper.git", rev = "9b4be43" } #build:static_assert_ffi
8787
# [target.'cfg(all(portable_atomic_test_no_std_static_assert_ffi, target_os = "aix"))'.dependencies] #build:static_assert_ffi
8888
# libc = "=0.2.163" #build:static_assert_ffi
8989

@@ -97,7 +97,7 @@ rustversion = "1"
9797
serde_test = { git = "https://github.com/taiki-e/serde_test.git", rev = "f35c385" } # support {i,u}128
9898
sptr = "0.3"
9999
static_assertions = "1"
100-
test-helper = { features = ["sys", "critical-section-std"], git = "https://github.com/taiki-e/test-helper.git", rev = "93816e3" }
100+
test-helper = { features = ["sys", "critical-section-std"], git = "https://github.com/taiki-e/test-helper.git", rev = "9b4be43" }
101101

102102
[target.'cfg(unix)'.dev-dependencies]
103103
libc = "=0.2.163" # newer libc requires Rust 1.63

bench/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fastrand = "2"
1919
paste = "1"
2020
quickcheck = { version = "1", default-features = false, git = "https://github.com/taiki-e/quickcheck.git", rev = "83b1d59" } # https://github.com/BurntSushi/quickcheck/pull/304 + https://github.com/BurntSushi/quickcheck/pull/282 + https://github.com/BurntSushi/quickcheck/pull/296 + f16/f128 support + lower MSRV
2121
static_assertions = "1"
22-
test-helper = { features = ["sys", "critical-section-std"], git = "https://github.com/taiki-e/test-helper.git", rev = "93816e3" }
22+
test-helper = { features = ["sys", "critical-section-std"], git = "https://github.com/taiki-e/test-helper.git", rev = "9b4be43" }
2323

2424
[target.'cfg(unix)'.dev-dependencies]
2525
libc = "0.2"

build.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn main() {
5454
// Custom cfgs set by build script. Not public API.
5555
// grep -F 'cargo:rustc-cfg=' build.rs | grep -Ev '^ *//' | sed -E 's/^.*cargo:rustc-cfg=//; s/(=\\)?".*$//' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
5656
println!(
57-
"cargo:rustc-check-cfg=cfg(portable_atomic_disable_fiq,portable_atomic_force_amo,portable_atomic_ll_sc_rmw,portable_atomic_atomic_intrinsics,portable_atomic_no_asm,portable_atomic_no_asm_maybe_uninit,portable_atomic_no_atomic_64,portable_atomic_no_atomic_cas,portable_atomic_no_atomic_load_store,portable_atomic_no_atomic_min_max,portable_atomic_no_cfg_target_has_atomic,portable_atomic_no_cmpxchg16b_intrinsic,portable_atomic_no_cmpxchg16b_target_feature,portable_atomic_no_const_mut_refs,portable_atomic_no_const_raw_ptr_deref,portable_atomic_no_const_transmute,portable_atomic_no_core_unwind_safe,portable_atomic_no_diagnostic_namespace,portable_atomic_no_offset_of,portable_atomic_no_strict_provenance,portable_atomic_no_stronger_failure_ordering,portable_atomic_no_track_caller,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_pre_llvm_15,portable_atomic_pre_llvm_16,portable_atomic_pre_llvm_18,portable_atomic_pre_llvm_20,portable_atomic_s_mode,portable_atomic_sanitize_thread,portable_atomic_target_feature,portable_atomic_unsafe_assume_single_core,portable_atomic_unstable_asm,portable_atomic_unstable_asm_experimental_arch,portable_atomic_unstable_cfg_target_has_atomic,portable_atomic_unstable_isa_attribute)"
57+
"cargo:rustc-check-cfg=cfg(portable_atomic_disable_fiq,portable_atomic_force_amo,portable_atomic_ll_sc_rmw,portable_atomic_atomic_intrinsics,portable_atomic_no_asm,portable_atomic_no_asm_maybe_uninit,portable_atomic_no_atomic_64,portable_atomic_no_atomic_cas,portable_atomic_no_atomic_load_store,portable_atomic_no_atomic_min_max,portable_atomic_no_cfg_target_has_atomic,portable_atomic_no_cmpxchg16b_intrinsic,portable_atomic_no_cmpxchg16b_target_feature,portable_atomic_no_const_mut_refs,portable_atomic_no_const_raw_ptr_deref,portable_atomic_no_const_transmute,portable_atomic_no_core_unwind_safe,portable_atomic_no_diagnostic_namespace,portable_atomic_no_strict_provenance,portable_atomic_no_stronger_failure_ordering,portable_atomic_no_track_caller,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_pre_llvm_15,portable_atomic_pre_llvm_16,portable_atomic_pre_llvm_18,portable_atomic_pre_llvm_20,portable_atomic_s_mode,portable_atomic_sanitize_thread,portable_atomic_target_feature,portable_atomic_unsafe_assume_single_core,portable_atomic_unstable_asm,portable_atomic_unstable_asm_experimental_arch,portable_atomic_unstable_cfg_target_has_atomic,portable_atomic_unstable_isa_attribute)"
5858
);
5959
// TODO: handle multi-line target_feature_fallback
6060
// grep -F 'target_feature_fallback("' build.rs | grep -Ev '^ *//' | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' | sed -E 's/,$/\n/'
@@ -115,10 +115,6 @@ fn main() {
115115
if !version.probe(74, 2023, 8, 23) {
116116
println!("cargo:rustc-cfg=portable_atomic_no_asm_maybe_uninit");
117117
}
118-
// For test
119-
if version.minor < 77 {
120-
println!("cargo:rustc-cfg=portable_atomic_no_offset_of");
121-
}
122118
// #[diagnostic] stabilized in Rust 1.78 (nightly-2024-03-09): https://github.com/rust-lang/rust/pull/119888
123119
if !version.probe(78, 2024, 3, 8) {
124120
println!("cargo:rustc-cfg=portable_atomic_no_diagnostic_namespace");

src/utils.rs

Lines changed: 21 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -720,33 +720,11 @@ pub(crate) mod ffi {
720720
$(#[$attr])*
721721
$vis type $name = $ty;
722722
)*
723-
// Static assertions for FFI bindings.
724-
// This checks that FFI bindings defined in this crate and FFI bindings generated for
725-
// the platform's latest header file using bindgen have the same types.
726-
// Since this is static assertion, we can detect problems with
727-
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
728-
// without actually running tests on these platforms.
729-
// See also https://github.com/taiki-e/test-helper/blob/HEAD/tools/codegen/src/ffi.rs.
730723
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
731-
#[allow(
732-
unused_imports,
733-
clippy::cast_possible_wrap,
734-
clippy::cast_sign_loss,
735-
clippy::cast_possible_truncation
736-
)]
737-
const _: fn() = || {
738-
#[cfg(not(any(target_os = "aix", windows)))]
739-
use test_helper::sys;
740-
#[cfg(target_os = "aix")]
741-
use libc as sys;
742-
$(
743-
$(#[$attr])*
744-
{
745-
$(use windows_sys::$($windows_path)::+ as sys;)?
746-
let _: $name = 0 as sys::$name;
747-
}
748-
)*
749-
};
724+
test_helper::static_assert_sys_type!($(
725+
$(#[$attr])*
726+
type $([$($windows_path)::+])? $name;
727+
)*);
750728
};
751729
}
752730
/// Defines #[repr(C)] structs with #[cfg(test)] static assertions which checks
@@ -774,46 +752,14 @@ pub(crate) mod ffi {
774752
$field_vis $field_name: $field_ty,
775753
)*}
776754
)*
777-
// Static assertions for FFI bindings.
778-
// This checks that FFI bindings defined in this crate and FFI bindings generated for
779-
// the platform's latest header file using bindgen have the same fields.
780-
// Since this is static assertion, we can detect problems with
781-
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
782-
// without actually running tests on these platforms.
783-
// See also https://github.com/taiki-e/test-helper/blob/HEAD/tools/codegen/src/ffi.rs.
784755
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
785-
#[allow(unused_imports, clippy::undocumented_unsafe_blocks)]
786-
const _: fn() = || {
787-
#[cfg(not(any(target_os = "aix", windows)))]
788-
use test_helper::sys;
789-
#[cfg(target_os = "aix")]
790-
use libc as sys;
791-
$(
792-
$(#[$attr])*
793-
{
794-
$(use windows_sys::$($windows_path)::+ as sys;)?
795-
static_assert!(
796-
core::mem::size_of::<$name>()
797-
== core::mem::size_of::<sys::$name>()
798-
);
799-
let s: $name = unsafe { core::mem::zeroed() };
800-
// field names and types
801-
let _ = sys::$name {$(
802-
$(#[$field_attr])*
803-
$field_name: s.$field_name,
804-
)*};
805-
// field offsets
806-
#[cfg(not(portable_atomic_no_offset_of))]
807-
{$(
808-
$(#[$field_attr])*
809-
static_assert!(
810-
core::mem::offset_of!($name, $field_name) ==
811-
core::mem::offset_of!(sys::$name, $field_name),
812-
);
813-
)*}
814-
}
815-
)*
816-
};
756+
test_helper::static_assert_sys_struct!($(
757+
$(#[$attr])*
758+
struct $([$($windows_path)::+])? $name {$(
759+
$(#[$field_attr])*
760+
$field_name: $field_ty,
761+
)*}
762+
)*);
817763
};
818764
}
819765
/// Defines constants with #[cfg(test)] static assertions which checks
@@ -829,52 +775,11 @@ pub(crate) mod ffi {
829775
$(#[$attr])*
830776
$vis const $name: $ty = $val;
831777
)*
832-
// Static assertions for FFI bindings.
833-
// This checks that FFI bindings defined in this crate and FFI bindings generated for
834-
// the platform's latest header file using bindgen have the same values.
835-
// Since this is static assertion, we can detect problems with
836-
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
837-
// without actually running tests on these platforms.
838-
// See also https://github.com/taiki-e/test-helper/blob/HEAD/tools/codegen/src/ffi.rs.
839778
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
840-
#[allow(
841-
unused_attributes, // for #[allow(..)] in $(#[$attr])*
842-
unused_imports,
843-
clippy::cast_possible_wrap,
844-
clippy::cast_sign_loss,
845-
clippy::cast_possible_truncation,
846-
)]
847-
const _: fn() = || {
848-
#[cfg(not(any(target_os = "aix", windows)))]
849-
use test_helper::sys;
850-
#[cfg(target_os = "aix")]
851-
use libc as sys;
852-
$(
853-
$(#[$attr])*
854-
{
855-
$(use windows_sys::$($windows_path)::+ as sys;)?
856-
sys_const_cmp!($name, $ty);
857-
}
858-
)*
859-
};
860-
};
861-
}
862-
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
863-
macro_rules! sys_const_cmp {
864-
(RTLD_DEFAULT, $ty:ty) => {
865-
// ptr comparison and ptr-to-int cast are not stable on const context, so use ptr-to-int
866-
// transmute and compare its result.
867-
static_assert!(
868-
// SAFETY: Pointer-to-integer transmutes are valid (since we are okay with losing the
869-
// provenance here). (Same as <pointer>::addr().)
870-
unsafe {
871-
core::mem::transmute::<$ty, usize>(RTLD_DEFAULT)
872-
== core::mem::transmute::<$ty, usize>(sys::RTLD_DEFAULT)
873-
}
874-
);
875-
};
876-
($name:ident, $ty:ty) => {
877-
static_assert!($name == sys::$name as $ty);
779+
test_helper::static_assert_sys_const!($(
780+
$(#[$attr])*
781+
const $([$($windows_path)::+])? $name: $ty;
782+
)*);
878783
};
879784
}
880785
/// Defines functions with #[cfg(test)] static assertions which checks
@@ -896,43 +801,14 @@ pub(crate) mod ffi {
896801
$(#[$fn_attr])*
897802
$vis fn $name($($args)*) $(-> $ret_ty)?;
898803
)*}
899-
// Static assertions for FFI bindings.
900-
// This checks that FFI bindings defined in this crate and FFI bindings generated for
901-
// the platform's latest header file using bindgen have the same signatures.
902-
// Since this is static assertion, we can detect problems with
903-
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
904-
// without actually running tests on these platforms.
905-
// See also https://github.com/taiki-e/test-helper/blob/HEAD/tools/codegen/src/ffi.rs.
906804
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
907-
#[allow(unused_imports)]
908-
const _: fn() = || {
909-
#[cfg(not(any(target_os = "aix", windows)))]
910-
use test_helper::sys;
911-
#[cfg(target_os = "aix")]
912-
use libc as sys;
913-
$(
805+
test_helper::static_assert_sys_fn!(
806+
$(#[$extern_attr])*
807+
extern $abi {$(
914808
$(#[$fn_attr])*
915-
{
916-
$(use windows_sys::$($windows_path)::+ as sys;)?
917-
sys_fn_cmp!($abi fn $name($($args)*) $(-> $ret_ty)?);
918-
}
919-
)*
920-
};
921-
};
922-
}
923-
#[cfg(any(test, portable_atomic_test_no_std_static_assert_ffi))]
924-
macro_rules! sys_fn_cmp {
925-
(
926-
$abi:literal fn $name:ident($($_arg_pat:ident: $arg_ty:ty),*, ...) $(-> $ret_ty:ty)?
927-
) => {
928-
let mut _f: unsafe extern $abi fn($($arg_ty),*, ...) $(-> $ret_ty)? = $name;
929-
_f = sys::$name;
930-
};
931-
(
932-
$abi:literal fn $name:ident($($_arg_pat:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?
933-
) => {
934-
let mut _f: unsafe extern $abi fn($($arg_ty),*) $(-> $ret_ty)? = $name;
935-
_f = sys::$name;
809+
fn $([$($windows_path)::+])? $name($($args)*) $(-> $ret_ty)?;
810+
)*}
811+
);
936812
};
937813
}
938814

0 commit comments

Comments
 (0)