Skip to content

Commit

Permalink
Handle pointer cast of non-ffi-safe pointer target types
Browse files Browse the repository at this point in the history
Previously:

    error: `extern` block uses type `String`, which is not FFI-safe
      --> src/main.rs:13:27
       |
    13 |         unsafe fn test(x: *mut String);
       |                           ^^^^^^^^^^^ not FFI-safe
  • Loading branch information
dtolnay committed Mar 23, 2021
1 parent dec9392 commit 2982d73
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
},
_ => quote!(#var),
},
Type::Ptr(ty) => {
if types.is_considered_improper_ctype(&ty.inner) {
quote!(#var.cast())
} else {
quote!(#var)
}
}
Type::Str(_) => quote!(::cxx::private::RustStr::from(#var)),
Type::SliceRef(ty) => match ty.mutable {
false => quote!(::cxx::private::RustSlice::from_ref(#var)),
Expand Down Expand Up @@ -608,6 +615,13 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
}
_ => call,
},
Type::Ptr(ty) => {
if types.is_considered_improper_ctype(&ty.inner) {
quote!(#call.cast())
} else {
call
}
}
Type::Str(_) => quote!(#call.as_str()),
Type::SliceRef(slice) => {
let inner = &slice.inner;
Expand Down Expand Up @@ -1572,6 +1586,15 @@ fn expand_extern_type(ty: &Type, types: &Types, proper: bool) -> TokenStream {
_ => quote!(#ty),
}
}
Type::Ptr(ty) => {
if proper && types.is_considered_improper_ctype(&ty.inner) {
let mutability = ty.mutability;
let constness = ty.constness;
quote!(*#mutability #constness ::std::ffi::c_void)
} else {
quote!(#ty)
}
}
Type::Str(_) => quote!(::cxx::private::RustStr),
Type::SliceRef(_) => quote!(::cxx::private::RustSlice),
_ => quote!(#ty),
Expand Down

0 comments on commit 2982d73

Please sign in to comment.