@@ -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