@@ -238,11 +238,14 @@ pub trait ToStableHashKey<HCX> {
238
238
/// The associated constant `CAN_USE_UNSTABLE_SORT` denotes whether
239
239
/// unstable sorting can be used for this type. Set to true if and
240
240
/// only if `a == b` implies `a` and `b` are fully indistinguishable.
241
- pub unsafe trait StableOrd : Ord {
241
+ ///
242
+ /// **Be careful when implementing this trait, as an incorrect
243
+ /// implementation can cause miscompilation!**
244
+ pub trait StableOrd : Ord {
242
245
const CAN_USE_UNSTABLE_SORT : bool ;
243
246
}
244
247
245
- unsafe impl < T : StableOrd > StableOrd for & T {
248
+ impl < T : StableOrd > StableOrd for & T {
246
249
const CAN_USE_UNSTABLE_SORT : bool = T :: CAN_USE_UNSTABLE_SORT ;
247
250
}
248
251
@@ -290,7 +293,7 @@ macro_rules! impl_stable_traits_for_trivial_type {
290
293
}
291
294
}
292
295
293
- unsafe impl $crate:: stable_hasher:: StableOrd for $t {
296
+ impl $crate:: stable_hasher:: StableOrd for $t {
294
297
const CAN_USE_UNSTABLE_SORT : bool = true ;
295
298
}
296
299
} ;
@@ -327,7 +330,7 @@ impl<CTX> HashStable<CTX> for Hash128 {
327
330
}
328
331
}
329
332
330
- unsafe impl StableOrd for Hash128 {
333
+ impl StableOrd for Hash128 {
331
334
const CAN_USE_UNSTABLE_SORT : bool = true ;
332
335
}
333
336
@@ -392,7 +395,7 @@ impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2)
392
395
}
393
396
}
394
397
395
- unsafe impl < T1 : StableOrd , T2 : StableOrd > StableOrd for ( T1 , T2 ) {
398
+ impl < T1 : StableOrd , T2 : StableOrd > StableOrd for ( T1 , T2 ) {
396
399
const CAN_USE_UNSTABLE_SORT : bool = T1 :: CAN_USE_UNSTABLE_SORT && T2 :: CAN_USE_UNSTABLE_SORT ;
397
400
}
398
401
@@ -410,7 +413,7 @@ where
410
413
}
411
414
}
412
415
413
- unsafe impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd > StableOrd for ( T1 , T2 , T3 ) {
416
+ impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd > StableOrd for ( T1 , T2 , T3 ) {
414
417
const CAN_USE_UNSTABLE_SORT : bool =
415
418
T1 :: CAN_USE_UNSTABLE_SORT && T2 :: CAN_USE_UNSTABLE_SORT && T3 :: CAN_USE_UNSTABLE_SORT ;
416
419
}
@@ -431,9 +434,7 @@ where
431
434
}
432
435
}
433
436
434
- unsafe impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd , T4 : StableOrd > StableOrd
435
- for ( T1 , T2 , T3 , T4 )
436
- {
437
+ impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd , T4 : StableOrd > StableOrd for ( T1 , T2 , T3 , T4 ) {
437
438
const CAN_USE_UNSTABLE_SORT : bool = T1 :: CAN_USE_UNSTABLE_SORT
438
439
&& T2 :: CAN_USE_UNSTABLE_SORT
439
440
&& T3 :: CAN_USE_UNSTABLE_SORT
@@ -530,7 +531,7 @@ impl<CTX> HashStable<CTX> for str {
530
531
}
531
532
}
532
533
533
- unsafe impl StableOrd for & str {
534
+ impl StableOrd for & str {
534
535
const CAN_USE_UNSTABLE_SORT : bool = true ;
535
536
}
536
537
@@ -541,9 +542,9 @@ impl<CTX> HashStable<CTX> for String {
541
542
}
542
543
}
543
544
544
- // Safety: String comparison only depends on their contents and the
545
+ // String comparison only depends on their contents and the
545
546
// contents are not changed by (de-)serialization.
546
- unsafe impl StableOrd for String {
547
+ impl StableOrd for String {
547
548
const CAN_USE_UNSTABLE_SORT : bool = true ;
548
549
}
549
550
@@ -570,8 +571,8 @@ impl<CTX> HashStable<CTX> for bool {
570
571
}
571
572
}
572
573
573
- // Safety: sort order of bools is not changed by (de-)serialization.
574
- unsafe impl StableOrd for bool {
574
+ // sort order of bools is not changed by (de-)serialization.
575
+ impl StableOrd for bool {
575
576
const CAN_USE_UNSTABLE_SORT : bool = true ;
576
577
}
577
578
@@ -590,8 +591,8 @@ where
590
591
}
591
592
}
592
593
593
- // Safety: the Option wrapper does not add instability to comparison.
594
- unsafe impl < T : StableOrd > StableOrd for Option < T > {
594
+ // the Option wrapper does not add instability to comparison.
595
+ impl < T : StableOrd > StableOrd for Option < T > {
595
596
const CAN_USE_UNSTABLE_SORT : bool = T :: CAN_USE_UNSTABLE_SORT ;
596
597
}
597
598
0 commit comments