cxx icon indicating copy to clipboard operation
cxx copied to clipboard

Function with no arguments and *mut T return value does not compile

Open djmarcin opened this issue 3 years ago • 1 comments

We have a function with this signature pub unsafe fn get_current_span() -> *mut api::OptionSpan and we include it in the Rust portion of our cxxbridge module. This gives the following error.

error[E0277]: expected a `FnOnce<()>` closure, found `unsafe fn() -> *mut api::OptionSpan {_::__get_current_span::__get_current_span}`
--> node/sdk/cxx.rs:8:1
   |
8  | / pub mod ffi {
9  | |     #![allow(clippy::too_many_arguments)]
10 | |
11 | |     struct Parameter {
...  |
81 | |     }
82 | | }
   | |_^ expected an `FnOnce<()>` closure, found `unsafe fn() -> *mut api::OptionSpan {_::__get_current_span::__get_current_span}`
   |
::: ./external/raze__cxx__1_0_49/src/unwind.rs:9:8
   |
9  |       F: FnOnce() -> R,
   |          ------------- required by this bound in `cxx::private::catch_unwind`
   |
= help: the trait `FnOnce<()>` is not implemented for `unsafe fn() -> *mut api::OptionSpan {_::__get_current_span::__get_current_span}`
= note: wrap the `unsafe fn() -> *mut api::OptionSpan {_::__get_current_span::__get_current_span}` in a closure with no arguments: `|| { /* code */ }`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

If we change the signature to pub unsafe fn get_current_span(_: u32) -> *mut api::OptionSpan or pub unsafe fn get_current_span() then compilation works.

djmarcin avatar Jun 23 '21 23:06 djmarcin

The problem here was that get_current_span() did not actually need to be unsafe and somehow the proc macro "knows" this and chokes on it, but doesn't give a very good error message.

djmarcin avatar Jun 25 '21 06:06 djmarcin