From 354a46f90cde6a0b5ddc9714995514f69764f144 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Sun, 16 Jan 2022 02:36:24 -0800 Subject: [PATCH 1/5] Implement Debug, Pointer, etc on function pointers for all stable calling conventions --- library/core/src/ptr/mod.rs | 67 +++++++++++++++---- .../function-pointer-impls.rs | 11 +++ 2 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 src/test/ui/function-pointer/function-pointer-impls.rs diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 8ab72e6aeeafa..d2818e5677d2c 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1451,21 +1451,62 @@ macro_rules! fnptr_impls_safety_abi { } } -macro_rules! fnptr_impls_args { - ($($Arg: ident),+) => { - fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ } - fnptr_impls_safety_abi! { extern "C" fn($($Arg),+) -> Ret, $($Arg),+ } - fnptr_impls_safety_abi! { extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ } - fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ } - fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+) -> Ret, $($Arg),+ } - fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ } +macro_rules! fnptr_impl_for_abi { + ($Abi:tt, $($Arg: ident),*) => { + fnptr_impls_safety_abi! { extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } + fnptr_impls_safety_abi! { unsafe extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } }; - () => { +} + +macro_rules! fnptr_impl_for_abi_variadic_support { + ($Abi:tt, ) => { // No variadic functions with 0 parameters - fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, } - fnptr_impls_safety_abi! { extern "C" fn() -> Ret, } - fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, } - fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, } + fnptr_impls_safety_abi! { extern $Abi fn() -> Ret, } + fnptr_impls_safety_abi! { unsafe extern $Abi fn() -> Ret, } + }; + + ($Abi:tt, $($Arg: ident),*) => { + fnptr_impl_for_abi! { $Abi, $($Arg),* } + + fnptr_impls_safety_abi! { extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } + fnptr_impls_safety_abi! { unsafe extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } + } +} + +macro_rules! fnptr_impls_args { + ($($Arg: ident),*) => { + fnptr_impl_for_abi! { "Rust", $($Arg),* } + fnptr_impl_for_abi! { "stdcall", $($Arg),* } + fnptr_impl_for_abi! { "fastcall", $($Arg),* } + fnptr_impl_for_abi! { "aapcs", $($Arg),* } + fnptr_impl_for_abi! { "win64", $($Arg),* } + fnptr_impl_for_abi! { "sysv64", $($Arg),* } + fnptr_impl_for_abi! { "system", $($Arg),* } + fnptr_impl_for_abi! { "rust-intrinsic", $($Arg),* } + fnptr_impl_for_abi! { "rust-call", $($Arg),* } + fnptr_impl_for_abi! { "platform-intrinsic", $($Arg),* } + fnptr_impl_for_abi! { "unadjusted", $($Arg),* } + + // fnptr_impl_for_abi! { "C-unwind", $($Arg),* } + // fnptr_impl_for_abi! { "stdcall-unwind", $($Arg),* } + // fnptr_impl_for_abi! { "vectorcall", $($Arg),* } + // fnptr_impl_for_abi! { "thiscall", $($Arg),* } + // fnptr_impl_for_abi! { "thiscall-unwind", $($Arg),* } + + // fnptr_impl_for_abi! { "ptx-kernel", $($Arg),* } + // fnptr_impl_for_abi! { "msp430-interrupt", $($Arg),* } + // fnptr_impl_for_abi! { "x86-interrupt", $($Arg),* } + // fnptr_impl_for_abi! { "amdgpu-kernel", $($Arg),* } + // fnptr_impl_for_abi! { "efiapi", $($Arg),* } + // fnptr_impl_for_abi! { "avr-interrupt", $($Arg),* } + // fnptr_impl_for_abi! { "avr-non-blocking-interrupt", $($Arg),* } + // fnptr_impl_for_abi! { "C-cmse-nonsecure-call", $($Arg),* } + // fnptr_impl_for_abi! { "wasm", $($Arg),* } + + // fnptr_impl_for_abi! { "system-unwind", $($Arg),* } + + fnptr_impl_for_abi_variadic_support! { "C", $($Arg),* } + fnptr_impl_for_abi_variadic_support! { "cdecl", $($Arg),* } }; } diff --git a/src/test/ui/function-pointer/function-pointer-impls.rs b/src/test/ui/function-pointer/function-pointer-impls.rs new file mode 100644 index 0000000000000..25ba4e7757cfa --- /dev/null +++ b/src/test/ui/function-pointer/function-pointer-impls.rs @@ -0,0 +1,11 @@ +// check-pass +fn assert_debug() {} + +fn main() { + assert_debug::(); + assert_debug::(); + assert_debug::(); + assert_debug::(); + assert_debug::(); + assert_debug::(); +} From 3af522fe9fe0e42cf7911ddd6de9bc4a61985ac0 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Fri, 1 Apr 2022 19:04:03 -0700 Subject: [PATCH 2/5] Enable a bunch of features to make it compile with the unstable ABIs --- library/core/src/lib.rs | 11 +++++++++++ library/core/src/ptr/mod.rs | 34 +++++++++++++++++----------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 4bd94e3ce3915..2cd234cb351d7 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -153,11 +153,21 @@ #![feature(const_slice_from_ref)] // // Language features: +#![feature(abi_amdgpu_kernel)] +#![feature(abi_avr_interrupt)] +#![feature(abi_c_cmse_nonsecure_call)] +#![feature(abi_efiapi)] +#![feature(abi_msp430_interrupt)] +#![feature(abi_ptx)] +#![feature(abi_thiscall)] #![feature(abi_unadjusted)] +#![feature(abi_vectorcall)] +#![feature(abi_x86_interrupt)] #![feature(allow_internal_unsafe)] #![feature(allow_internal_unstable)] #![feature(associated_type_bounds)] #![feature(auto_traits)] +#![feature(c_unwind)] #![feature(cfg_target_has_atomic)] #![feature(const_fn_floating_point_arithmetic)] #![feature(const_fn_fn_ptr_basics)] @@ -202,6 +212,7 @@ #![feature(try_blocks)] #![feature(unboxed_closures)] #![feature(unsized_fn_params)] +#![feature(wasm_abi)] #![feature(asm_const)] // // Target features: diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index d2818e5677d2c..c17237bde9279 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1487,23 +1487,23 @@ macro_rules! fnptr_impls_args { fnptr_impl_for_abi! { "platform-intrinsic", $($Arg),* } fnptr_impl_for_abi! { "unadjusted", $($Arg),* } - // fnptr_impl_for_abi! { "C-unwind", $($Arg),* } - // fnptr_impl_for_abi! { "stdcall-unwind", $($Arg),* } - // fnptr_impl_for_abi! { "vectorcall", $($Arg),* } - // fnptr_impl_for_abi! { "thiscall", $($Arg),* } - // fnptr_impl_for_abi! { "thiscall-unwind", $($Arg),* } - - // fnptr_impl_for_abi! { "ptx-kernel", $($Arg),* } - // fnptr_impl_for_abi! { "msp430-interrupt", $($Arg),* } - // fnptr_impl_for_abi! { "x86-interrupt", $($Arg),* } - // fnptr_impl_for_abi! { "amdgpu-kernel", $($Arg),* } - // fnptr_impl_for_abi! { "efiapi", $($Arg),* } - // fnptr_impl_for_abi! { "avr-interrupt", $($Arg),* } - // fnptr_impl_for_abi! { "avr-non-blocking-interrupt", $($Arg),* } - // fnptr_impl_for_abi! { "C-cmse-nonsecure-call", $($Arg),* } - // fnptr_impl_for_abi! { "wasm", $($Arg),* } - - // fnptr_impl_for_abi! { "system-unwind", $($Arg),* } + fnptr_impl_for_abi! { "C-unwind", $($Arg),* } + fnptr_impl_for_abi! { "stdcall-unwind", $($Arg),* } + fnptr_impl_for_abi! { "vectorcall", $($Arg),* } + fnptr_impl_for_abi! { "thiscall", $($Arg),* } + fnptr_impl_for_abi! { "thiscall-unwind", $($Arg),* } + + fnptr_impl_for_abi! { "ptx-kernel", $($Arg),* } + fnptr_impl_for_abi! { "msp430-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "x86-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "amdgpu-kernel", $($Arg),* } + fnptr_impl_for_abi! { "efiapi", $($Arg),* } + fnptr_impl_for_abi! { "avr-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "avr-non-blocking-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "C-cmse-nonsecure-call", $($Arg),* } + fnptr_impl_for_abi! { "wasm", $($Arg),* } + + fnptr_impl_for_abi! { "system-unwind", $($Arg),* } fnptr_impl_for_abi_variadic_support! { "C", $($Arg),* } fnptr_impl_for_abi_variadic_support! { "cdecl", $($Arg),* } From c261f13b37c655d3fdbefaa785f5645396843ff3 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Fri, 27 May 2022 18:58:20 -0700 Subject: [PATCH 3/5] Shrink doc sizes --- library/core/src/primitive_docs.rs | 5 ++- library/core/src/ptr/mod.rs | 67 ++++++++++++++++++------------ library/std/src/primitive_docs.rs | 5 ++- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 8fcd8cdeb1042..d7e63d9a2d4de 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1294,8 +1294,9 @@ mod prim_ref {} /// [`Pointer`]: fmt::Pointer /// /// Due to a temporary restriction in Rust's type system, these traits are only implemented on -/// functions that take 12 arguments or less, with the `"Rust"` and `"C"` ABIs. In the future, this -/// may change. +/// functions that take 12 arguments or less. In the future, this may change. +/// +/// These traits are implemented on all available ABIs, though the documentation hides them. /// /// In addition, function pointers of *any* signature, ABI, or safety are [`Copy`], and all *safe* /// function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`]. This works because these traits diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index c17237bde9279..b679f20f10e30 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1390,7 +1390,8 @@ pub fn hash(hashee: *const T, into: &mut S) { // Impls for function pointers macro_rules! fnptr_impls_safety_abi { - ($FnTy: ty, $($Arg: ident),*) => { + ($(#[$Meta:meta])? $FnTy: ty, $($Arg: ident),*) => { + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl PartialEq for $FnTy { #[inline] @@ -1399,9 +1400,11 @@ macro_rules! fnptr_impls_safety_abi { } } + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl Eq for $FnTy {} + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl PartialOrd for $FnTy { #[inline] @@ -1410,6 +1413,7 @@ macro_rules! fnptr_impls_safety_abi { } } + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl Ord for $FnTy { #[inline] @@ -1418,6 +1422,7 @@ macro_rules! fnptr_impls_safety_abi { } } + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl hash::Hash for $FnTy { fn hash(&self, state: &mut HH) { @@ -1425,6 +1430,7 @@ macro_rules! fnptr_impls_safety_abi { } } + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl fmt::Pointer for $FnTy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -1437,6 +1443,7 @@ macro_rules! fnptr_impls_safety_abi { } } + $(#[$Meta])? #[stable(feature = "fnptr_impls", since = "1.4.0")] impl fmt::Debug for $FnTy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -1451,7 +1458,17 @@ macro_rules! fnptr_impls_safety_abi { } } +// #[doc(hidden)] is applied by default due to how many permutations are made. +// Without this, the documentation page for `fn` becomes 14 MB. +// In the future, this could be replaced by having rustdoc collapse repetitive ABI impls. macro_rules! fnptr_impl_for_abi { + ($Abi:tt, $($Arg: ident),*) => { + fnptr_impls_safety_abi! { #[doc(hidden)] extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } + fnptr_impls_safety_abi! { #[doc(hidden)] unsafe extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } + }; +} + +macro_rules! fnptr_impl_for_abi_with_docs { ($Abi:tt, $($Arg: ident),*) => { fnptr_impls_safety_abi! { extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } fnptr_impls_safety_abi! { unsafe extern $Abi fn($($Arg),*) -> Ret, $($Arg),* } @@ -1468,42 +1485,40 @@ macro_rules! fnptr_impl_for_abi_variadic_support { ($Abi:tt, $($Arg: ident),*) => { fnptr_impl_for_abi! { $Abi, $($Arg),* } - fnptr_impls_safety_abi! { extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } - fnptr_impls_safety_abi! { unsafe extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } + fnptr_impls_safety_abi! { #[doc(hidden)] extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } + fnptr_impls_safety_abi! { #[doc(hidden)] unsafe extern $Abi fn($($Arg),*, ...) -> Ret, $($Arg),* } } } macro_rules! fnptr_impls_args { ($($Arg: ident),*) => { - fnptr_impl_for_abi! { "Rust", $($Arg),* } - fnptr_impl_for_abi! { "stdcall", $($Arg),* } - fnptr_impl_for_abi! { "fastcall", $($Arg),* } - fnptr_impl_for_abi! { "aapcs", $($Arg),* } - fnptr_impl_for_abi! { "win64", $($Arg),* } - fnptr_impl_for_abi! { "sysv64", $($Arg),* } - fnptr_impl_for_abi! { "system", $($Arg),* } - fnptr_impl_for_abi! { "rust-intrinsic", $($Arg),* } - fnptr_impl_for_abi! { "rust-call", $($Arg),* } - fnptr_impl_for_abi! { "platform-intrinsic", $($Arg),* } - fnptr_impl_for_abi! { "unadjusted", $($Arg),* } + fnptr_impl_for_abi_with_docs! { "Rust", $($Arg),* } + fnptr_impl_for_abi! { "C-cmse-nonsecure-call", $($Arg),* } fnptr_impl_for_abi! { "C-unwind", $($Arg),* } - fnptr_impl_for_abi! { "stdcall-unwind", $($Arg),* } - fnptr_impl_for_abi! { "vectorcall", $($Arg),* } - fnptr_impl_for_abi! { "thiscall", $($Arg),* } - fnptr_impl_for_abi! { "thiscall-unwind", $($Arg),* } - - fnptr_impl_for_abi! { "ptx-kernel", $($Arg),* } - fnptr_impl_for_abi! { "msp430-interrupt", $($Arg),* } - fnptr_impl_for_abi! { "x86-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "aapcs", $($Arg),* } fnptr_impl_for_abi! { "amdgpu-kernel", $($Arg),* } - fnptr_impl_for_abi! { "efiapi", $($Arg),* } fnptr_impl_for_abi! { "avr-interrupt", $($Arg),* } fnptr_impl_for_abi! { "avr-non-blocking-interrupt", $($Arg),* } - fnptr_impl_for_abi! { "C-cmse-nonsecure-call", $($Arg),* } - fnptr_impl_for_abi! { "wasm", $($Arg),* } - + fnptr_impl_for_abi! { "efiapi", $($Arg),* } + fnptr_impl_for_abi! { "fastcall", $($Arg),* } + fnptr_impl_for_abi! { "msp430-interrupt", $($Arg),* } + fnptr_impl_for_abi! { "platform-intrinsic", $($Arg),* } + fnptr_impl_for_abi! { "ptx-kernel", $($Arg),* } + fnptr_impl_for_abi! { "rust-call", $($Arg),* } + fnptr_impl_for_abi! { "rust-intrinsic", $($Arg),* } + fnptr_impl_for_abi! { "stdcall", $($Arg),* } + fnptr_impl_for_abi! { "stdcall-unwind", $($Arg),* } + fnptr_impl_for_abi! { "system", $($Arg),* } fnptr_impl_for_abi! { "system-unwind", $($Arg),* } + fnptr_impl_for_abi! { "sysv64", $($Arg),* } + fnptr_impl_for_abi! { "thiscall", $($Arg),* } + fnptr_impl_for_abi! { "thiscall-unwind", $($Arg),* } + fnptr_impl_for_abi! { "unadjusted", $($Arg),* } + fnptr_impl_for_abi! { "vectorcall", $($Arg),* } + fnptr_impl_for_abi! { "wasm", $($Arg),* } + fnptr_impl_for_abi! { "win64", $($Arg),* } + fnptr_impl_for_abi! { "x86-interrupt", $($Arg),* } fnptr_impl_for_abi_variadic_support! { "C", $($Arg),* } fnptr_impl_for_abi_variadic_support! { "cdecl", $($Arg),* } diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 8fcd8cdeb1042..d7e63d9a2d4de 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -1294,8 +1294,9 @@ mod prim_ref {} /// [`Pointer`]: fmt::Pointer /// /// Due to a temporary restriction in Rust's type system, these traits are only implemented on -/// functions that take 12 arguments or less, with the `"Rust"` and `"C"` ABIs. In the future, this -/// may change. +/// functions that take 12 arguments or less. In the future, this may change. +/// +/// These traits are implemented on all available ABIs, though the documentation hides them. /// /// In addition, function pointers of *any* signature, ABI, or safety are [`Copy`], and all *safe* /// function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`]. This works because these traits From 904bd0d292882f457b30290a92ede52d2defbc8b Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Fri, 27 May 2022 19:01:19 -0700 Subject: [PATCH 4/5] Add #[doc(hidden)] to no argument variadic ABIs --- library/core/src/ptr/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index b679f20f10e30..bbf626c3cfda3 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1478,8 +1478,8 @@ macro_rules! fnptr_impl_for_abi_with_docs { macro_rules! fnptr_impl_for_abi_variadic_support { ($Abi:tt, ) => { // No variadic functions with 0 parameters - fnptr_impls_safety_abi! { extern $Abi fn() -> Ret, } - fnptr_impls_safety_abi! { unsafe extern $Abi fn() -> Ret, } + fnptr_impls_safety_abi! { #[doc(hidden)] extern $Abi fn() -> Ret, } + fnptr_impls_safety_abi! { #[doc(hidden)] unsafe extern $Abi fn() -> Ret, } }; ($Abi:tt, $($Arg: ident),*) => { From 8efec95bb0817f35707ae5e2f4e4d015518ef94c Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Fri, 27 May 2022 22:01:38 -0700 Subject: [PATCH 5/5] Fix test --- src/test/ui/issues/issue-59488.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/issues/issue-59488.stderr b/src/test/ui/issues/issue-59488.stderr index 76a47c49bbafb..a7863a89b2da5 100644 --- a/src/test/ui/issues/issue-59488.stderr +++ b/src/test/ui/issues/issue-59488.stderr @@ -103,7 +103,7 @@ LL | assert_eq!(Foo::Bar, i); extern "C" fn(A, B, C) -> Ret extern "C" fn(A, B, C, ...) -> Ret extern "C" fn(A, B, C, D) -> Ret - and 68 others + and 768 others = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 9 previous errors