PhantomData is essentially a variance (and marker traits) annotation. It affects the ways in which the consumers of a type may use it (it’s effectively part of the API).
I think we should have a lint where, if a private field has type
PhantomData, we suggest making that field public instead. Given that
PhantomData is a ZST with no information content, this doesn’t really provide any kind of additional capability to users of the type. It merely makes the field show up in
rustdoc, so that readers of the documentation can learn how the type will behave, and highlights that it is part of the public API contract. If the field is public, then changing its type will seem like a semver-breaking change, which is good and right, because changing variance or marker traits is a semver-breaking change.
If the given field was the only private field in the
struct, making it public would allow users to construct it and exhaustively pattern match on it, which they couldn’t before, which is clearly bad. The lint should take this into account and suggest adding a separate private dummy field (probably of type
()) in these cases. I don’t know how common this is though? My sense is that in most of the cases where you have a reason to use
PhantomData, you also have another field which is private and contains, usually, a raw pointer.