rust icon indicating copy to clipboard operation
rust copied to clipboard

lint: change help for pointers to dyn types in FFI

Open niacdoial opened this issue 4 months ago • 18 comments

Context

while playing around, I encountered the warning for dyn types in extern "C" functions, but even after that I assumed that a (rust) raw pointer could be interpreted in C ('s ABI) as a void *. (...to be fair part of why I ignored the warning is because I wanted to poke at the generated assembly, not make useful code)

Remaining considerations

  • The information in the new warning message only appears when raw pointers to dyn types appear. Should it appear in other cases too? (references? Boxes too, eventhough they already have a nicer warning, "box cannot be represented as a single pointer"?)
  • Is the hint added in the new warning well-formulated? Understandable enough?
  • likely other things

Possible alternatives to this PR

  • changing the overall "dyn types in FFI" message to also talk about raw pointers
  • add the hint for passing opaque pointers for the overall "dyn types in FFI" message.
  • probably other solutions
  • if the current warning is fine (and it was a skill issue on my part), just disregard this.

Example

example code:

extern "C"
fn caller(callee: *const dyn Fn(i32)->i32){
    // -- snip --
}

old warning:

warning: `extern` fn uses type `dyn Fn(i32) -> i32`, which is not FFI-safe
 --> file/name.rs:42:19
   |
42 | fn caller(callee: *const dyn Fn(i32)->i32) {
   |                   ^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
   |
  = note: trait objects have no C equivalent
  = note: `#[warn(improper_ctypes_definitions)]` on by default

new warning:

warning: `extern` fn uses type `dyn Fn(i32) -> i32`, which is not FFI-safe
 --> file/name.rs:42:19
   |
42 | fn caller(callee: *const dyn Fn(i32)->i32) -> (){
   |                   ^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
   |
  = help: to pass an opaque pointer through a FFI boundary, consider replacing `*const dyn ...` by `*const & dyn ...` (or `*mut dyn ...` by `*const &mut dyn ...`)
  = note: pointers to trait objects have no C equivalent
  = note: `#[warn(improper_ctypes_definitions)]` on by default

niacdoial avatar Oct 13 '24 22:10 niacdoial