Skip to content

Commit 12445e0

Browse files
committed
Auto merge of rust-lang#135360 - RalfJung:structural-partial-eq, r=compiler-errors
update and clarify StructuralPartialEq docs This apparently hasn't been updated when we finalized the current const pattern matching behavior. Fixes rust-lang#92454 by providing rationale and context in the docs linked from that error message.
2 parents eb54a50 + 41857a3 commit 12445e0

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

library/core/src/marker.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,19 @@ pub trait Unsize<T: ?Sized> {
190190

191191
/// Required trait for constants used in pattern matches.
192192
///
193-
/// Any type that derives `PartialEq` automatically implements this trait,
194-
/// *regardless* of whether its type-parameters implement `PartialEq`.
195-
///
196-
/// If a `const` item contains some type that does not implement this trait,
197-
/// then that type either (1.) does not implement `PartialEq` (which means the
198-
/// constant will not provide that comparison method, which code generation
199-
/// assumes is available), or (2.) it implements *its own* version of
200-
/// `PartialEq` (which we assume does not conform to a structural-equality
201-
/// comparison).
202-
///
203-
/// In either of the two scenarios above, we reject usage of such a constant in
204-
/// a pattern match.
205-
///
206-
/// See also the [structural match RFC][RFC1445], and [issue 63438] which
207-
/// motivated migrating from an attribute-based design to this trait.
208-
///
209-
/// [RFC1445]: https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md
210-
/// [issue 63438]: https://github.com/rust-lang/rust/issues/63438
193+
/// Constants are only allowed as patterns if (a) their type implements
194+
/// `PartialEq`, and (b) interpreting the value of the constant as a pattern
195+
/// is equialent to calling `PartialEq`. This ensures that constants used as
196+
/// patterns cannot expose implementation details in an unexpected way or
197+
/// cause semver hazards.
198+
///
199+
/// This trait ensures point (b).
200+
/// Any type that derives `PartialEq` automatically implements this trait.
201+
///
202+
/// Implementing this trait (which is unstable) is a way for type authors to explicitly allow
203+
/// comparing const values of this type; that operation will recursively compare all fields
204+
/// (including private fields), even if that behavior differs from `PartialEq`. This can make it
205+
/// semver-breaking to add further private fields to a type.
211206
#[unstable(feature = "structural_match", issue = "31434")]
212207
#[diagnostic::on_unimplemented(message = "the type `{Self}` does not `#[derive(PartialEq)]`")]
213208
#[lang = "structural_peq"]

0 commit comments

Comments
 (0)