Skip to content

Commit c2f228f

Browse files
authored
Rollup merge of #115607 - RalfJung:safe-traits-unsafe-code, r=dtolnay
clarify that unsafe code must not rely on our safe traits This adds a disclaimer to PartialEq, Eq, PartialOrd, Ord, Hash, Deref, DerefMut. We already have a similar disclaimer in ExactSizeIterator (worded a bit differently): ``` /// Note that this trait is a safe trait and as such does *not* and *cannot* /// guarantee that the returned length is correct. This means that `unsafe` /// code **must not** rely on the correctness of [`Iterator::size_hint`]. The /// unstable and unsafe [`TrustedLen`](super::marker::TrustedLen) trait gives /// this additional guarantee. ``` If there are any other traits that should carry such a disclaimer, please let me know. Fixes #73682
2 parents 790309b + 6211114 commit c2f228f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Diff for: library/core/src/cmp.rs

+20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ use self::Ordering::*;
6363
/// (transitive) impls are not forced to exist, but these requirements apply
6464
/// whenever they do exist.
6565
///
66+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
67+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
68+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
69+
/// methods.
70+
///
6671
/// ## Derivable
6772
///
6873
/// This trait can be used with `#[derive]`. When `derive`d on structs, two
@@ -250,6 +255,11 @@ pub macro PartialEq($item:item) {
250255
/// This property cannot be checked by the compiler, and therefore `Eq` implies
251256
/// [`PartialEq`], and has no extra methods.
252257
///
258+
/// Violating this property is a logic error. The behavior resulting from a logic error is not
259+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
260+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
261+
/// methods.
262+
///
253263
/// ## Derivable
254264
///
255265
/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has
@@ -659,6 +669,11 @@ impl<T: Clone> Clone for Reverse<T> {
659669
/// It's easy to accidentally make `cmp` and `partial_cmp` disagree by
660670
/// deriving some of the traits and manually implementing others.
661671
///
672+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
673+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
674+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
675+
/// methods.
676+
///
662677
/// ## Corollaries
663678
///
664679
/// From the above and the requirements of `PartialOrd`, it follows that `<` defines a strict total order.
@@ -892,6 +907,11 @@ pub macro Ord($item:item) {
892907
/// transitively: if `T: PartialOrd<U>` and `U: PartialOrd<V>` then `U: PartialOrd<T>` and `T:
893908
/// PartialOrd<V>`.
894909
///
910+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
911+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
912+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
913+
/// methods.
914+
///
895915
/// ## Corollaries
896916
///
897917
/// The following corollaries follow from the above requirements:

Diff for: library/core/src/hash/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ mod sip;
153153
/// Thankfully, you won't need to worry about upholding this property when
154154
/// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
155155
///
156+
/// Violating this property is a logic error. The behavior resulting from a logic error is not
157+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
158+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
159+
/// methods.
160+
///
156161
/// ## Prefix collisions
157162
///
158163
/// Implementations of `hash` should ensure that the data they

Diff for: library/core/src/ops/deref.rs

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
/// For similar reasons, **this trait should never fail**. Failure during
1515
/// dereferencing can be extremely confusing when `Deref` is invoked implicitly.
1616
///
17+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
18+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
19+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of this
20+
/// method.
21+
///
1722
/// # More on `Deref` coercion
1823
///
1924
/// If `T` implements `Deref<Target = U>`, and `x` is a value of type `T`, then:
@@ -114,6 +119,11 @@ impl<T: ?Sized> Deref for &mut T {
114119
/// dereferencing can be extremely confusing when `DerefMut` is invoked
115120
/// implicitly.
116121
///
122+
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
123+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
124+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of this
125+
/// method.
126+
///
117127
/// # More on `Deref` coercion
118128
///
119129
/// If `T` implements `DerefMut<Target = U>`, and `x` is a value of type `T`,

0 commit comments

Comments
 (0)