make field representing types invariant over the base type

Reported-by: David Rheinsberg <david@readahead.eu>
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Multiple.20LockedBy.20with.20Single.20Lock/near/579275908
This commit is contained in:
Benno Lossin
2026-04-03 21:31:05 +02:00
parent 2972b5e59f
commit cb37ee2c87
+4 -1
View File
@@ -8,7 +8,10 @@
#[expect(missing_debug_implementations)]
#[fundamental]
pub struct FieldRepresentingType<T: ?Sized, const VARIANT: u32, const FIELD: u32> {
_phantom: PhantomData<T>,
// We want this type to be invariant over `T`, because otherwise `field_of!(Struct<'short>,
// field)` is a subtype of `field_of!(Struct<'long>, field)`. This subtype relationship does not
// have an immediately obvious meaning and we want to prevent people from relying on it.
_phantom: PhantomData<fn(T) -> T>,
}
// SAFETY: `FieldRepresentingType` doesn't contain any `T`