From 07ee0317638cf1f290a8dace1c7ccc8fea16a236 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 15 Apr 2022 19:27:53 +0200 Subject: [PATCH] Stop using CRATE_DEF_INDEX. `CRATE_DEF_ID` and `CrateNum::as_def_id` are almost always what we want. --- .../src/back/symbol_export.rs | 17 +----- compiler/rustc_hir/src/def.rs | 6 +-- compiler/rustc_hir/src/definitions.rs | 5 -- compiler/rustc_hir/src/hir_id.rs | 9 ++-- .../src/rmeta/decoder/cstore_impl.rs | 4 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 2 +- .../rustc_middle/src/dep_graph/dep_node.rs | 4 +- compiler/rustc_middle/src/middle/stability.rs | 5 +- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/ty/mod.rs | 8 +-- compiler/rustc_middle/src/ty/print/pretty.rs | 14 +++-- .../rustc_middle/src/ty/structural_impls.rs | 3 +- .../src/partitioning/default.rs | 6 +-- compiler/rustc_passes/src/entry.rs | 53 ++++++++----------- compiler/rustc_passes/src/hir_id_validator.rs | 4 +- compiler/rustc_passes/src/stability.rs | 4 +- .../rustc_query_impl/src/profiling_support.rs | 4 +- .../rustc_resolve/src/build_reduced_graph.rs | 16 +++--- compiler/rustc_resolve/src/diagnostics.rs | 4 +- compiler/rustc_resolve/src/late.rs | 4 +- .../rustc_resolve/src/late/diagnostics.rs | 10 ++-- compiler/rustc_resolve/src/lib.rs | 11 ++-- compiler/rustc_span/src/def_id.rs | 15 +++++- src/librustdoc/clean/mod.rs | 6 +-- src/librustdoc/clean/types.rs | 16 ++---- src/librustdoc/formats/cache.rs | 4 +- src/librustdoc/html/format.rs | 5 +- src/librustdoc/json/conversions.rs | 3 +- src/librustdoc/visit_lib.rs | 4 +- .../clippy/clippy_lints/src/missing_doc.rs | 6 +-- 30 files changed, 109 insertions(+), 147 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 765bd877db169..7b7c676c26cd1 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -1,12 +1,10 @@ use std::collections::hash_map::Entry::*; use rustc_ast::expand::allocator::ALLOCATOR_METHODS; -use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; use rustc_hir::Node; -use rustc_index::vec::IndexVec; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::exported_symbols::{ metadata_symbol_name, ExportedSymbol, SymbolExportLevel, @@ -277,17 +275,6 @@ fn upstream_monomorphizations_provider( let mut instances: DefIdMap> = Default::default(); - let cnum_stable_ids: IndexVec = { - let mut cnum_stable_ids = IndexVec::from_elem_n(Fingerprint::ZERO, cnums.len() + 1); - - for &cnum in cnums.iter() { - cnum_stable_ids[cnum] = - tcx.def_path_hash(DefId { krate: cnum, index: CRATE_DEF_INDEX }).0; - } - - cnum_stable_ids - }; - let drop_in_place_fn_def_id = tcx.lang_items().drop_in_place_fn(); for &cnum in cnums.iter() { @@ -316,7 +303,7 @@ fn upstream_monomorphizations_provider( // If there are multiple monomorphizations available, // we select one deterministically. let other_cnum = *e.get(); - if cnum_stable_ids[other_cnum] > cnum_stable_ids[cnum] { + if tcx.stable_crate_id(other_cnum) > tcx.stable_crate_id(cnum) { e.insert(cnum); } } diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 53d60d280c001..324e110005717 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -1,4 +1,4 @@ -use crate::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use crate::def_id::DefId; use crate::hir; use rustc_ast as ast; @@ -124,9 +124,7 @@ impl DefKind { pub fn descr(self, def_id: DefId) -> &'static str { match self { DefKind::Fn => "function", - DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE => { - "crate" - } + DefKind::Mod if def_id.is_crate_root() && !def_id.is_local() => "crate", DefKind::Mod => "module", DefKind::Static(..) => "static", DefKind::Enum => "enum", diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 4908992085a6e..bce9ba12ac0c4 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -353,11 +353,6 @@ impl Definitions { } } - /// Retrieves the root definition. - pub fn get_root_def(&self) -> LocalDefId { - LocalDefId { local_def_index: CRATE_DEF_INDEX } - } - /// Adds a definition with a parent definition. pub fn create_def( &mut self, diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 3b5e4dcf5e011..e0ff28dff15cd 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -1,4 +1,4 @@ -use crate::def_id::{LocalDefId, CRATE_DEF_INDEX}; +use crate::def_id::{LocalDefId, CRATE_DEF_ID}; use std::fmt; /// Uniquely identifies a node in the HIR of the current crate. It is @@ -84,8 +84,5 @@ impl ItemLocalId { pub const INVALID: ItemLocalId = ItemLocalId::MAX; } -/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`. -pub const CRATE_HIR_ID: HirId = HirId { - owner: LocalDefId { local_def_index: CRATE_DEF_INDEX }, - local_id: ItemLocalId::from_u32(0), -}; +/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`. +pub const CRATE_HIR_ID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::from_u32(0) }; diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 63bf929fb8639..9c88171d17566 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -5,7 +5,7 @@ use crate::native_libs; use rustc_ast as ast; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::exported_symbols::ExportedSymbol; @@ -324,7 +324,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { continue; } - bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX }); + bfs_queue.push_back(cnum.as_def_id()); } let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index e967750aebb52..50010fb83cf3b 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1633,7 +1633,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let hir = tcx.hir(); let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; - let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)); + let stability = tcx.lookup_stability(CRATE_DEF_ID); let macros = self.lazy(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)); let spans = self.tcx.sess.parse_sess.proc_macro_quoted_spans(); diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index d20be0a34d2d5..8402ca3028cce 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -60,7 +60,7 @@ use crate::mir::mono::MonoItem; use crate::ty::TyCtxt; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::definitions::DefPathHash; use rustc_hir::HirId; use rustc_query_system::dep_graph::FingerprintStyle; @@ -366,7 +366,7 @@ impl<'tcx> DepNodeParams> for CrateNum { #[inline(always)] fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint { - let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX }; + let def_id = self.as_def_id(); def_id.to_fingerprint(tcx) } diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index fd6e241346db8..758658c3d8c94 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -11,7 +11,7 @@ use rustc_errors::{Applicability, Diagnostic}; use rustc_feature::GateIssue; use rustc_hir as hir; use rustc_hir::def::DefKind; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{self, HirId}; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE}; @@ -370,8 +370,7 @@ impl<'tcx> TyCtxt<'tcx> { }; } - let is_staged_api = - self.lookup_stability(DefId { index: CRATE_DEF_INDEX, ..def_id }).is_some(); + let is_staged_api = self.lookup_stability(def_id.krate.as_def_id()).is_some(); if !is_staged_api { return EvalResult::Allow; } diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 49769b7ae3d89..4f4b6cf704fa9 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -15,7 +15,7 @@ use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex} use rustc_errors::ErrorGuaranteed; use rustc_hir::def::{CtorKind, Namespace}; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::{self, GeneratorKind}; use rustc_hir::{self as hir, HirId}; use rustc_session::Session; @@ -385,7 +385,7 @@ impl<'tcx> Body<'tcx> { pub fn new_cfg_only(basic_blocks: IndexVec>) -> Self { let mut body = Body { phase: MirPhase::Built, - source: MirSource::item(DefId::local(CRATE_DEF_INDEX)), + source: MirSource::item(CRATE_DEF_ID.to_def_id()), basic_blocks, source_scopes: IndexVec::new(), generator: None, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index c2accea11ba2a..2901772bc74ec 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -36,7 +36,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::tagged_ptr::CopyTaggedPtr; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_ID}; use rustc_hir::Node; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; @@ -319,7 +319,7 @@ impl Visibility { pub fn from_hir(visibility: &hir::Visibility<'_>, id: hir::HirId, tcx: TyCtxt<'_>) -> Self { match visibility.node { hir::VisibilityKind::Public => Visibility::Public, - hir::VisibilityKind::Crate(_) => Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)), + hir::VisibilityKind::Crate(_) => Visibility::Restricted(CRATE_DEF_ID.to_def_id()), hir::VisibilityKind::Restricted { ref path, .. } => match path.res { // If there is no resolution, `resolve` will have already reported an error, so // assume that the visibility is public to avoid reporting more privacy errors. @@ -1992,8 +1992,8 @@ impl<'tcx> TyCtxt<'tcx> { } fn opt_item_name(self, def_id: DefId) -> Option { - if def_id.index == CRATE_DEF_INDEX { - Some(self.crate_name(def_id.krate)) + if let Some(cnum) = def_id.as_crate_root() { + Some(self.crate_name(cnum)) } else { let def_key = self.def_key(def_id); match def_key.disambiguated_data.data { diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 4379adb604e1f..d2c5bd1898614 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sso::SsoHashSet; use rustc_hir as hir; use rustc_hir::def::{self, CtorKind, DefKind, Namespace}; -use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData}; use rustc_hir::ItemKind; use rustc_session::config::TrimmedDefPaths; @@ -336,9 +336,7 @@ pub trait PrettyPrinter<'tcx>: // If `def_id` is a direct or injected extern crate, return the // path to the crate followed by the path to the item within the crate. - if def_id.index == CRATE_DEF_INDEX { - let cnum = def_id.krate; - + if let Some(cnum) = def_id.as_crate_root() { if cnum == LOCAL_CRATE { return Ok((self.path_crate(cnum)?, true)); } @@ -2228,11 +2226,11 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { ty::BrNamed(_, _) => br.kind, ty::BrAnon(i) => { let name = region_map[&(i + 1)]; - ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name) + ty::BrNamed(CRATE_DEF_ID.to_def_id(), name) } ty::BrEnv => { let name = region_map[&0]; - ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name) + ty::BrNamed(CRATE_DEF_ID.to_def_id(), name) } }; self.tcx.mk_region(ty::ReLateBound( @@ -2258,7 +2256,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { } }; do_continue(&mut self, name); - ty::BrNamed(DefId::local(CRATE_DEF_INDEX), name) + ty::BrNamed(CRATE_DEF_ID.to_def_id(), name) } }; tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind })) @@ -2693,7 +2691,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N let mut seen_defs: DefIdSet = Default::default(); for &cnum in tcx.crates(()).iter() { - let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; + let def_id = cnum.as_def_id(); // Ignore crates that are not direct dependencies. match tcx.extern_crate(def_id) { diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 5c7910db362d2..4ef6ff1835ffd 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -10,7 +10,6 @@ use crate::ty::{self, InferConst, Lift, Term, Ty, TyCtxt}; use rustc_data_structures::functor::IdFunctor; use rustc_hir as hir; use rustc_hir::def::Namespace; -use rustc_hir::def_id::CRATE_DEF_INDEX; use rustc_index::vec::{Idx, IndexVec}; use std::fmt; @@ -71,7 +70,7 @@ impl fmt::Debug for ty::BoundRegionKind { match *self { ty::BrAnon(n) => write!(f, "BrAnon({:?})", n), ty::BrNamed(did, name) => { - if did.index == CRATE_DEF_INDEX { + if did.is_crate_root() { write!(f, "BrNamed({})", name) } else { write!(f, "BrNamed({:?}, {})", did, name) diff --git a/compiler/rustc_monomorphize/src/partitioning/default.rs b/compiler/rustc_monomorphize/src/partitioning/default.rs index c4ffb19f87a91..1677419887988 100644 --- a/compiler/rustc_monomorphize/src/partitioning/default.rs +++ b/compiler/rustc_monomorphize/src/partitioning/default.rs @@ -2,7 +2,7 @@ use std::collections::hash_map::Entry; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir::def::DefKind; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::definitions::DefPathDataName; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::exported_symbols::SymbolExportLevel; @@ -335,10 +335,10 @@ fn compute_codegen_unit_name( let mut cgu_def_id = None; // Walk backwards from the item we want to find the module for. loop { - if current_def_id.index == CRATE_DEF_INDEX { + if current_def_id.is_crate_root() { if cgu_def_id.is_none() { // If we have not found a module yet, take the crate root. - cgu_def_id = Some(DefId { krate: def_id.krate, index: CRATE_DEF_INDEX }); + cgu_def_id = Some(def_id.krate.as_def_id()); } break; } else if tcx.def_kind(current_def_id) == DefKind::Mod { diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index 5a1373ad1a218..db083d0453bc0 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -1,21 +1,18 @@ use rustc_ast::entry::EntryPointType; use rustc_errors::struct_span_err; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::{ForeignItem, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID}; -use rustc_middle::hir::map::Map; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{DefIdTree, TyCtxt}; use rustc_session::config::{CrateType, EntryFnType}; use rustc_session::parse::feature_err; use rustc_session::Session; use rustc_span::symbol::sym; use rustc_span::{Span, DUMMY_SP}; -struct EntryContext<'a, 'tcx> { - session: &'a Session, - - map: Map<'tcx>, +struct EntryContext<'tcx> { + tcx: TyCtxt<'tcx>, /// The function that has attribute named `main`. attr_main_fn: Option<(LocalDefId, Span)>, @@ -28,10 +25,9 @@ struct EntryContext<'a, 'tcx> { non_main_fns: Vec, } -impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { +impl<'tcx> ItemLikeVisitor<'tcx> for EntryContext<'tcx> { fn visit_item(&mut self, item: &'tcx Item<'tcx>) { - let def_key = self.map.def_key(item.def_id); - let at_root = def_key.parent == Some(CRATE_DEF_INDEX); + let at_root = self.tcx.local_parent(item.def_id) == Some(CRATE_DEF_ID); find_item(item, self, at_root); } @@ -60,13 +56,8 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> { return None; } - let mut ctxt = EntryContext { - session: tcx.sess, - map: tcx.hir(), - attr_main_fn: None, - start_fn: None, - non_main_fns: Vec::new(), - }; + let mut ctxt = + EntryContext { tcx, attr_main_fn: None, start_fn: None, non_main_fns: Vec::new() }; tcx.hir().visit_all_item_likes(&mut ctxt); @@ -75,11 +66,11 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> { // Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs` // (with `ast::Item`), so make sure to keep them in sync. -fn entry_point_type(ctxt: &EntryContext<'_, '_>, item: &Item<'_>, at_root: bool) -> EntryPointType { - let attrs = ctxt.map.attrs(item.hir_id()); - if ctxt.session.contains_name(attrs, sym::start) { +fn entry_point_type(ctxt: &EntryContext<'_>, item: &Item<'_>, at_root: bool) -> EntryPointType { + let attrs = ctxt.tcx.hir().attrs(item.hir_id()); + if ctxt.tcx.sess.contains_name(attrs, sym::start) { EntryPointType::Start - } else if ctxt.session.contains_name(attrs, sym::rustc_main) { + } else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) { EntryPointType::MainAttr } else if item.ident.name == sym::main { if at_root { @@ -98,16 +89,16 @@ fn throw_attr_err(sess: &Session, span: Span, attr: &str) { .emit(); } -fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { +fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_>, at_root: bool) { match entry_point_type(ctxt, item, at_root) { EntryPointType::None => (), _ if !matches!(item.kind, ItemKind::Fn(..)) => { - let attrs = ctxt.map.attrs(item.hir_id()); - if let Some(attr) = ctxt.session.find_by_name(attrs, sym::start) { - throw_attr_err(&ctxt.session, attr.span, "start"); + let attrs = ctxt.tcx.hir().attrs(item.hir_id()); + if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::start) { + throw_attr_err(&ctxt.tcx.sess, attr.span, "start"); } - if let Some(attr) = ctxt.session.find_by_name(attrs, sym::rustc_main) { - throw_attr_err(&ctxt.session, attr.span, "rustc_main"); + if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::rustc_main) { + throw_attr_err(&ctxt.tcx.sess, attr.span, "rustc_main"); } } EntryPointType::MainNamed => (), @@ -119,7 +110,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { ctxt.attr_main_fn = Some((item.def_id, item.span)); } else { struct_span_err!( - ctxt.session, + ctxt.tcx.sess, item.span, E0137, "multiple functions with a `#[main]` attribute" @@ -133,7 +124,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { if ctxt.start_fn.is_none() { ctxt.start_fn = Some((item.def_id, item.span)); } else { - struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions") + struct_span_err!(ctxt.tcx.sess, item.span, E0138, "multiple `start` functions") .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here") .span_label(item.span, "multiple `start` functions") .emit(); @@ -142,7 +133,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { } } -fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> { +fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId, EntryFnType)> { if let Some((def_id, _)) = visitor.start_fn { Some((def_id.to_def_id(), EntryFnType::Start)) } else if let Some((def_id, _)) = visitor.attr_main_fn { @@ -177,7 +168,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De } } -fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { +fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) { let sp = tcx.def_span(CRATE_DEF_ID); if *tcx.sess.parse_sess.reached_eof.borrow() { // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 56755d68686e3..379a6827c8aac 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -1,7 +1,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lock; use rustc_hir as hir; -use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::intravisit; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::{HirId, ItemLocalId}; @@ -89,7 +89,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { self.owner = Some(owner); walk(self); - if owner.local_def_index == CRATE_DEF_INDEX { + if owner == CRATE_DEF_ID { return; } diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 01ba9e35c24dc..35a858cb86c3f 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -6,7 +6,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap}; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{FieldDef, Generics, HirId, Item, TraitRef, Ty, TyKind, Variant}; @@ -703,7 +703,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { let Some(cnum) = self.tcx.extern_mod_stmt_cnum(item.def_id) else { return; }; - let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; + let def_id = cnum.as_def_id(); self.tcx.check_stability(def_id, Some(item.hir_id()), item.span, None); } diff --git a/compiler/rustc_query_impl/src/profiling_support.rs b/compiler/rustc_query_impl/src/profiling_support.rs index acccf43f06285..b20aa7b53468a 100644 --- a/compiler/rustc_query_impl/src/profiling_support.rs +++ b/compiler/rustc_query_impl/src/profiling_support.rs @@ -1,7 +1,7 @@ use measureme::{StringComponent, StringId}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::profiling::SelfProfiler; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; use rustc_middle::ty::{TyCtxt, WithOptConstParam}; use rustc_query_system::query::QueryCache; @@ -143,7 +143,7 @@ impl SpecIntoSelfProfilingString for CrateNum { &self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>, ) -> StringId { - builder.def_id_to_string_id(DefId { krate: *self, index: CRATE_DEF_INDEX }) + builder.def_id_to_string_id(self.as_def_id()) } } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 54e8c03156d55..77995b60a557e 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -23,7 +23,7 @@ use rustc_errors::{struct_span_err, Applicability}; use rustc_expand::base::SyntaxExtension; use rustc_expand::expand::AstFragment; use rustc_hir::def::{self, *}; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID}; use rustc_metadata::creader::LoadedMacro; use rustc_middle::bug; use rustc_middle::metadata::ModChild; @@ -140,8 +140,8 @@ impl<'a> Resolver<'a> { let parent = def_key.parent.map(|index| { self.get_nearest_non_block_module(DefId { index, krate: def_id.krate }) }); - let name = if def_id.index == CRATE_DEF_INDEX { - self.cstore().crate_name(def_id.krate) + let name = if let Some(cnum) = def_id.as_crate_root() { + self.cstore().crate_name(cnum) } else { def_key.disambiguated_data.data.get_opt_name().expect("module without name") }; @@ -250,7 +250,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { match vis.kind { ast::VisibilityKind::Public => Ok(ty::Visibility::Public), ast::VisibilityKind::Crate(..) => { - Ok(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))) + Ok(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())) } ast::VisibilityKind::Inherited => { Ok(match self.parent_scope.module.kind { @@ -759,7 +759,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let mut ctor_vis = if vis == ty::Visibility::Public && self.r.session.contains_name(&item.attrs, sym::non_exhaustive) { - ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)) + ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) } else { vis }; @@ -1108,7 +1108,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { root_span: span, span, module_path: Vec::new(), - vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))), + vis: Cell::new(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())), used: Cell::new(false), }) }; @@ -1244,7 +1244,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let vis = if is_macro_export { ty::Visibility::Public } else { - ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)) + ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) }; let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas); self.r.set_binding_parent_module(binding, parent_scope.module); @@ -1490,7 +1490,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { let ctor_vis = if vis == ty::Visibility::Public && self.r.session.contains_name(&variant.attrs, sym::non_exhaustive) { - ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)) + ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()) } else { vis }; diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f3b8c1e266c58..899980a4c0887 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -10,7 +10,7 @@ use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed use rustc_feature::BUILTIN_ATTRIBUTES; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS}; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::PrimTy; use rustc_middle::bug; use rustc_middle::ty::DefIdTree; @@ -1167,7 +1167,7 @@ impl<'a> Resolver<'a> { } Scope::ExternPrelude => { suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| { - let res = Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX)); + let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id()); filter_fn(res).then_some(TypoSuggestion::typo_from_res(ident.name, res)) })); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 763f31622bc1f..1a98dd38fc97e 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -19,7 +19,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_errors::DiagnosticId; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS}; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_hir::{PrimTy, TraitCandidate}; use rustc_middle::{bug, span_bug}; use rustc_session::lint; @@ -2210,7 +2210,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // trait to resolve. In that case, we leave the `B` // segment to be resolved by type-check. return Ok(Some(PartialRes::with_unresolved_segments( - Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX)), + Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id()), path.len(), ))); } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 0926f24ae70ef..17a9ef4b4db83 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -19,7 +19,7 @@ use rustc_errors::{ use rustc_hir as hir; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind}; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::PrimTy; use rustc_session::parse::feature_err; use rustc_span::edition::Edition; @@ -351,7 +351,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } }) .collect::>(); - let crate_def_id = DefId::local(CRATE_DEF_INDEX); + let crate_def_id = CRATE_DEF_ID.to_def_id(); if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) { let mut enum_candidates: Vec<_> = self .r @@ -1331,10 +1331,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { names.extend(extern_prelude.iter().flat_map(|(ident, _)| { self.r.crate_loader.maybe_process_path_extern(ident.name).and_then( |crate_id| { - let crate_mod = Res::Def( - DefKind::Mod, - DefId { krate: crate_id, index: CRATE_DEF_INDEX }, - ); + let crate_mod = + Res::Def(DefKind::Mod, crate_id.as_def_id()); if filter_fn(crate_mod) { Some(TypoSuggestion::typo_from_res( diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 0335c40d70d8c..5389cb19b58e4 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -37,7 +37,7 @@ use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind use rustc_hir::def::Namespace::*; use rustc_hir::def::{self, CtorOf, DefKind, PartialRes}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId}; -use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPathData, Definitions}; use rustc_hir::TraitCandidate; use rustc_index::vec::IndexVec; @@ -755,7 +755,7 @@ impl<'a> NameBinding<'a> { NameBindingKind::Module(&ModuleData { kind: ModuleKind::Def(DefKind::Mod, def_id, _), .. - }) => def_id.index == CRATE_DEF_INDEX, + }) => def_id.is_crate_root(), _ => false, } } @@ -1207,18 +1207,17 @@ impl<'a> Resolver<'a> { ); let definitions = Definitions::new(session.local_stable_crate_id(), krate.spans.inner_span); - let root = definitions.get_root_def(); let mut visibilities = FxHashMap::default(); visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public); let mut def_id_to_node_id = IndexVec::default(); - assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), root); + assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID); let mut node_id_to_def_id = FxHashMap::default(); - node_id_to_def_id.insert(CRATE_NODE_ID, root); + node_id_to_def_id.insert(CRATE_NODE_ID, CRATE_DEF_ID); let mut invocation_parents = FxHashMap::default(); - invocation_parents.insert(LocalExpnId::ROOT, (root, ImplTraitContext::Existential)); + invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential)); let mut extern_prelude: FxHashMap> = session .opts diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 6d1b36796d869..d5f806308cf41 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -283,8 +283,19 @@ impl DefId { self.as_local().unwrap_or_else(|| panic!("DefId::expect_local: `{:?}` isn't local", self)) } + #[inline] + pub fn is_crate_root(self) -> bool { + self.index == CRATE_DEF_INDEX + } + + #[inline] + pub fn as_crate_root(self) -> Option { + if self.is_crate_root() { Some(self.krate) } else { None } + } + + #[inline] pub fn is_top_level_module(self) -> bool { - self.is_local() && self.index == CRATE_DEF_INDEX + self.is_local() && self.is_crate_root() } } @@ -357,7 +368,7 @@ impl LocalDefId { #[inline] pub fn is_top_level_module(self) -> bool { - self.local_def_index == CRATE_DEF_INDEX + self == CRATE_DEF_ID } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 21016afbf5f99..a070cef227252 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -15,7 +15,7 @@ use rustc_attr as attr; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData}; use rustc_middle::middle::resolve_lifetime as rl; use rustc_middle::ty::fold::TypeFolder; @@ -1975,7 +1975,7 @@ fn clean_extern_crate( // this is the ID of the `extern crate` statement let cnum = cx.tcx.extern_mod_stmt_cnum(krate.def_id).unwrap_or(LOCAL_CRATE); // this is the ID of the crate itself - let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; + let crate_def_id = cnum.as_def_id(); let attrs = cx.tcx.hir().attrs(krate.hir_id()); let ty_vis = cx.tcx.visibility(krate.def_id); let please_inline = ty_vis.is_public() @@ -2094,7 +2094,7 @@ fn clean_use_statement( } else { if inline_attr.is_none() { if let Res::Def(DefKind::Mod, did) = path.res { - if !did.is_local() && did.index == CRATE_DEF_INDEX { + if !did.is_local() && did.is_crate_root() { // if we're `pub use`ing an extern crate root, don't inline it unless we // were specifically asked for it denied = true; diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4b473df155f58..e30bc6e0a97ee 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -20,7 +20,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::thin_vec::ThinVec; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyId, Mutability}; use rustc_index::vec::IndexVec; @@ -104,14 +104,6 @@ impl ItemId { ItemId::Primitive(_, krate) => krate, } } - - #[inline] - crate fn index(self) -> Option { - match self { - ItemId::DefId(id) => Some(id.index), - _ => None, - } - } } impl From for ItemId { @@ -160,7 +152,7 @@ impl ExternalCrate { #[inline] crate fn def_id(&self) -> DefId { - DefId { krate: self.crate_num, index: CRATE_DEF_INDEX } + self.crate_num.as_def_id() } crate fn src(&self, tcx: TyCtxt<'_>) -> FileName { @@ -217,7 +209,7 @@ impl ExternalCrate { // Failing that, see if there's an attribute specifying where to find this // external crate - let did = DefId { krate: self.crate_num, index: CRATE_DEF_INDEX }; + let did = self.crate_num.as_def_id(); tcx.get_attrs(did) .lists(sym::doc) .filter(|a| a.has_name(sym::html_root_url)) @@ -559,7 +551,7 @@ impl Item { } crate fn is_crate(&self) -> bool { - self.is_mod() && self.item_id.as_def_id().map_or(false, |did| did.index == CRATE_DEF_INDEX) + self.is_mod() && self.item_id.as_def_id().map_or(false, |did| did.is_crate_root()) } crate fn is_mod(&self) -> bool { self.type_() == ItemType::Module diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index e138e434c4e04..efd1a78a9d9a5 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -1,7 +1,7 @@ use std::mem; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; -use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId}; use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::ty::TyCtxt; use rustc_span::{sym, Symbol}; @@ -293,7 +293,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { // A crate has a module at its root, containing all items, // which should not be indexed. The crate-item itself is // inserted later on when serializing the search-index. - if item.item_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) { + if item.item_id.as_def_id().map_or(false, |idx| !idx.is_crate_root()) { let desc = item.doc_value().map_or_else(String::new, |x| { short_markdown_summary(x.as_str(), &item.link_names(self.cache)) }); diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 55b0028180f66..6954e2363f5f0 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -18,7 +18,6 @@ use rustc_hir::def_id::DefId; use rustc_middle::ty; use rustc_middle::ty::DefIdTree; use rustc_middle::ty::TyCtxt; -use rustc_span::def_id::CRATE_DEF_INDEX; use rustc_span::{sym, Symbol}; use rustc_target::spec::abi::Abi; @@ -1312,7 +1311,7 @@ impl clean::Visibility { // visibility, so it shouldn't matter. let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id()); - if vis_did.index == CRATE_DEF_INDEX { + if vis_did.is_crate_root() { "pub(crate) ".to_owned() } else if parent_module == Some(vis_did) { // `pub(in foo)` where `foo` is the parent module @@ -1360,7 +1359,7 @@ impl clean::Visibility { // visibility, so it shouldn't matter. let parent_module = find_nearest_parent_module(tcx, item_did); - if vis_did.index == CRATE_DEF_INDEX { + if vis_did.is_crate_root() { "pub(crate) ".to_owned() } else if parent_module == Some(vis_did) { // `pub(in foo)` where `foo` is the parent module diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 0b5fb48059579..56b02cd848041 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -10,7 +10,6 @@ use std::fmt; use rustc_ast::ast; use rustc_hir::{def::CtorKind, def_id::DefId}; use rustc_middle::ty::{self, TyCtxt}; -use rustc_span::def_id::CRATE_DEF_INDEX; use rustc_span::Pos; use rustc_target::spec::abi::Abi as RustcAbi; @@ -83,7 +82,7 @@ impl JsonRenderer<'_> { match v { Public => Visibility::Public, Inherited => Visibility::Default, - Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate, + Restricted(did) if did.is_crate_root() => Visibility::Crate, Restricted(did) => Visibility::Restricted { parent: from_item_id(did.into()), path: self.tcx.def_path(did).to_string_no_crate_verbose(), diff --git a/src/librustdoc/visit_lib.rs b/src/librustdoc/visit_lib.rs index 5bcec779bc0e7..9723cdbe3345f 100644 --- a/src/librustdoc/visit_lib.rs +++ b/src/librustdoc/visit_lib.rs @@ -1,6 +1,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId}; use rustc_middle::middle::privacy::{AccessLevel, AccessLevels}; use rustc_middle::ty::TyCtxt; @@ -29,7 +29,7 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> { } crate fn visit_lib(&mut self, cnum: CrateNum) { - let did = DefId { krate: cnum, index: CRATE_DEF_INDEX }; + let did = cnum.as_def_id(); self.update(did, Some(AccessLevel::Public)); self.visit_mod(did); } diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index fc0483a929a7a..5816a95dcebff 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -10,7 +10,7 @@ use clippy_utils::diagnostics::span_lint; use rustc_ast::ast; use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass, LintContext}; -use rustc_middle::ty; +use rustc_middle::ty::{self, DefIdTree}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::source_map::Span; @@ -114,8 +114,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { hir::ItemKind::Fn(..) => { // ignore main() if it.ident.name == sym::main { - let def_key = cx.tcx.hir().def_key(it.def_id); - if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) { + let at_root = cx.tcx.local_parent(it.def_id) == Some(CRATE_DEF_ID); + if at_root { return; } }