Skip to content

Commit 8052f73

Browse files
authored
Auto merge of #34879 - petrochenkov:fnptr, r=alexcrichton
Implement traits for variadic function pointers Closes #34874 cc #28268 r? @alexcrichton
2 parents bbfcb47 + 9c5039a commit 8052f73

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/libcore/ptr.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,21 @@ macro_rules! fnptr_impls_safety_abi {
571571
}
572572

573573
macro_rules! fnptr_impls_args {
574-
($($Arg: ident),*) => {
574+
($($Arg: ident),+) => {
575575
fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
576576
fnptr_impls_safety_abi! { extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
577+
fnptr_impls_safety_abi! { extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
577578
fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),*) -> Ret, $($Arg),* }
578579
fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),*) -> Ret, $($Arg),* }
579-
}
580+
fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),* , ...) -> Ret, $($Arg),* }
581+
};
582+
() => {
583+
// No variadic functions with 0 parameters
584+
fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, }
585+
fnptr_impls_safety_abi! { extern "C" fn() -> Ret, }
586+
fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, }
587+
fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, }
588+
};
580589
}
581590

582591
fnptr_impls_args! { }

src/libcoretest/ptr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,17 @@ fn test_unsized_unique() {
171171
let zs: &mut [i32] = &mut [1, 2, 3];
172172
assert!(ys == zs);
173173
}
174+
175+
#[test]
176+
fn test_variadic_fnptr() {
177+
use core::hash::{Hash, SipHasher};
178+
extern "C" {
179+
fn printf(_: *const u8, ...);
180+
}
181+
let p: unsafe extern "C" fn(*const u8, ...) = printf;
182+
let q = p.clone();
183+
assert_eq!(p, q);
184+
assert!(!(p < q));
185+
let mut s = SipHasher::new();
186+
assert_eq!(p.hash(&mut s), q.hash(&mut s));
187+
}

0 commit comments

Comments
 (0)