Skip to content

Commit 9fad214

Browse files
authoredApr 19, 2022
Rollup merge of #96142 - cjgillot:no-crate-def-index, r=petrochenkov
Stop using CRATE_DEF_INDEX outside of metadata encoding. `CRATE_DEF_ID` and `CrateNum::as_def_id` are almost always what we want. We should not manipulate raw `DefIndex` outside of metadata encoding.
2 parents 036d200 + 07ee031 commit 9fad214

File tree

30 files changed

+109
-147
lines changed

30 files changed

+109
-147
lines changed
 

‎compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::collections::hash_map::Entry::*;
22

33
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
4-
use rustc_data_structures::fingerprint::Fingerprint;
54
use rustc_data_structures::fx::FxHashMap;
65
use rustc_hir as hir;
7-
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
6+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
87
use rustc_hir::Node;
9-
use rustc_index::vec::IndexVec;
108
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
119
use rustc_middle::middle::exported_symbols::{
1210
metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
@@ -277,17 +275,6 @@ fn upstream_monomorphizations_provider(
277275

278276
let mut instances: DefIdMap<FxHashMap<_, _>> = Default::default();
279277

280-
let cnum_stable_ids: IndexVec<CrateNum, Fingerprint> = {
281-
let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO, cnums.len() + 1);
282-
283-
for &cnum in cnums.iter() {
284-
cnum_stable_ids[cnum] =
285-
tcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).0;
286-
}
287-
288-
cnum_stable_ids
289-
};
290-
291278
let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn();
292279

293280
for &cnum in cnums.iter() {
@@ -316,7 +303,7 @@ fn upstream_monomorphizations_provider(
316303
// If there are multiple monomorphizations available,
317304
// we select one deterministically.
318305
let other_cnum = *e.get();
319-
if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] {
306+
if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) {
320307
e.insert(cnum);
321308
}
322309
}

‎compiler/rustc_hir/src/def.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1+
use crate::def_id::DefId;
22
use crate::hir;
33

44
use rustc_ast as ast;
@@ -124,9 +124,7 @@ impl DefKind {
124124
pub fn descr(self, def_id: DefId) -> &'static str {
125125
match self {
126126
DefKind::Fn => "function",
127-
DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => {
128-
"crate"
129-
}
127+
DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate",
130128
DefKind::Mod => "module",
131129
DefKind::Static(..) => "static",
132130
DefKind::Enum => "enum",

‎compiler/rustc_hir/src/definitions.rs

-5
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,6 @@ impl Definitions {
353353
}
354354
}
355355

356-
/// Retrieves the root definition.
357-
pub fn get_root_def(&self) -> LocalDefId {
358-
LocalDefId { local_def_index: CRATE_DEF_INDEX }
359-
}
360-
361356
/// Adds a definition with a parent definition.
362357
pub fn create_def(
363358
&mut self,

‎compiler/rustc_hir/src/hir_id.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
1+
use crate::def_id::{LocalDefId, CRATE_DEF_ID};
22
use std::fmt;
33

44
/// Uniquely identifies a node in the HIR of the current crate. It is
@@ -84,8 +84,5 @@ impl ItemLocalId {
8484
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
8585
}
8686

87-
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
88-
pub const CRATE_HIR_ID: HirId = HirId {
89-
owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
90-
local_id: ItemLocalId::from_u32(0),
91-
};
87+
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
88+
pub const CRATE_HIR_ID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::from_u32(0) };

‎compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::native_libs;
55

66
use rustc_ast as ast;
77
use rustc_hir::def::{CtorKind, DefKind, Res};
8-
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
8+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
99
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1010
use rustc_middle::metadata::ModChild;
1111
use rustc_middle::middle::exported_symbols::ExportedSymbol;
@@ -325,7 +325,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
325325
continue;
326326
}
327327

328-
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
328+
bfs_queue.push_back(cnum.as_def_id());
329329
}
330330

331331
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| {

‎compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16401640
let hir = tcx.hir();
16411641

16421642
let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index;
1643-
let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX));
1643+
let stability = tcx.lookup_stability(CRATE_DEF_ID);
16441644
let macros =
16451645
self.lazy(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
16461646
let spans = self.tcx.sess.parse_sess.proc_macro_quoted_spans();

‎compiler/rustc_middle/src/dep_graph/dep_node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::mir::mono::MonoItem;
6060
use crate::ty::TyCtxt;
6161

6262
use rustc_data_structures::fingerprint::Fingerprint;
63-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
63+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::HirId;
6666
use rustc_query_system::dep_graph::FingerprintStyle;
@@ -366,7 +366,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
366366

367367
#[inline(always)]
368368
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
369-
let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
369+
let def_id = self.as_def_id();
370370
def_id.to_fingerprint(tcx)
371371
}
372372

‎compiler/rustc_middle/src/middle/stability.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_errors::{Applicability, Diagnostic};
1111
use rustc_feature::GateIssue;
1212
use rustc_hir as hir;
1313
use rustc_hir::def::DefKind;
14-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
14+
use rustc_hir::def_id::{DefId, LocalDefId};
1515
use rustc_hir::{self, HirId};
1616
use rustc_middle::ty::print::with_no_trimmed_paths;
1717
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
@@ -370,8 +370,7 @@ impl<'tcx> TyCtxt<'tcx> {
370370
};
371371
}
372372

373-
let is_staged_api =
374-
self.lookup_stability(DefId { index: CRATE_DEF_INDEX, ..def_id }).is_some();
373+
let is_staged_api = self.lookup_stability(def_id.krate.as_def_id()).is_some();
375374
if !is_staged_api {
376375
return EvalResult::Allow;
377376
}

‎compiler/rustc_middle/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex}
1515

1616
use rustc_errors::ErrorGuaranteed;
1717
use rustc_hir::def::{CtorKind, Namespace};
18-
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
18+
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
1919
use rustc_hir::{self, GeneratorKind};
2020
use rustc_hir::{self as hir, HirId};
2121
use rustc_session::Session;
@@ -385,7 +385,7 @@ impl<'tcx> Body<'tcx> {
385385
pub fn new_cfg_only(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
386386
let mut body = Body {
387387
phase: MirPhase::Built,
388-
source: MirSource::item(DefId::local(CRATE_DEF_INDEX)),
388+
source: MirSource::item(CRATE_DEF_ID.to_def_id()),
389389
basic_blocks,
390390
source_scopes: IndexVec::new(),
391391
generator: None,

‎compiler/rustc_middle/src/ty/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3636
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3737
use rustc_hir as hir;
3838
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
39-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_INDEX};
39+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_ID};
4040
use rustc_hir::Node;
4141
use rustc_macros::HashStable;
4242
use rustc_query_system::ich::StableHashingContext;
@@ -320,7 +320,7 @@ impl Visibility {
320320
pub fn from_hir(visibility: &hir::Visibility<'_>, id: hir::HirId, tcx: TyCtxt<'_>) -> Self {
321321
match visibility.node {
322322
hir::VisibilityKind::Public => Visibility::Public,
323-
hir::VisibilityKind::Crate(_) => Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)),
323+
hir::VisibilityKind::Crate(_) => Visibility::Restricted(CRATE_DEF_ID.to_def_id()),
324324
hir::VisibilityKind::Restricted { ref path, .. } => match path.res {
325325
// If there is no resolution, `resolve` will have already reported an error, so
326326
// assume that the visibility is public to avoid reporting more privacy errors.
@@ -1993,8 +1993,8 @@ impl<'tcx> TyCtxt<'tcx> {
19931993
}
19941994

19951995
fn opt_item_name(self, def_id: DefId) -> Option<Symbol> {
1996-
if def_id.index == CRATE_DEF_INDEX {
1997-
Some(self.crate_name(def_id.krate))
1996+
if let Some(cnum) = def_id.as_crate_root() {
1997+
Some(self.crate_name(cnum))
19981998
} else {
19991999
let def_key = self.def_key(def_id);
20002000
match def_key.disambiguated_data.data {

‎compiler/rustc_middle/src/ty/print/pretty.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::sso::SsoHashSet;
77
use rustc_hir as hir;
88
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
9-
use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_INDEX, LOCAL_CRATE};
9+
use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_ID, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
1111
use rustc_session::config::TrimmedDefPaths;
1212
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
@@ -335,9 +335,7 @@ pub trait PrettyPrinter<'tcx>:
335335

336336
// If `def_id` is a direct or injected extern crate, return the
337337
// path to the crate followed by the path to the item within the crate.
338-
if def_id.index == CRATE_DEF_INDEX {
339-
let cnum = def_id.krate;
340-
338+
if let Some(cnum) = def_id.as_crate_root() {
341339
if cnum == LOCAL_CRATE {
342340
return Ok((self.path_crate(cnum)?, true));
343341
}
@@ -2227,11 +2225,11 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
22272225
ty::BrNamed(_, _) => br.kind,
22282226
ty::BrAnon(i) => {
22292227
let name = region_map[&(i + 1)];
2230-
ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name)
2228+
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
22312229
}
22322230
ty::BrEnv => {
22332231
let name = region_map[&0];
2234-
ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name)
2232+
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
22352233
}
22362234
};
22372235
self.tcx.mk_region(ty::ReLateBound(
@@ -2257,7 +2255,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
22572255
}
22582256
};
22592257
do_continue(&mut self, name);
2260-
ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name)
2258+
ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
22612259
}
22622260
};
22632261
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind }))
@@ -2697,7 +2695,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
26972695
let mut seen_defs: DefIdSet = Default::default();
26982696

26992697
for &cnum in tcx.crates(()).iter() {
2700-
let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
2698+
let def_id = cnum.as_def_id();
27012699

27022700
// Ignore crates that are not direct dependencies.
27032701
match tcx.extern_crate(def_id) {

‎compiler/rustc_middle/src/ty/structural_impls.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::ty::{self, InferConst, Lift, Term, Ty, TyCtxt};
1010
use rustc_data_structures::functor::IdFunctor;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::Namespace;
13-
use rustc_hir::def_id::CRATE_DEF_INDEX;
1413
use rustc_index::vec::{Idx, IndexVec};
1514

1615
use std::fmt;
@@ -71,7 +70,7 @@ impl fmt::Debug for ty::BoundRegionKind {
7170
match *self {
7271
ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
7372
ty::BrNamed(did, name) => {
74-
if did.index == CRATE_DEF_INDEX {
73+
if did.is_crate_root() {
7574
write!(f, "BrNamed({})", name)
7675
} else {
7776
write!(f, "BrNamed({:?}, {})", did, name)

‎compiler/rustc_monomorphize/src/partitioning/default.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::hash_map::Entry;
22

33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_hir::def::DefKind;
5-
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
5+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
66
use rustc_hir::definitions::DefPathDataName;
77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
88
use rustc_middle::middle::exported_symbols::SymbolExportLevel;
@@ -335,10 +335,10 @@ fn compute_codegen_unit_name(
335335
let mut cgu_def_id = None;
336336
// Walk backwards from the item we want to find the module for.
337337
loop {
338-
if current_def_id.index == CRATE_DEF_INDEX {
338+
if current_def_id.is_crate_root() {
339339
if cgu_def_id.is_none() {
340340
// If we have not found a module yet, take the crate root.
341-
cgu_def_id = Some(DefId { krate: def_id.krate, index: CRATE_DEF_INDEX });
341+
cgu_def_id = Some(def_id.krate.as_def_id());
342342
}
343343
break;
344344
} else if tcx.def_kind(current_def_id) == DefKind::Mod {

0 commit comments

Comments
 (0)
Please sign in to comment.