about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-01-11 21:46:43 +0000
committerbors <bors@rust-lang.org>2025-01-11 21:46:43 +0000
commit12445e0b2c532e389b8293924ed7c2b6fad5238f (patch)
tree166a0e6d520a3be59aaf0d10da0fa0e22de02287
parenteb54a50837ad4bcc9842924f27e7287ca66e294c (diff)
parent41857a3d423d3db70367da8c89f77175762967b0 (diff)
downloadrust-12445e0b2c532e389b8293924ed7c2b6fad5238f.tar.gz
rust-12445e0b2c532e389b8293924ed7c2b6fad5238f.zip
Auto merge of #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 https://github.com/rust-lang/rust/issues/92454 by providing rationale and context in the docs linked from that error message.
-rw-r--r--library/core/src/marker.rs31
1 files changed, 13 insertions, 18 deletions
diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs
index b1e67e4e900..4af9b666e54 100644
--- a/library/core/src/marker.rs
+++ b/library/core/src/marker.rs
@@ -190,24 +190,19 @@ pub trait Unsize<T: ?Sized> {
 
 /// Required trait for constants used in pattern matches.
 ///
-/// Any type that derives `PartialEq` automatically implements this trait,
-/// *regardless* of whether its type-parameters implement `PartialEq`.
-///
-/// If a `const` item contains some type that does not implement this trait,
-/// then that type either (1.) does not implement `PartialEq` (which means the
-/// constant will not provide that comparison method, which code generation
-/// assumes is available), or (2.) it implements *its own* version of
-/// `PartialEq` (which we assume does not conform to a structural-equality
-/// comparison).
-///
-/// In either of the two scenarios above, we reject usage of such a constant in
-/// a pattern match.
-///
-/// See also the [structural match RFC][RFC1445], and [issue 63438] which
-/// motivated migrating from an attribute-based design to this trait.
-///
-/// [RFC1445]: https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md
-/// [issue 63438]: https://github.com/rust-lang/rust/issues/63438
+/// Constants are only allowed as patterns if (a) their type implements
+/// `PartialEq`, and (b) interpreting the value of the constant as a pattern
+/// is equialent to calling `PartialEq`. This ensures that constants used as
+/// patterns cannot expose implementation details in an unexpected way or
+/// cause semver hazards.
+///
+/// This trait ensures point (b).
+/// Any type that derives `PartialEq` automatically implements this trait.
+///
+/// Implementing this trait (which is unstable) is a way for type authors to explicitly allow
+/// comparing const values of this type; that operation will recursively compare all fields
+/// (including private fields), even if that behavior differs from `PartialEq`. This can make it
+/// semver-breaking to add further private fields to a type.
 #[unstable(feature = "structural_match", issue = "31434")]
 #[diagnostic::on_unimplemented(message = "the type `{Self}` does not `#[derive(PartialEq)]`")]
 #[lang = "structural_peq"]