improper_ctypes
lint fires on #[repr(transparent)]
wrappers around PhantomData
(in struct fields).
#106629
Labels
A-FFI
Area: Foreign function interface (FFI)
A-lint
Area: Lints (warnings about flaws in source code) such as unused_mut.
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
If I have a
#[repr(transparent)]
wrapper around aPhantomData
, it's considered an improper C type when used in a struct field, even though the wrappedPhantomData
would be allowed. Specifically (playground)produces the error
This is over-zealous for a few reasons.
PhantomData
on its own is allowed in C structs, so changing this to_marker: PhantomData<u8>
would solve the warning (but defeats the point of my use case1).#[repr(transparent)]
is supposed to be treated as the inner type for purposes of ABI, and theimproper_ctypes
lint is about ABI.Using
#[repr(C)]
instead of#[repr(transparent)]
solves this technically (it shuts the warning up), but seems pretty dodgy, since it's not actually laid out the way C would lay it out anymore (that is, we've guaranteed thatPhantomData
is never going to impact layout at all, and we haven't guaranteed this for zero-sized#[repr(C)]
types which are notably not a real thing in C).TLDR: this lint fires on
#[repr(transparent)]
wrappers aroundPhantomData
when they're used as struct fields. IMO these should be fine sincePhantomData
doesn't emit a warning in that case, and#[repr(transparent)]
is supposed to defer to the inner type.(That said, firing this lint on a type passed by pointer is pretty weird anyway, since the indirection means doesn't actually have to be a valid C type (for the ABI), which is the basic complaint behind #66373, which is not what I'm complaining about. IMO this shouldn't fire even if
Bar
were being passed by value, as making the fieldPhantomData
(instead of a wrapper around one) doesn't fire in that case)Footnotes
If you're curious about the real use case I have, a closer but still-basically-minimized version is here. It's important that the type not be a direct
PhantomData
(basically#[non_exhaustive]
is not really usable for FFI, but C has cases where considers appending new fields to the end of a type to be a non-breaking change). ↩The text was updated successfully, but these errors were encountered: