Skip to content

Commit 3da287a

Browse files
committed
impl !PartialOrd for HirId
1 parent f59ebe3 commit 3da287a

File tree

8 files changed

+33
-38
lines changed

8 files changed

+33
-38
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,7 @@ dependencies = [
39463946
name = "rustc_lint_defs"
39473947
version = "0.0.0"
39483948
dependencies = [
3949+
"derive-where",
39493950
"rustc_abi",
39503951
"rustc_ast",
39513952
"rustc_data_structures",

compiler/rustc_driver_impl/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,11 @@ Available lint options:
903903

904904
fn sort_lints(sess: &Session, mut lints: Vec<&'static Lint>) -> Vec<&'static Lint> {
905905
// The sort doesn't case-fold but it's doubtful we care.
906-
lints.sort_by_cached_key(|x: &&Lint| (x.default_level(sess.edition()), x.name));
906+
lints.sort_by(|a, b| {
907+
(a.default_level(sess.edition()), a.name)
908+
.partial_cmp(&(b.default_level(sess.edition()), b.name))
909+
.unwrap()
910+
});
907911
lints
908912
}
909913

compiler/rustc_hir/src/hir_id.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ pub struct HirId {
8383
pub local_id: ItemLocalId,
8484
}
8585

86+
// To ensure correctness of incremental compilation,
87+
// `HirId` must not implement `Ord` or `PartialOrd`.
88+
// See https://github.com./rust-lang/rust/issues/90317.
89+
impl !Ord for HirId {}
90+
impl !PartialOrd for HirId {}
91+
8692
impl Debug for HirId {
8793
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8894
// Example: HirId(DefId(0:1 ~ aa[7697]::{use#0}).10)
@@ -128,18 +134,6 @@ impl fmt::Display for HirId {
128134
}
129135
}
130136

131-
impl Ord for HirId {
132-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
133-
(self.index()).cmp(&(other.index()))
134-
}
135-
}
136-
137-
impl PartialOrd for HirId {
138-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
139-
Some(self.cmp(other))
140-
}
141-
}
142-
143137
rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
144138
rustc_data_structures::define_id_collections!(
145139
ItemLocalMap,

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(debug_closure_helpers)]
1212
#![feature(exhaustive_patterns)]
1313
#![feature(let_chains)]
14+
#![feature(negative_impls)]
1415
#![feature(never_type)]
1516
#![feature(rustc_attrs)]
1617
#![feature(variant_count)]

compiler/rustc_lint_defs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
derive-where = "1.2.7"
89
rustc_abi = { path = "../rustc_abi" }
910
rustc_ast = { path = "../rustc_ast" }
1011
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_lint_defs/src/lib.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use derive_where::derive_where;
12
use rustc_abi::ExternAbi;
23
use rustc_ast::AttrId;
34
use rustc_ast::attr::AttributeExt;
@@ -8,7 +9,8 @@ use rustc_data_structures::stable_hasher::{
89
};
910
use rustc_error_messages::{DiagMessage, MultiSpan};
1011
use rustc_hir::def::Namespace;
11-
use rustc_hir::{HashStableContext, HirId, MissingLifetimeKind};
12+
use rustc_hir::def_id::DefPathHash;
13+
use rustc_hir::{HashStableContext, HirId, ItemLocalId, MissingLifetimeKind};
1214
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1315
pub use rustc_span::edition::Edition;
1416
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
@@ -102,7 +104,7 @@ pub enum Applicability {
102104
/// The index values have a type of `u16` to reduce the size of the `LintExpectationId`.
103105
/// It's reasonable to assume that no user will define 2^16 attributes on one node or
104106
/// have that amount of lints listed. `u16` values should therefore suffice.
105-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Encodable, Decodable)]
107+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Encodable, Decodable)]
106108
pub enum LintExpectationId {
107109
/// Used for lints emitted during the `EarlyLintPass`. This id is not
108110
/// hash stable and should not be cached.
@@ -156,13 +158,14 @@ impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId {
156158
}
157159

158160
impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectationId {
159-
type KeyType = (HirId, u16, u16);
161+
type KeyType = (DefPathHash, ItemLocalId, u16, u16);
160162

161163
#[inline]
162-
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
164+
fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType {
163165
match self {
164166
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
165-
(*hir_id, *attr_index, *lint_index)
167+
let (def_path_hash, lint_idx) = hir_id.to_stable_hash_key(hcx);
168+
(def_path_hash, lint_idx, *attr_index, *lint_index)
166169
}
167170
_ => {
168171
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
@@ -174,19 +177,8 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
174177
/// Setting for how to handle a lint.
175178
///
176179
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
177-
#[derive(
178-
Clone,
179-
Copy,
180-
PartialEq,
181-
PartialOrd,
182-
Eq,
183-
Ord,
184-
Debug,
185-
Hash,
186-
Encodable,
187-
Decodable,
188-
HashStable_Generic
189-
)]
180+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Encodable, Decodable, HashStable_Generic)]
181+
#[derive_where(PartialOrd)]
190182
pub enum Level {
191183
/// The `allow` level will not issue any message.
192184
Allow,
@@ -201,7 +193,7 @@ pub enum Level {
201193
///
202194
/// The [`LintExpectationId`] is used to later link a lint emission to the actual
203195
/// expectation. It can be ignored in most cases.
204-
Expect(LintExpectationId),
196+
Expect(#[derive_where(skip)] LintExpectationId),
205197
/// The `warn` level will produce a warning if the lint was violated, however the
206198
/// compiler will continue with its execution.
207199
Warn,
@@ -211,7 +203,7 @@ pub enum Level {
211203
///
212204
/// The [`LintExpectationId`] is intended to fulfill expectations marked via the
213205
/// `#[expect]` attribute, that will still be suppressed due to the level.
214-
ForceWarn(Option<LintExpectationId>),
206+
ForceWarn(#[derive_where(skip)] Option<LintExpectationId>),
215207
/// The `deny` level will produce an error and stop further execution after the lint
216208
/// pass is complete.
217209
Deny,
@@ -294,6 +286,10 @@ impl Level {
294286
_ => None,
295287
}
296288
}
289+
290+
pub fn min(self, other: Self) -> Self {
291+
if self < other { self } else { other }
292+
}
297293
}
298294

299295
/// Specification of a single lint.

compiler/rustc_middle/src/lint.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::cmp;
2-
31
use rustc_data_structures::fx::FxIndexMap;
42
use rustc_data_structures::sorted_map::SortedMap;
53
use rustc_errors::{Diag, MultiSpan};
@@ -100,12 +98,12 @@ pub fn reveal_actual_level(
10098
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
10199
level
102100
} else {
103-
cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
101+
level.min(sess.opts.lint_cap.unwrap_or(Level::Forbid))
104102
};
105103

106104
if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
107105
// Ensure that we never exceed driver level.
108-
level = cmp::min(*driver_level, level);
106+
level = driver_level.min(level);
109107
}
110108

111109
level

compiler/rustc_passes/src/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11311131
if dead_codes.is_empty() {
11321132
return;
11331133
}
1134-
dead_codes.sort_by_key(|v| v.level);
1134+
dead_codes.sort_by(|a, b| a.level.partial_cmp(&b.level).unwrap());
11351135
for group in dead_codes.chunk_by(|a, b| a.level == b.level) {
11361136
self.lint_at_single_level(&group, participle, Some(def_id), report_on);
11371137
}

0 commit comments

Comments
 (0)