-
Notifications
You must be signed in to change notification settings - Fork 13.3k
improper_ctypes complains about types that contain PhantomData #34798
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The problem, as far as I know, is that This is because Zero Sized Types (which |
it also happens (although with a slightly different message) when the type is not ZST: https://is.gd/8529c8
|
Unfortunately, we need to use phantom data to use bindgen with C++, because a struct like: template<typename T>
struct Foo {
int bar;
}; The only way we can represent that in rust is struct Foo<T> {
bar: c_int,
_phantom: PhantomData<T>,
} |
I've run into this issue with something similar use std::marker::PhantomData;
fn main() {
let buffer = vec![0u8; 16];
let data = Data {
ptr: &buffer[0],
phantom: PhantomData,
};
unsafe { bogus(&data) };
}
#[repr(C)]
struct Data<'a> {
ptr: *const u8,
phantom: PhantomData<&'a [u8]>,
}
extern "C" {
fn bogus(data: *const Data);
} which generates the same kind of warning:
My real use-case has other fields in the struct, which are needed to match the C API. |
I did a little poking around to understand how unsafe this is. In https://play.rust-lang.org/?gist=24634425b2bd8707c73f09e418900ec5 you can see that the However, it is reported in debuginfo. So while passing its address across the ffi boundary is I think covered by the existing safety rules, a debugger could be confused by the duplicate labels on the struct. |
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
The linked issue rust-lang/rust#34798 has since been fixed, and rust will not warn if PhantomData is used in FFI structs. See also: rust-lang/rust#39462
* Re-enable improper_ctypes lint on bindings The linked issue rust-lang/rust#34798 has since been fixed, and rust will not warn if PhantomData is used in FFI structs. See also: rust-lang/rust#39462 * Update ci.yml Fixes warnings on github actions about updating to Node v20 by updating the used actions. actions-rs/toolchain is not maintained anymore, switch to dtolnay/rust-toolchain instead.
This comes up in bindings generated from C code that contains unions (the
__BindgenUnionField
type).The text was updated successfully, but these errors were encountered: