Repository navigation
Implement Fn traits for safe functions of any ABI
#160186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -369,6 +369,43 @@ impl ExternAbi { | |||||||||||||||||
| | Self::Swift => true, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Returns whether safe functions with this ABI implement the `Fn` traits. | ||||||||||||||||||
| /// `false` only for uncallable or internal ABIs. | ||||||||||||||||||
| pub fn is_fn_trait_compatible(self) -> bool { | ||||||||||||||||||
| match self { | ||||||||||||||||||
| ExternAbi::C { .. } | ||||||||||||||||||
| | ExternAbi::System { .. } | ||||||||||||||||||
| | ExternAbi::Rust | ||||||||||||||||||
| | ExternAbi::RustCold | ||||||||||||||||||
| | ExternAbi::RustPreserveNone | ||||||||||||||||||
| | ExternAbi::RustTail | ||||||||||||||||||
| | ExternAbi::EfiApi | ||||||||||||||||||
| | ExternAbi::Swift | ||||||||||||||||||
| | ExternAbi::Aapcs { .. } | ||||||||||||||||||
| | ExternAbi::CmseNonSecureCall | ||||||||||||||||||
| | ExternAbi::CmseNonSecureEntry | ||||||||||||||||||
| | ExternAbi::Cdecl { .. } | ||||||||||||||||||
| | ExternAbi::Stdcall { .. } | ||||||||||||||||||
| | ExternAbi::Fastcall { .. } | ||||||||||||||||||
| | ExternAbi::Thiscall { .. } | ||||||||||||||||||
| | ExternAbi::Vectorcall { .. } | ||||||||||||||||||
| | ExternAbi::SysV64 { .. } | ||||||||||||||||||
| | ExternAbi::Win64 { .. } => true, | ||||||||||||||||||
| ExternAbi::RustCall | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need a comment explaining that RustCall implements the Fn traits, but not this way
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is actually no reason why we couldn't make rust-call
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like something worth saying explicitly?
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a comment at the top of the function saying that we make all internal ABIs |
||||||||||||||||||
| | ExternAbi::RustInvalid | ||||||||||||||||||
| | ExternAbi::LlvmIntrinsic | ||||||||||||||||||
| | ExternAbi::Custom | ||||||||||||||||||
| | ExternAbi::GpuKernel | ||||||||||||||||||
| | ExternAbi::PtxKernel | ||||||||||||||||||
| | ExternAbi::AvrInterrupt | ||||||||||||||||||
| | ExternAbi::AvrNonBlockingInterrupt | ||||||||||||||||||
| | ExternAbi::Msp430Interrupt | ||||||||||||||||||
| | ExternAbi::RiscvInterruptM | ||||||||||||||||||
| | ExternAbi::RiscvInterruptS | ||||||||||||||||||
| | ExternAbi::X86Interrupt => false, | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| pub fn all_names() -> Vec<&'static str> { | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1147,7 +1147,7 @@ impl<I: Interner> FnSig<I> { | |||||
| } | ||||||
|
|
||||||
| pub fn is_fn_trait_compatible(self) -> bool { | ||||||
| !self.c_variadic() && self.safety().is_safe() && self.abi() == ExternAbi::Rust | ||||||
| !self.c_variadic() && self.safety().is_safe() && self.abi().is_fn_trait_compatible() | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We could try to remove the |
||||||
| } | ||||||
|
|
||||||
| /// Set the safety flag. | ||||||
|
|
||||||
|
RalfJung marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //! Test implementation of `Fn` traits for functions with non-`Rust` ABI | ||
|
|
||
| use std::ops::{Fn, FnMut, FnOnce}; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| struct Foo(i32, i32); | ||
|
|
||
| extern "C" fn square(foo: Foo) -> Foo { | ||
| Foo(foo.0 * foo.1, foo.0 - foo.1) | ||
| } | ||
|
|
||
| fn call_it<F: ?Sized + Fn(Foo) -> Foo>(f: &F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn call_it_mut<F: ?Sized + FnMut(Foo) -> Foo>(f: &mut F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn call_it_once<F: FnOnce(Foo) -> Foo>(f: F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn main() { | ||
| assert_eq!(call_it(&square, Foo(20, 10)), Foo(200, 10)); | ||
| assert_eq!(call_it_mut(&mut square, Foo(21, 9)), Foo(189, 12)); | ||
| assert_eq!(call_it_once(square, Foo(18, -3)), Foo(-54, 21)); | ||
|
|
||
| let mut square_ptr: extern "C" fn(Foo) -> Foo = square; | ||
| assert_eq!(call_it(&square_ptr, Foo(30, 11)), Foo(330, 19)); | ||
| assert_eq!(call_it_mut(&mut square_ptr, Foo(273, -1)), Foo(-273, 274)); | ||
| assert_eq!(call_it_once(square_ptr, Foo(27, 27)), Foo(729, 0)); | ||
|
|
||
| let mut square_dyn: Box<dyn Fn(Foo) -> Foo> = Box::new(square); | ||
| assert_eq!(call_it(&*square_dyn, Foo(1, 3)), Foo(3, -2)); | ||
| assert_eq!(call_it_mut(&mut *square_dyn, Foo(5, 25)), Foo(125, -20)); | ||
| assert_eq!(call_it_once(square_dyn, Foo(9, 2)), Foo(18, 7)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| //@ run-pass | ||
| //@ edition: 2021 | ||
|
|
||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
|
|
||
| #[expect(improper_ctypes_definitions)] | ||
| pub extern "C" fn abi() -> Pin<Box<dyn Future<Output = ()> + 'static>> { | ||
| Box::pin(async {}) | ||
| } | ||
|
|
||
| fn test(f: impl AsyncFn()) { | ||
| let _ = async { | ||
| f().await; | ||
| f().await; | ||
| }; | ||
| } | ||
|
|
||
| fn main() { | ||
| test(abi); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| //@ run-pass | ||
|
|
||
| extern "C" fn f() { | ||
| } | ||
|
|
||
| fn is_fn<F>(_: F) where F: Fn() {} | ||
| fn is_fn<F>(f: F) where F: Fn() { | ||
| f(); | ||
| } | ||
|
|
||
| fn main() { | ||
| // extern functions are extern "C" fn | ||
| let _x: extern "C" fn() = f; // OK | ||
| is_fn(f); | ||
| //~^ ERROR expected `Fn()` signature, found `extern "C" fn() {f}` | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //@ run-pass | ||
| //! Test implementation of `Fn` traits for functions with non-`Rust` ABI | ||
|
|
||
| use std::ops::{Fn, FnMut, FnOnce}; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| struct Foo(i32, i32); | ||
|
|
||
| extern "C" fn square(foo: Foo) -> Foo { | ||
| Foo(foo.0 * foo.1, foo.0 - foo.1) | ||
| } | ||
|
|
||
| fn call_it<F: ?Sized + Fn(Foo) -> Foo>(f: &F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn call_it_mut<F: ?Sized + FnMut(Foo) -> Foo>(f: &mut F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn call_it_once<F: FnOnce(Foo) -> Foo>(f: F, i: Foo) -> Foo { | ||
| f(i) | ||
| } | ||
|
|
||
| fn main() { | ||
| assert_eq!(call_it(&square, Foo(20, 10)), Foo(200, 10)); | ||
| assert_eq!(call_it_mut(&mut square, Foo(21, 9)), Foo(189, 12)); | ||
| assert_eq!(call_it_once(square, Foo(18, -3)), Foo(-54, 21)); | ||
|
|
||
| let mut square_ptr: extern "C" fn(Foo) -> Foo = square; | ||
| assert_eq!(call_it(&square_ptr, Foo(30, 11)), Foo(330, 19)); | ||
| assert_eq!(call_it_mut(&mut square_ptr, Foo(273, -1)), Foo(-273, 274)); | ||
| assert_eq!(call_it_once(square_ptr, Foo(27, 27)), Foo(729, 0)); | ||
|
|
||
| let mut square_dyn: Box<dyn Fn(Foo) -> Foo> = Box::new(square); | ||
| assert_eq!(call_it(&*square_dyn, Foo(1, 3)), Foo(3, -2)); | ||
| assert_eq!(call_it_mut(&mut *square_dyn, Foo(5, 25)), Foo(125, -20)); | ||
| assert_eq!(call_it_once(square_dyn, Foo(9, 2)), Foo(18, 7)); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.