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.
This commit is contained in:
commit
9fad214593
30 changed files with 109 additions and 147 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<DefIndex> {
|
||||
match self {
|
||||
ItemId::DefId(id) => Some(id.index),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DefId> 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
|
||||
|
|
|
|||
|
|
@ -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};
|
||||
|
|
@ -302,7 +302,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))
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue