From 7269b96f233f5ee19548da412bd6e93743f20f61 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Wed, 8 Jul 2026 22:23:58 -0700 Subject: [PATCH] [compiler] Implement `PartialOrd` via `Ord` for `Span` and newtype_indexes For something unconditionally `Ord`, this is better than using `partial_cmp` on fields. I couldn't find any other cases in the compiler codebase of this, but it's entirely possible I missed some. Let me know if you can think of any. --- compiler/rustc_index_macros/src/newtype.rs | 4 +++- compiler/rustc_span/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_index_macros/src/newtype.rs b/compiler/rustc_index_macros/src/newtype.rs index 4553cf026bb08..9fe2b4caa7229 100644 --- a/compiler/rustc_index_macros/src/newtype.rs +++ b/compiler/rustc_index_macros/src/newtype.rs @@ -138,13 +138,15 @@ impl Parse for Newtype { } } impl ::std::cmp::Ord for #name { + #[inline] fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.as_u32().cmp(&other.as_u32()) } } impl ::std::cmp::PartialOrd for #name { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { - self.as_u32().partial_cmp(&other.as_u32()) + Some(self.cmp(other)) } } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 5015741f10c5f..ae01956be9301 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -738,7 +738,7 @@ impl Default for SpanData { impl PartialOrd for Span { fn partial_cmp(&self, rhs: &Self) -> Option { - PartialOrd::partial_cmp(&self.data(), &rhs.data()) + Some(self.cmp(rhs)) } } impl Ord for Span {