Skip to content

Commit e983092

Browse files
committed
Auto merge of rust-lang#90408 - pierwill:untrack-localdefid-90317, r=cjgillot
Remove `PartialOrd`, `Ord` from `LocalDefId` Part of work on rust-lang#90317.
2 parents 34926f0 + e6ff0ba commit e983092

File tree

13 files changed

+87
-31
lines changed

13 files changed

+87
-31
lines changed

compiler/rustc_data_structures/src/graph/scc/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::fx::FxHashSet;
99
use crate::graph::vec_graph::VecGraph;
1010
use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors};
1111
use rustc_index::vec::{Idx, IndexVec};
12+
use std::cmp::Ord;
1213
use std::ops::Range;
1314

1415
#[cfg(test)]
@@ -38,7 +39,7 @@ struct SccData<S: Idx> {
3839
all_successors: Vec<S>,
3940
}
4041

41-
impl<N: Idx, S: Idx> Sccs<N, S> {
42+
impl<N: Idx, S: Idx + Ord> Sccs<N, S> {
4243
pub fn new(graph: &(impl DirectedGraph<Node = N> + WithNumNodes + WithSuccessors)) -> Self {
4344
SccsConstruction::construct(graph)
4445
}
@@ -85,7 +86,7 @@ impl<N: Idx, S: Idx> DirectedGraph for Sccs<N, S> {
8586
type Node = S;
8687
}
8788

88-
impl<N: Idx, S: Idx> WithNumNodes for Sccs<N, S> {
89+
impl<N: Idx, S: Idx + Ord> WithNumNodes for Sccs<N, S> {
8990
fn num_nodes(&self) -> usize {
9091
self.num_sccs()
9192
}
@@ -103,7 +104,7 @@ impl<'graph, N: Idx, S: Idx> GraphSuccessors<'graph> for Sccs<N, S> {
103104
type Iter = std::iter::Cloned<std::slice::Iter<'graph, S>>;
104105
}
105106

106-
impl<N: Idx, S: Idx> WithSuccessors for Sccs<N, S> {
107+
impl<N: Idx, S: Idx + Ord> WithSuccessors for Sccs<N, S> {
107108
fn successors(&self, node: S) -> <Self as GraphSuccessors<'_>>::Iter {
108109
self.successors(node).iter().cloned()
109110
}

compiler/rustc_data_structures/src/graph/vec_graph/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::cmp::Ord;
2+
13
use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors};
24
use rustc_index::vec::{Idx, IndexVec};
35

@@ -17,7 +19,7 @@ pub struct VecGraph<N: Idx> {
1719
edge_targets: Vec<N>,
1820
}
1921

20-
impl<N: Idx> VecGraph<N> {
22+
impl<N: Idx + Ord> VecGraph<N> {
2123
pub fn new(num_nodes: usize, mut edge_pairs: Vec<(N, N)>) -> Self {
2224
// Sort the edges by the source -- this is important.
2325
edge_pairs.sort();
@@ -100,7 +102,7 @@ impl<'graph, N: Idx> GraphSuccessors<'graph> for VecGraph<N> {
100102
type Iter = std::iter::Cloned<std::slice::Iter<'graph, N>>;
101103
}
102104

103-
impl<N: Idx> WithSuccessors for VecGraph<N> {
105+
impl<N: Idx + Ord> WithSuccessors for VecGraph<N> {
104106
fn successors(&self, node: N) -> <Self as GraphSuccessors<'_>>::Iter {
105107
self.successors(node).iter().cloned()
106108
}

compiler/rustc_hir/src/definitions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ impl DefPathTable {
101101
pub struct Definitions {
102102
table: DefPathTable,
103103

104-
// FIXME(eddyb) ideally all `LocalDefId`s would be HIR owners.
104+
/// Only [`LocalDefId`]s for items and item-like are HIR owners.
105+
/// The associated `HirId` has a `local_id` of `0`.
106+
/// Generic parameters and closures are also assigned a `LocalDefId` but are not HIR owners.
107+
/// Their `HirId`s are defined by their position while lowering the enclosing owner.
108+
// FIXME(cjgillot) Some `LocalDefId`s from `use` items are dropped during lowering and lack a `HirId`.
105109
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
106110
/// The reverse mapping of `def_id_to_hir_id`.
107111
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,

compiler/rustc_hir/src/hir.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ pub enum UnsafeSource {
12031203
UserProvided,
12041204
}
12051205

1206-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Hash, Debug)]
1206+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Hash, Debug)]
12071207
pub struct BodyId {
12081208
pub hir_id: HirId,
12091209
}
@@ -1980,7 +1980,7 @@ pub struct FnSig<'hir> {
19801980
// The bodies for items are stored "out of line", in a separate
19811981
// hashmap in the `Crate`. Here we just record the hir-id of the item
19821982
// so it can fetched later.
1983-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
1983+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
19841984
pub struct TraitItemId {
19851985
pub def_id: LocalDefId,
19861986
}
@@ -2043,7 +2043,7 @@ pub enum TraitItemKind<'hir> {
20432043
// The bodies for items are stored "out of line", in a separate
20442044
// hashmap in the `Crate`. Here we just record the hir-id of the item
20452045
// so it can fetched later.
2046-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
2046+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
20472047
pub struct ImplItemId {
20482048
pub def_id: LocalDefId,
20492049
}
@@ -2644,7 +2644,7 @@ impl<'hir> VariantData<'hir> {
26442644
// The bodies for items are stored "out of line", in a separate
26452645
// hashmap in the `Crate`. Here we just record the hir-id of the item
26462646
// so it can fetched later.
2647-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug, Hash)]
2647+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, Hash)]
26482648
pub struct ItemId {
26492649
pub def_id: LocalDefId,
26502650
}
@@ -2883,7 +2883,7 @@ pub enum AssocItemKind {
28832883
// The bodies for items are stored "out of line", in a separate
28842884
// hashmap in the `Crate`. Here we just record the hir-id of the item
28852885
// so it can fetched later.
2886-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
2886+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug)]
28872887
pub struct ForeignItemId {
28882888
pub def_id: LocalDefId,
28892889
}

compiler/rustc_hir/src/hir_id.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::fmt;
1111
/// the `local_id` part of the `HirId` changing, which is a very useful property in
1212
/// incremental compilation where we have to persist things through changes to
1313
/// the code base.
14-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
14+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
1515
#[derive(Encodable, Decodable)]
1616
pub struct HirId {
1717
pub owner: LocalDefId,
@@ -32,6 +32,10 @@ impl HirId {
3232
pub fn make_owner(owner: LocalDefId) -> Self {
3333
Self { owner, local_id: ItemLocalId::from_u32(0) }
3434
}
35+
36+
pub fn index(self) -> (usize, usize) {
37+
(rustc_index::vec::Idx::index(self.owner), rustc_index::vec::Idx::index(self.local_id))
38+
}
3539
}
3640

3741
impl fmt::Display for HirId {
@@ -40,6 +44,18 @@ impl fmt::Display for HirId {
4044
}
4145
}
4246

47+
impl Ord for HirId {
48+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
49+
(self.index()).cmp(&(other.index()))
50+
}
51+
}
52+
53+
impl PartialOrd for HirId {
54+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
55+
Some(self.cmp(&other))
56+
}
57+
}
58+
4359
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
4460
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
4561

compiler/rustc_index/src/bit_set.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ impl<T: Idx> SparseBitSet<T> {
675675

676676
fn insert(&mut self, elem: T) -> bool {
677677
assert!(elem.index() < self.domain_size);
678-
let changed = if let Some(i) = self.elems.iter().position(|&e| e >= elem) {
678+
let changed = if let Some(i) = self.elems.iter().position(|&e| e.index() >= elem.index()) {
679679
if self.elems[i] == elem {
680680
// `elem` is already in the set.
681681
false
@@ -715,6 +715,10 @@ impl<T: Idx> SparseBitSet<T> {
715715
self.elems.iter()
716716
}
717717

718+
bit_relations_inherent_impls! {}
719+
}
720+
721+
impl<T: Idx + Ord> SparseBitSet<T> {
718722
fn last_set_in(&self, range: impl RangeBounds<T>) -> Option<T> {
719723
let mut last_leq = None;
720724
for e in self.iter() {
@@ -724,8 +728,6 @@ impl<T: Idx> SparseBitSet<T> {
724728
}
725729
last_leq
726730
}
727-
728-
bit_relations_inherent_impls! {}
729731
}
730732

731733
/// A fixed-size bitset type with a hybrid representation: sparse when there
@@ -802,7 +804,10 @@ impl<T: Idx> HybridBitSet<T> {
802804
/// Returns the previous element present in the bitset from `elem`,
803805
/// inclusively of elem. That is, will return `Some(elem)` if elem is in the
804806
/// bitset.
805-
pub fn last_set_in(&self, range: impl RangeBounds<T>) -> Option<T> {
807+
pub fn last_set_in(&self, range: impl RangeBounds<T>) -> Option<T>
808+
where
809+
T: Ord,
810+
{
806811
match self {
807812
HybridBitSet::Sparse(sparse) => sparse.last_set_in(range),
808813
HybridBitSet::Dense(dense) => dense.last_set_in(range),

compiler/rustc_index/src/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::vec;
1212
/// Represents some newtyped `usize` wrapper.
1313
///
1414
/// Purpose: avoid mixing indexes for different bitvector domains.
15-
pub trait Idx: Copy + 'static + Ord + Debug + Hash {
15+
pub trait Idx: Copy + 'static + Eq + PartialEq + Debug + Hash {
1616
fn new(idx: usize) -> Self;
1717

1818
fn index(self) -> usize;

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13061306
})
13071307
.collect::<Vec<_>>();
13081308
// Sort everything to ensure a stable order for diagnotics.
1309-
keys_and_jobs.sort_by_key(|&(def_id, _, _)| def_id);
1309+
keys_and_jobs.sort_by_key(|&(def_id, _, _)| def_id.index());
13101310
for (def_id, encode_const, encode_opt) in keys_and_jobs.into_iter() {
13111311
debug_assert!(encode_const || encode_opt);
13121312

compiler/rustc_middle/src/mir/mono.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9-
use rustc_hir::{HirId, ItemId};
9+
use rustc_hir::ItemId;
1010
use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
1111
use rustc_session::config::OptLevel;
1212
use rustc_span::source_map::Span;
@@ -355,7 +355,7 @@ impl<'tcx> CodegenUnit<'tcx> {
355355
// The codegen tests rely on items being process in the same order as
356356
// they appear in the file, so for local items, we sort by node_id first
357357
#[derive(PartialEq, Eq, PartialOrd, Ord)]
358-
pub struct ItemSortKey<'tcx>(Option<HirId>, SymbolName<'tcx>);
358+
pub struct ItemSortKey<'tcx>(Option<usize>, SymbolName<'tcx>);
359359

360360
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
361361
ItemSortKey(
@@ -366,10 +366,7 @@ impl<'tcx> CodegenUnit<'tcx> {
366366
// instances into account. The others don't matter for
367367
// the codegen tests and can even make item order
368368
// unstable.
369-
InstanceDef::Item(def) => def
370-
.did
371-
.as_local()
372-
.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)),
369+
InstanceDef::Item(def) => Some(def.did.index.as_usize()),
373370
InstanceDef::VtableShim(..)
374371
| InstanceDef::ReifyShim(..)
375372
| InstanceDef::Intrinsic(..)
@@ -380,10 +377,10 @@ impl<'tcx> CodegenUnit<'tcx> {
380377
| InstanceDef::CloneShim(..) => None,
381378
}
382379
}
383-
MonoItem::Static(def_id) => {
384-
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
380+
MonoItem::Static(def_id) => Some(def_id.index.as_usize()),
381+
MonoItem::GlobalAsm(item_id) => {
382+
Some(item_id.def_id.to_def_id().index.as_usize())
385383
}
386-
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
387384
},
388385
item.symbol_name(tcx),
389386
)

compiler/rustc_middle/src/ty/fast_reject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub type SimplifiedType = SimplifiedTypeGen<DefId>;
2020
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, TyEncodable, TyDecodable)]
2121
pub enum SimplifiedTypeGen<D>
2222
where
23-
D: Copy + Debug + Ord + Eq,
23+
D: Copy + Debug + Eq,
2424
{
2525
BoolSimplifiedType,
2626
CharSimplifiedType,

compiler/rustc_mir_transform/src/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ impl<'tcx> Inliner<'tcx> {
212212
// a lower `HirId` than the callee. This ensures that the callee will
213213
// not inline us. This trick only works without incremental compilation.
214214
// So don't do it if that is enabled.
215-
if !self.tcx.dep_graph.is_fully_enabled() && self.hir_id < callee_hir_id {
215+
if !self.tcx.dep_graph.is_fully_enabled() && self.hir_id.index() < callee_hir_id.index()
216+
{
216217
return Ok(());
217218
}
218219

compiler/rustc_span/src/def_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
322322
/// few cases where we know that only DefIds from the local crate are expected
323323
/// and a DefId from a different crate would signify a bug somewhere. This
324324
/// is when LocalDefId comes in handy.
325-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
325+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
326326
pub struct LocalDefId {
327327
pub local_def_index: DefIndex,
328328
}

compiler/rustc_span/src/lib.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl FileName {
424424
/// `SpanData` is public because `Span` uses a thread-local interner and can't be
425425
/// sent to other threads, but some pieces of performance infra run in a separate thread.
426426
/// Using `Span` is generally preferred.
427-
#[derive(Clone, Copy, Hash, PartialEq, Eq, Ord, PartialOrd)]
427+
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
428428
pub struct SpanData {
429429
pub lo: BytePos,
430430
pub hi: BytePos,
@@ -434,6 +434,36 @@ pub struct SpanData {
434434
pub parent: Option<LocalDefId>,
435435
}
436436

437+
// Order spans by position in the file.
438+
impl Ord for SpanData {
439+
fn cmp(&self, other: &Self) -> Ordering {
440+
let SpanData {
441+
lo: s_lo,
442+
hi: s_hi,
443+
ctxt: s_ctxt,
444+
// `LocalDefId` does not implement `Ord`.
445+
// The other fields are enough to determine in-file order.
446+
parent: _,
447+
} = self;
448+
let SpanData {
449+
lo: o_lo,
450+
hi: o_hi,
451+
ctxt: o_ctxt,
452+
// `LocalDefId` does not implement `Ord`.
453+
// The other fields are enough to determine in-file order.
454+
parent: _,
455+
} = other;
456+
457+
(s_lo, s_hi, s_ctxt).cmp(&(o_lo, o_hi, o_ctxt))
458+
}
459+
}
460+
461+
impl PartialOrd for SpanData {
462+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
463+
Some(self.cmp(other))
464+
}
465+
}
466+
437467
impl SpanData {
438468
#[inline]
439469
pub fn span(&self) -> Span {

0 commit comments

Comments
 (0)