From cb37ee2c87be982a3fab793264bbb6f81c4cd861 Mon Sep 17 00:00:00 2001 From: Benno Lossin Date: Fri, 3 Apr 2026 21:31:05 +0200 Subject: [PATCH] make field representing types invariant over the base type Reported-by: David Rheinsberg Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Multiple.20LockedBy.20with.20Single.20Lock/near/579275908 --- library/core/src/field.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/core/src/field.rs b/library/core/src/field.rs index e8ef309b9c84..0e537e2f92fc 100644 --- a/library/core/src/field.rs +++ b/library/core/src/field.rs @@ -8,7 +8,10 @@ #[expect(missing_debug_implementations)] #[fundamental] pub struct FieldRepresentingType { - _phantom: PhantomData, + // 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 T>, } // SAFETY: `FieldRepresentingType` doesn't contain any `T`