Skip to content

Commit 114dd20

Browse files
committed
Un-unsafe the StableOrd trait
Whilst incorrect implementations of this trait can cause miscompilation, they cannot cause memory unsafety in rustc.
1 parent 02c7a59 commit 114dd20

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

Diff for: compiler/rustc_abi/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ pub struct Size {
425425
raw: u64,
426426
}
427427

428-
// Safety: Ord is implement as just comparing numerical values and numerical values
428+
// Ord is implement as just comparing numerical values and numerical values
429429
// are not changed by (de-)serialization.
430430
#[cfg(feature = "nightly")]
431-
unsafe impl StableOrd for Size {
431+
impl StableOrd for Size {
432432
const CAN_USE_UNSTABLE_SORT: bool = true;
433433
}
434434

Diff for: compiler/rustc_data_structures/src/stable_hasher.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,14 @@ pub trait ToStableHashKey<HCX> {
238238
/// The associated constant `CAN_USE_UNSTABLE_SORT` denotes whether
239239
/// unstable sorting can be used for this type. Set to true if and
240240
/// 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 {
242245
const CAN_USE_UNSTABLE_SORT: bool;
243246
}
244247

245-
unsafe impl<T: StableOrd> StableOrd for &T {
248+
impl<T: StableOrd> StableOrd for &T {
246249
const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
247250
}
248251

@@ -290,7 +293,7 @@ macro_rules! impl_stable_traits_for_trivial_type {
290293
}
291294
}
292295

293-
unsafe impl $crate::stable_hasher::StableOrd for $t {
296+
impl $crate::stable_hasher::StableOrd for $t {
294297
const CAN_USE_UNSTABLE_SORT: bool = true;
295298
}
296299
};
@@ -327,7 +330,7 @@ impl<CTX> HashStable<CTX> for Hash128 {
327330
}
328331
}
329332

330-
unsafe impl StableOrd for Hash128 {
333+
impl StableOrd for Hash128 {
331334
const CAN_USE_UNSTABLE_SORT: bool = true;
332335
}
333336

@@ -392,7 +395,7 @@ impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2)
392395
}
393396
}
394397

395-
unsafe impl<T1: StableOrd, T2: StableOrd> StableOrd for (T1, T2) {
398+
impl<T1: StableOrd, T2: StableOrd> StableOrd for (T1, T2) {
396399
const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT;
397400
}
398401

@@ -410,7 +413,7 @@ where
410413
}
411414
}
412415

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) {
414417
const CAN_USE_UNSTABLE_SORT: bool =
415418
T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT && T3::CAN_USE_UNSTABLE_SORT;
416419
}
@@ -431,9 +434,7 @@ where
431434
}
432435
}
433436

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) {
437438
const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT
438439
&& T2::CAN_USE_UNSTABLE_SORT
439440
&& T3::CAN_USE_UNSTABLE_SORT
@@ -530,7 +531,7 @@ impl<CTX> HashStable<CTX> for str {
530531
}
531532
}
532533

533-
unsafe impl StableOrd for &str {
534+
impl StableOrd for &str {
534535
const CAN_USE_UNSTABLE_SORT: bool = true;
535536
}
536537

@@ -541,9 +542,9 @@ impl<CTX> HashStable<CTX> for String {
541542
}
542543
}
543544

544-
// Safety: String comparison only depends on their contents and the
545+
// String comparison only depends on their contents and the
545546
// contents are not changed by (de-)serialization.
546-
unsafe impl StableOrd for String {
547+
impl StableOrd for String {
547548
const CAN_USE_UNSTABLE_SORT: bool = true;
548549
}
549550

@@ -570,8 +571,8 @@ impl<CTX> HashStable<CTX> for bool {
570571
}
571572
}
572573

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 {
575576
const CAN_USE_UNSTABLE_SORT: bool = true;
576577
}
577578

@@ -590,8 +591,8 @@ where
590591
}
591592
}
592593

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> {
595596
const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
596597
}
597598

Diff for: compiler/rustc_hir/src/hir_id.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ impl ItemLocalId {
165165
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
166166
}
167167

168-
// Safety: Ord is implement as just comparing the ItemLocalId's numerical
168+
// Ord is implement as just comparing the ItemLocalId's numerical
169169
// values and these are not changed by (de-)serialization.
170-
unsafe impl StableOrd for ItemLocalId {
170+
impl StableOrd for ItemLocalId {
171171
const CAN_USE_UNSTABLE_SORT: bool = true;
172172
}
173173

Diff for: compiler/rustc_query_system/src/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl<HCX> ToStableHashKey<HCX> for WorkProductId {
301301
self.hash
302302
}
303303
}
304-
unsafe impl StableOrd for WorkProductId {
304+
impl StableOrd for WorkProductId {
305305
// Fingerprint can use unstable (just a tuple of `u64`s), so WorkProductId can as well
306306
const CAN_USE_UNSTABLE_SORT: bool = true;
307307
}

Diff for: compiler/rustc_session/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ pub enum OutputType {
491491
DepInfo,
492492
}
493493

494-
// Safety: Trivial C-Style enums have a stable sort order across compilation sessions.
495-
unsafe impl StableOrd for OutputType {
494+
// Trivial C-Style enums have a stable sort order across compilation sessions.
495+
impl StableOrd for OutputType {
496496
const CAN_USE_UNSTABLE_SORT: bool = true;
497497
}
498498

Diff for: compiler/rustc_span/src/def_id.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl Default for DefPathHash {
120120
}
121121
}
122122

123-
// Safety: `DefPathHash` sort order is not affected (de)serialization.
124-
unsafe impl StableOrd for DefPathHash {
123+
// `DefPathHash` sort order is not affected (de)serialization.
124+
impl StableOrd for DefPathHash {
125125
const CAN_USE_UNSTABLE_SORT: bool = true;
126126
}
127127

0 commit comments

Comments
 (0)