@@ -233,7 +233,17 @@ pub trait ToStableHashKey<HCX> {
233
233
/// - `DefIndex`, `CrateNum`, `LocalDefId`, because their concrete
234
234
/// values depend on state that might be different between
235
235
/// compilation sessions.
236
- pub unsafe trait StableOrd : Ord { }
236
+ ///
237
+ /// The associated constant `CAN_USE_UNSTABLE_SORT` denotes whether
238
+ /// unstable sorting can be used for this type. Set to true if and
239
+ /// only if `a == b` implies `a` and `b` are fully indistinguishable.
240
+ pub unsafe trait StableOrd : Ord {
241
+ const CAN_USE_UNSTABLE_SORT : bool ;
242
+ }
243
+
244
+ unsafe impl < T : StableOrd > StableOrd for & T {
245
+ const CAN_USE_UNSTABLE_SORT : bool = T :: CAN_USE_UNSTABLE_SORT ;
246
+ }
237
247
238
248
/// Implement HashStable by just calling `Hash::hash()`. Also implement `StableOrd` for the type since
239
249
/// that has the same requirements.
@@ -253,7 +263,9 @@ macro_rules! impl_stable_traits_for_trivial_type {
253
263
}
254
264
}
255
265
256
- unsafe impl $crate:: stable_hasher:: StableOrd for $t { }
266
+ unsafe impl $crate:: stable_hasher:: StableOrd for $t {
267
+ const CAN_USE_UNSTABLE_SORT : bool = true ;
268
+ }
257
269
} ;
258
270
}
259
271
@@ -339,7 +351,9 @@ impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2)
339
351
}
340
352
}
341
353
342
- unsafe impl < T1 : StableOrd , T2 : StableOrd > StableOrd for ( T1 , T2 ) { }
354
+ unsafe impl < T1 : StableOrd , T2 : StableOrd > StableOrd for ( T1 , T2 ) {
355
+ const CAN_USE_UNSTABLE_SORT : bool = T1 :: CAN_USE_UNSTABLE_SORT && T2 :: CAN_USE_UNSTABLE_SORT ;
356
+ }
343
357
344
358
impl < T1 , T2 , T3 , CTX > HashStable < CTX > for ( T1 , T2 , T3 )
345
359
where
@@ -355,7 +369,10 @@ where
355
369
}
356
370
}
357
371
358
- unsafe impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd > StableOrd for ( T1 , T2 , T3 ) { }
372
+ unsafe impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd > StableOrd for ( T1 , T2 , T3 ) {
373
+ const CAN_USE_UNSTABLE_SORT : bool =
374
+ T1 :: CAN_USE_UNSTABLE_SORT && T2 :: CAN_USE_UNSTABLE_SORT && T3 :: CAN_USE_UNSTABLE_SORT ;
375
+ }
359
376
360
377
impl < T1 , T2 , T3 , T4 , CTX > HashStable < CTX > for ( T1 , T2 , T3 , T4 )
361
378
where
@@ -376,6 +393,10 @@ where
376
393
unsafe impl < T1 : StableOrd , T2 : StableOrd , T3 : StableOrd , T4 : StableOrd > StableOrd
377
394
for ( T1 , T2 , T3 , T4 )
378
395
{
396
+ const CAN_USE_UNSTABLE_SORT : bool = T1 :: CAN_USE_UNSTABLE_SORT
397
+ && T2 :: CAN_USE_UNSTABLE_SORT
398
+ && T3 :: CAN_USE_UNSTABLE_SORT
399
+ && T4 :: CAN_USE_UNSTABLE_SORT ;
379
400
}
380
401
381
402
impl < T : HashStable < CTX > , CTX > HashStable < CTX > for [ T ] {
@@ -468,7 +489,9 @@ impl<CTX> HashStable<CTX> for str {
468
489
}
469
490
}
470
491
471
- unsafe impl StableOrd for & str { }
492
+ unsafe impl StableOrd for & str {
493
+ const CAN_USE_UNSTABLE_SORT : bool = true ;
494
+ }
472
495
473
496
impl < CTX > HashStable < CTX > for String {
474
497
#[ inline]
@@ -479,7 +502,9 @@ impl<CTX> HashStable<CTX> for String {
479
502
480
503
// Safety: String comparison only depends on their contents and the
481
504
// contents are not changed by (de-)serialization.
482
- unsafe impl StableOrd for String { }
505
+ unsafe impl StableOrd for String {
506
+ const CAN_USE_UNSTABLE_SORT : bool = true ;
507
+ }
483
508
484
509
impl < HCX > ToStableHashKey < HCX > for String {
485
510
type KeyType = String ;
@@ -505,7 +530,9 @@ impl<CTX> HashStable<CTX> for bool {
505
530
}
506
531
507
532
// Safety: sort order of bools is not changed by (de-)serialization.
508
- unsafe impl StableOrd for bool { }
533
+ unsafe impl StableOrd for bool {
534
+ const CAN_USE_UNSTABLE_SORT : bool = true ;
535
+ }
509
536
510
537
impl < T , CTX > HashStable < CTX > for Option < T >
511
538
where
@@ -523,7 +550,9 @@ where
523
550
}
524
551
525
552
// Safety: the Option wrapper does not add instability to comparison.
526
- unsafe impl < T : StableOrd > StableOrd for Option < T > { }
553
+ unsafe impl < T : StableOrd > StableOrd for Option < T > {
554
+ const CAN_USE_UNSTABLE_SORT : bool = T :: CAN_USE_UNSTABLE_SORT ;
555
+ }
527
556
528
557
impl < T1 , T2 , CTX > HashStable < CTX > for Result < T1 , T2 >
529
558
where
0 commit comments