Skip to content

Commit 347fa7a

Browse files
committedJan 26, 2023
rustdoc: Stop using HirIds
Use `LocalDefId`s instead
·
1.89.01.69.0
1 parent e187f88 commit 347fa7a

File tree

10 files changed

+113
-151
lines changed

10 files changed

+113
-151
lines changed
 

‎src/librustdoc/clean/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_attr as attr;
1515
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
1616
use rustc_hir as hir;
1717
use rustc_hir::def::{CtorKind, DefKind, Res};
18-
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE};
18+
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
1919
use rustc_hir::PredicateOrigin;
2020
use rustc_hir_analysis::hir_ty_to_ty;
2121
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
@@ -116,7 +116,8 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
116116
}
117117
});
118118

119-
Item::from_hir_id_and_parts(doc.id, Some(doc.name), ModuleItem(Module { items, span }), cx)
119+
let kind = ModuleItem(Module { items, span });
120+
Item::from_def_id_and_parts(doc.def_id.to_def_id(), Some(doc.name), kind, cx)
120121
}
121122

122123
fn clean_generic_bound<'tcx>(
@@ -2067,12 +2068,12 @@ struct OneLevelVisitor<'hir> {
20672068
map: rustc_middle::hir::map::Map<'hir>,
20682069
item: Option<&'hir hir::Item<'hir>>,
20692070
looking_for: Ident,
2070-
target_hir_id: hir::HirId,
2071+
target_def_id: LocalDefId,
20712072
}
20722073

20732074
impl<'hir> OneLevelVisitor<'hir> {
2074-
fn new(map: rustc_middle::hir::map::Map<'hir>, target_hir_id: hir::HirId) -> Self {
2075-
Self { map, item: None, looking_for: Ident::empty(), target_hir_id }
2075+
fn new(map: rustc_middle::hir::map::Map<'hir>, target_def_id: LocalDefId) -> Self {
2076+
Self { map, item: None, looking_for: Ident::empty(), target_def_id }
20762077
}
20772078

20782079
fn reset(&mut self, looking_for: Ident) {
@@ -2092,7 +2093,7 @@ impl<'hir> hir::intravisit::Visitor<'hir> for OneLevelVisitor<'hir> {
20922093
if self.item.is_none()
20932094
&& item.ident == self.looking_for
20942095
&& matches!(item.kind, hir::ItemKind::Use(_, _))
2095-
|| item.hir_id() == self.target_hir_id
2096+
|| item.owner_id.def_id == self.target_def_id
20962097
{
20972098
self.item = Some(item);
20982099
}
@@ -2106,11 +2107,11 @@ impl<'hir> hir::intravisit::Visitor<'hir> for OneLevelVisitor<'hir> {
21062107
fn get_all_import_attributes<'hir>(
21072108
mut item: &hir::Item<'hir>,
21082109
tcx: TyCtxt<'hir>,
2109-
target_hir_id: hir::HirId,
2110+
target_def_id: LocalDefId,
21102111
attributes: &mut Vec<ast::Attribute>,
21112112
) {
21122113
let hir_map = tcx.hir();
2113-
let mut visitor = OneLevelVisitor::new(hir_map, target_hir_id);
2114+
let mut visitor = OneLevelVisitor::new(hir_map, target_def_id);
21142115
// If the item is an import and has at least a path with two parts, we go into it.
21152116
while let hir::ItemKind::Use(path, _) = item.kind &&
21162117
path.segments.len() > 1 &&
@@ -2138,7 +2139,7 @@ fn clean_maybe_renamed_item<'tcx>(
21382139
cx: &mut DocContext<'tcx>,
21392140
item: &hir::Item<'tcx>,
21402141
renamed: Option<Symbol>,
2141-
import_id: Option<hir::HirId>,
2142+
import_id: Option<LocalDefId>,
21422143
) -> Vec<Item> {
21432144
use hir::ItemKind;
21442145

@@ -2183,7 +2184,7 @@ fn clean_maybe_renamed_item<'tcx>(
21832184
generics: clean_generics(generics, cx),
21842185
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
21852186
}),
2186-
ItemKind::Impl(impl_) => return clean_impl(impl_, item.hir_id(), cx),
2187+
ItemKind::Impl(impl_) => return clean_impl(impl_, item.owner_id.def_id, cx),
21872188
// proc macros can have a name set by attributes
21882189
ItemKind::Fn(ref sig, generics, body_id) => {
21892190
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
@@ -2218,10 +2219,10 @@ fn clean_maybe_renamed_item<'tcx>(
22182219

22192220
let mut extra_attrs = Vec::new();
22202221
if let Some(hir::Node::Item(use_node)) =
2221-
import_id.and_then(|hir_id| cx.tcx.hir().find(hir_id))
2222+
import_id.and_then(|def_id| cx.tcx.hir().find_by_def_id(def_id))
22222223
{
22232224
// We get all the various imports' attributes.
2224-
get_all_import_attributes(use_node, cx.tcx, item.hir_id(), &mut extra_attrs);
2225+
get_all_import_attributes(use_node, cx.tcx, item.owner_id.def_id, &mut extra_attrs);
22252226
}
22262227

22272228
if !extra_attrs.is_empty() {
@@ -2244,12 +2245,12 @@ fn clean_maybe_renamed_item<'tcx>(
22442245

22452246
fn clean_variant<'tcx>(variant: &hir::Variant<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
22462247
let kind = VariantItem(clean_variant_data(&variant.data, &variant.disr_expr, cx));
2247-
Item::from_hir_id_and_parts(variant.hir_id, Some(variant.ident.name), kind, cx)
2248+
Item::from_def_id_and_parts(variant.def_id.to_def_id(), Some(variant.ident.name), kind, cx)
22482249
}
22492250

22502251
fn clean_impl<'tcx>(
22512252
impl_: &hir::Impl<'tcx>,
2252-
hir_id: hir::HirId,
2253+
def_id: LocalDefId,
22532254
cx: &mut DocContext<'tcx>,
22542255
) -> Vec<Item> {
22552256
let tcx = cx.tcx;
@@ -2260,7 +2261,6 @@ fn clean_impl<'tcx>(
22602261
.iter()
22612262
.map(|ii| clean_impl_item(tcx.hir().impl_item(ii.id), cx))
22622263
.collect::<Vec<_>>();
2263-
let def_id = tcx.hir().local_def_id(hir_id);
22642264

22652265
// If this impl block is an implementation of the Deref trait, then we
22662266
// need to try inlining the target's inherent impl blocks as well.
@@ -2289,7 +2289,7 @@ fn clean_impl<'tcx>(
22892289
ImplKind::Normal
22902290
},
22912291
}));
2292-
Item::from_hir_id_and_parts(hir_id, None, kind, cx)
2292+
Item::from_def_id_and_parts(def_id.to_def_id(), None, kind, cx)
22932293
};
22942294
if let Some(type_alias) = type_alias {
22952295
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
@@ -2510,8 +2510,8 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
25102510
hir::ForeignItemKind::Type => ForeignTypeItem,
25112511
};
25122512

2513-
Item::from_hir_id_and_parts(
2514-
item.hir_id(),
2513+
Item::from_def_id_and_parts(
2514+
item.owner_id.def_id.to_def_id(),
25152515
Some(renamed.unwrap_or(item.ident.name)),
25162516
kind,
25172517
cx,

‎src/librustdoc/clean/types.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,6 @@ impl Item {
439439
self.attrs.doc_value()
440440
}
441441

442-
/// Convenience wrapper around [`Self::from_def_id_and_parts`] which converts
443-
/// `hir_id` to a [`DefId`]
444-
pub(crate) fn from_hir_id_and_parts(
445-
hir_id: hir::HirId,
446-
name: Option<Symbol>,
447-
kind: ItemKind,
448-
cx: &mut DocContext<'_>,
449-
) -> Item {
450-
Item::from_def_id_and_parts(cx.tcx.hir().local_def_id(hir_id).to_def_id(), name, kind, cx)
451-
}
452-
453442
pub(crate) fn from_def_id_and_parts(
454443
def_id: DefId,
455444
name: Option<Symbol>,
@@ -2416,10 +2405,7 @@ impl ConstantKind {
24162405

24172406
pub(crate) fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
24182407
match *self {
2419-
ConstantKind::TyConst { .. } => false,
2420-
ConstantKind::Extern { def_id } => def_id.as_local().map_or(false, |def_id| {
2421-
is_literal_expr(tcx, tcx.hir().local_def_id_to_hir_id(def_id))
2422-
}),
2408+
ConstantKind::TyConst { .. } | ConstantKind::Extern { .. } => false,
24232409
ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
24242410
is_literal_expr(tcx, body.hir_id)
24252411
}

‎src/librustdoc/doctest.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use rustc_ast as ast;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_data_structures::sync::Lrc;
44
use rustc_errors::{ColorConfig, ErrorGuaranteed, FatalError};
5-
use rustc_hir as hir;
6-
use rustc_hir::def_id::LOCAL_CRATE;
7-
use rustc_hir::intravisit;
8-
use rustc_hir::{HirId, CRATE_HIR_ID};
5+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
6+
use rustc_hir::{self as hir, intravisit, CRATE_HIR_ID};
97
use rustc_interface::interface;
108
use rustc_middle::hir::map::Map;
119
use rustc_middle::hir::nested_filter;
@@ -140,7 +138,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
140138
};
141139
hir_collector.visit_testable(
142140
"".to_string(),
143-
CRATE_HIR_ID,
141+
CRATE_DEF_ID,
144142
tcx.hir().span(CRATE_HIR_ID),
145143
|this| tcx.hir().walk_toplevel_module(this),
146144
);
@@ -1214,11 +1212,11 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
12141212
fn visit_testable<F: FnOnce(&mut Self)>(
12151213
&mut self,
12161214
name: String,
1217-
hir_id: HirId,
1215+
def_id: LocalDefId,
12181216
sp: Span,
12191217
nested: F,
12201218
) {
1221-
let ast_attrs = self.tcx.hir().attrs(hir_id);
1219+
let ast_attrs = self.tcx.hir().attrs(self.tcx.hir().local_def_id_to_hir_id(def_id));
12221220
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
12231221
if !cfg.matches(&self.sess.parse_sess, Some(self.tcx.features())) {
12241222
return;
@@ -1247,7 +1245,7 @@ impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
12471245
self.collector.enable_per_target_ignores,
12481246
Some(&crate::html::markdown::ExtraInfo::new(
12491247
self.tcx,
1250-
hir_id,
1248+
def_id.to_def_id(),
12511249
span_of_attrs(&attrs).unwrap_or(sp),
12521250
)),
12531251
);
@@ -1276,37 +1274,37 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
12761274
_ => item.ident.to_string(),
12771275
};
12781276

1279-
self.visit_testable(name, item.hir_id(), item.span, |this| {
1277+
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
12801278
intravisit::walk_item(this, item);
12811279
});
12821280
}
12831281

12841282
fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
1285-
self.visit_testable(item.ident.to_string(), item.hir_id(), item.span, |this| {
1283+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
12861284
intravisit::walk_trait_item(this, item);
12871285
});
12881286
}
12891287

12901288
fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
1291-
self.visit_testable(item.ident.to_string(), item.hir_id(), item.span, |this| {
1289+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
12921290
intravisit::walk_impl_item(this, item);
12931291
});
12941292
}
12951293

12961294
fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
1297-
self.visit_testable(item.ident.to_string(), item.hir_id(), item.span, |this| {
1295+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
12981296
intravisit::walk_foreign_item(this, item);
12991297
});
13001298
}
13011299

13021300
fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
1303-
self.visit_testable(v.ident.to_string(), v.hir_id, v.span, |this| {
1301+
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
13041302
intravisit::walk_variant(this, v);
13051303
});
13061304
}
13071305

13081306
fn visit_field_def(&mut self, f: &'hir hir::FieldDef<'_>) {
1309-
self.visit_testable(f.ident.to_string(), f.hir_id, f.span, |this| {
1307+
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
13101308
intravisit::walk_field_def(this, f);
13111309
});
13121310
}

‎src/librustdoc/html/markdown.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
2828
use rustc_data_structures::fx::FxHashMap;
2929
use rustc_hir::def_id::DefId;
30-
use rustc_hir::HirId;
3130
use rustc_middle::ty::TyCtxt;
3231
use rustc_span::edition::Edition;
3332
use rustc_span::{Span, Symbol};
@@ -784,45 +783,26 @@ pub(crate) fn find_testable_code<T: doctest::Tester>(
784783
}
785784

786785
pub(crate) struct ExtraInfo<'tcx> {
787-
id: ExtraInfoId,
786+
def_id: DefId,
788787
sp: Span,
789788
tcx: TyCtxt<'tcx>,
790789
}
791790

792-
enum ExtraInfoId {
793-
Hir(HirId),
794-
Def(DefId),
795-
}
796-
797791
impl<'tcx> ExtraInfo<'tcx> {
798-
pub(crate) fn new(tcx: TyCtxt<'tcx>, hir_id: HirId, sp: Span) -> ExtraInfo<'tcx> {
799-
ExtraInfo { id: ExtraInfoId::Hir(hir_id), sp, tcx }
800-
}
801-
802-
pub(crate) fn new_did(tcx: TyCtxt<'tcx>, did: DefId, sp: Span) -> ExtraInfo<'tcx> {
803-
ExtraInfo { id: ExtraInfoId::Def(did), sp, tcx }
792+
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: DefId, sp: Span) -> ExtraInfo<'tcx> {
793+
ExtraInfo { def_id, sp, tcx }
804794
}
805795

806796
fn error_invalid_codeblock_attr(&self, msg: &str, help: &str) {
807-
let hir_id = match self.id {
808-
ExtraInfoId::Hir(hir_id) => hir_id,
809-
ExtraInfoId::Def(item_did) => {
810-
match item_did.as_local() {
811-
Some(item_did) => self.tcx.hir().local_def_id_to_hir_id(item_did),
812-
None => {
813-
// If non-local, no need to check anything.
814-
return;
815-
}
816-
}
817-
}
818-
};
819-
self.tcx.struct_span_lint_hir(
820-
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
821-
hir_id,
822-
self.sp,
823-
msg,
824-
|lint| lint.help(help),
825-
);
797+
if let Some(def_id) = self.def_id.as_local() {
798+
self.tcx.struct_span_lint_hir(
799+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
800+
self.tcx.hir().local_def_id_to_hir_id(def_id),
801+
self.sp,
802+
msg,
803+
|lint| lint.help(help),
804+
);
805+
}
826806
}
827807
}
828808

‎src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,7 @@ impl<'a, 'b> DocVisitor for CoverageCalculator<'a, 'b> {
216216
);
217217

218218
let has_doc_example = tests.found_tests != 0;
219-
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
220-
// would presumably panic if a fake `DefIndex` were passed.
221-
let hir_id = self
222-
.ctx
223-
.tcx
224-
.hir()
225-
.local_def_id_to_hir_id(i.item_id.expect_def_id().expect_local());
219+
let hir_id = DocContext::as_local_hir_id(self.ctx.tcx, i.item_id).unwrap();
226220
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
227221

228222
// In case we have:

‎src/librustdoc/passes/check_doc_test_visibility.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::visit::DocVisitor;
1414
use crate::visit_ast::inherits_doc_hidden;
1515
use rustc_hir as hir;
1616
use rustc_middle::lint::LintLevelSource;
17+
use rustc_middle::ty::DefIdTree;
1718
use rustc_session::lint;
18-
use rustc_span::symbol::sym;
1919

2020
pub(crate) const CHECK_DOC_TEST_VISIBILITY: Pass = Pass {
2121
name: "check_doc_test_visibility",
@@ -79,11 +79,11 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
7979

8080
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
8181
// would presumably panic if a fake `DefIndex` were passed.
82-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.item_id.expect_def_id().expect_local());
82+
let def_id = item.item_id.expect_def_id().expect_local();
8383

8484
// check if parent is trait impl
85-
if let Some(parent_hir_id) = cx.tcx.hir().opt_parent_id(hir_id) {
86-
if let Some(parent_node) = cx.tcx.hir().find(parent_hir_id) {
85+
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id) {
86+
if let Some(parent_node) = cx.tcx.hir().find_by_def_id(parent_def_id) {
8787
if matches!(
8888
parent_node,
8989
hir::Node::Item(hir::Item {
@@ -96,13 +96,16 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
9696
}
9797
}
9898

99-
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
100-
|| inherits_doc_hidden(cx.tcx, hir_id)
101-
|| cx.tcx.hir().span(hir_id).in_derive_expansion()
99+
if cx.tcx.is_doc_hidden(def_id.to_def_id())
100+
|| inherits_doc_hidden(cx.tcx, def_id)
101+
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
102102
{
103103
return false;
104104
}
105-
let (level, source) = cx.tcx.lint_level_at_node(crate::lint::MISSING_DOC_CODE_EXAMPLES, hir_id);
105+
let (level, source) = cx.tcx.lint_level_at_node(
106+
crate::lint::MISSING_DOC_CODE_EXAMPLES,
107+
cx.tcx.hir().local_def_id_to_hir_id(def_id),
108+
);
106109
level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
107110
}
108111

‎src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,14 +1194,9 @@ impl LinkCollector<'_, '_> {
11941194
}
11951195

11961196
// item can be non-local e.g. when using #[doc(primitive = "pointer")]
1197-
if let Some((src_id, dst_id)) = id
1198-
.as_local()
1199-
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
1200-
// would presumably panic if a fake `DefIndex` were passed.
1201-
.and_then(|dst_id| {
1202-
item.item_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
1203-
})
1204-
{
1197+
if let Some((src_id, dst_id)) = id.as_local().and_then(|dst_id| {
1198+
item.item_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
1199+
}) {
12051200
if self.cx.tcx.effective_visibilities(()).is_exported(src_id)
12061201
&& !self.cx.tcx.effective_visibilities(()).is_exported(dst_id)
12071202
{

‎src/librustdoc/passes/lint/check_code_block_syntax.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use crate::passes::source_span_for_markdown_range;
1919
pub(crate) fn visit_item(cx: &DocContext<'_>, item: &clean::Item) {
2020
if let Some(dox) = &item.attrs.collapsed_doc_value() {
2121
let sp = item.attr_span(cx.tcx);
22-
let extra =
23-
crate::html::markdown::ExtraInfo::new_did(cx.tcx, item.item_id.expect_def_id(), sp);
22+
let extra = crate::html::markdown::ExtraInfo::new(cx.tcx, item.item_id.expect_def_id(), sp);
2423
for code_block in markdown::rust_code_blocks(dox, &extra) {
2524
check_rust_syntax(cx, item, dox, code_block);
2625
}
@@ -73,7 +72,6 @@ fn check_rust_syntax(
7372
return;
7473
};
7574

76-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_id);
7775
let empty_block = code_block.lang_string == Default::default() && code_block.is_fenced;
7876
let is_ignore = code_block.lang_string.ignore != markdown::Ignore::None;
7977

@@ -93,6 +91,7 @@ fn check_rust_syntax(
9391
// Finally build and emit the completed diagnostic.
9492
// All points of divergence have been handled earlier so this can be
9593
// done the same way whether the span is precise or not.
94+
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_id);
9695
cx.tcx.struct_span_lint_hir(crate::lint::INVALID_RUST_CODEBLOCKS, hir_id, sp, msg, |lint| {
9796
let explanation = if is_ignore {
9897
"`ignore` code blocks require valid Rust code for syntax highlighting; \

‎src/librustdoc/passes/propagate_doc_cfg.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::fold::DocFolder;
99
use crate::passes::Pass;
1010

1111
use rustc_hir::def_id::LocalDefId;
12+
use rustc_middle::ty::DefIdTree;
1213

1314
pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass {
1415
name: "propagate-doc-cfg",
@@ -41,24 +42,22 @@ impl<'a, 'tcx> CfgPropagator<'a, 'tcx> {
4142
let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local())
4243
else { return };
4344

44-
let hir = self.cx.tcx.hir();
45-
let hir_id = hir.local_def_id_to_hir_id(def_id);
46-
4745
if check_parent {
48-
let expected_parent = hir.get_parent_item(hir_id);
46+
let expected_parent = self.cx.tcx.opt_local_parent(def_id);
4947
// If parents are different, it means that `item` is a reexport and we need
5048
// to compute the actual `cfg` by iterating through its "real" parents.
51-
if self.parent == Some(expected_parent.def_id) {
49+
if self.parent.is_some() && self.parent == expected_parent {
5250
return;
5351
}
5452
}
5553

5654
let mut attrs = Vec::new();
57-
for (parent_hir_id, _) in hir.parent_iter(hir_id) {
58-
if let Some(def_id) = hir.opt_local_def_id(parent_hir_id) {
59-
attrs.extend_from_slice(load_attrs(self.cx, def_id.to_def_id()));
60-
}
55+
let mut next_def_id = def_id;
56+
while let Some(parent_def_id) = self.cx.tcx.opt_local_parent(next_def_id) {
57+
attrs.extend_from_slice(load_attrs(self.cx, parent_def_id.to_def_id()));
58+
next_def_id = parent_def_id;
6159
}
60+
6261
let (_, cfg) = merge_attrs(self.cx, None, item.attrs.other_attrs.as_slice(), Some(&attrs));
6362
item.cfg = cfg;
6463
}

‎src/librustdoc/visit_ast.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::def_id::{DefId, DefIdMap};
8-
use rustc_hir::{HirIdSet, Node, CRATE_HIR_ID};
9-
use rustc_middle::ty::TyCtxt;
7+
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
8+
use rustc_hir::{Node, CRATE_HIR_ID};
9+
use rustc_middle::ty::{DefIdTree, TyCtxt};
1010
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
1111
use rustc_span::symbol::{kw, sym, Symbol};
1212
use rustc_span::Span;
@@ -23,19 +23,26 @@ pub(crate) struct Module<'hir> {
2323
pub(crate) name: Symbol,
2424
pub(crate) where_inner: Span,
2525
pub(crate) mods: Vec<Module<'hir>>,
26-
pub(crate) id: hir::HirId,
26+
pub(crate) def_id: LocalDefId,
2727
// (item, renamed, import_id)
28-
pub(crate) items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>, Option<hir::HirId>)>,
28+
pub(crate) items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>)>,
2929
pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
3030
}
3131

3232
impl Module<'_> {
33-
pub(crate) fn new(name: Symbol, id: hir::HirId, where_inner: Span) -> Self {
34-
Module { name, id, where_inner, mods: Vec::new(), items: Vec::new(), foreigns: Vec::new() }
33+
pub(crate) fn new(name: Symbol, def_id: LocalDefId, where_inner: Span) -> Self {
34+
Module {
35+
name,
36+
def_id,
37+
where_inner,
38+
mods: Vec::new(),
39+
items: Vec::new(),
40+
foreigns: Vec::new(),
41+
}
3542
}
3643

3744
pub(crate) fn where_outer(&self, tcx: TyCtxt<'_>) -> Span {
38-
tcx.hir().span(self.id)
45+
tcx.def_span(self.def_id)
3946
}
4047
}
4148

@@ -46,10 +53,10 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
4653
std::iter::once(crate_name).chain(relative).collect()
4754
}
4855

49-
pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool {
50-
while let Some(id) = tcx.hir().get_enclosing_scope(node) {
56+
pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: LocalDefId) -> bool {
57+
while let Some(id) = tcx.opt_local_parent(node) {
5158
node = id;
52-
if tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
59+
if tcx.is_doc_hidden(node.to_def_id()) {
5360
return true;
5461
}
5562
}
@@ -61,7 +68,7 @@ pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool
6168

6269
pub(crate) struct RustdocVisitor<'a, 'tcx> {
6370
cx: &'a mut core::DocContext<'tcx>,
64-
view_item_stack: HirIdSet,
71+
view_item_stack: LocalDefIdSet,
6572
inlining: bool,
6673
/// Are the current module and all of its parents public?
6774
inside_public_path: bool,
@@ -71,8 +78,8 @@ pub(crate) struct RustdocVisitor<'a, 'tcx> {
7178
impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7279
pub(crate) fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
7380
// If the root is re-exported, terminate all recursion.
74-
let mut stack = HirIdSet::default();
75-
stack.insert(hir::CRATE_HIR_ID);
81+
let mut stack = LocalDefIdSet::default();
82+
stack.insert(CRATE_DEF_ID);
7683
RustdocVisitor {
7784
cx,
7885
view_item_stack: stack,
@@ -89,7 +96,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
8996

9097
pub(crate) fn visit(mut self) -> Module<'tcx> {
9198
let mut top_level_module = self.visit_mod_contents(
92-
hir::CRATE_HIR_ID,
99+
CRATE_DEF_ID,
93100
self.cx.tcx.hir().root_module(),
94101
self.cx.tcx.crate_name(LOCAL_CRATE),
95102
None,
@@ -152,16 +159,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
152159

153160
fn visit_mod_contents(
154161
&mut self,
155-
id: hir::HirId,
162+
def_id: LocalDefId,
156163
m: &'tcx hir::Mod<'tcx>,
157164
name: Symbol,
158-
parent_id: Option<hir::HirId>,
165+
parent_id: Option<LocalDefId>,
159166
) -> Module<'tcx> {
160-
let mut om = Module::new(name, id, m.spans.inner_span);
161-
let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id();
167+
let mut om = Module::new(name, def_id, m.spans.inner_span);
162168
// Keep track of if there were any private modules in the path.
163169
let orig_inside_public_path = self.inside_public_path;
164-
self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public();
170+
self.inside_public_path &= self.cx.tcx.local_visibility(def_id).is_public();
165171
for &i in m.item_ids {
166172
let item = self.cx.tcx.hir().item(i);
167173
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
@@ -193,7 +199,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
193199
/// Returns `true` if the target has been inlined.
194200
fn maybe_inline_local(
195201
&mut self,
196-
id: hir::HirId,
202+
def_id: LocalDefId,
197203
res: Res,
198204
renamed: Option<Symbol>,
199205
glob: bool,
@@ -211,10 +217,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
211217
return false;
212218
};
213219

214-
let use_attrs = tcx.hir().attrs(id);
220+
let use_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
215221
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
216222
let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
217-
|| use_attrs.lists(sym::doc).has_word(sym::hidden);
223+
|| tcx.is_doc_hidden(def_id.to_def_id());
218224

219225
// For cross-crate impl inlining we need to know whether items are
220226
// reachable in documentation -- a previously unreachable item can be
@@ -225,37 +231,39 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
225231
return false;
226232
}
227233

228-
let res_hir_id = match res_did.as_local() {
229-
Some(n) => tcx.hir().local_def_id_to_hir_id(n),
230-
None => return false,
234+
let Some(res_did) = res_did.as_local() else {
235+
return false;
231236
};
232237

233-
let is_private =
234-
!self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, res_did);
235-
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);
238+
let is_private = !self
239+
.cx
240+
.cache
241+
.effective_visibilities
242+
.is_directly_public(self.cx.tcx, res_did.to_def_id());
243+
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_did);
236244

237245
// Only inline if requested or if the item would otherwise be stripped.
238246
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
239247
return false;
240248
}
241249

242-
if !self.view_item_stack.insert(res_hir_id) {
250+
if !self.view_item_stack.insert(res_did) {
243251
return false;
244252
}
245253

246-
let ret = match tcx.hir().get(res_hir_id) {
254+
let ret = match tcx.hir().get_by_def_id(res_did) {
247255
Node::Item(&hir::Item { kind: hir::ItemKind::Mod(ref m), .. }) if glob => {
248256
let prev = mem::replace(&mut self.inlining, true);
249257
for &i in m.item_ids {
250258
let i = self.cx.tcx.hir().item(i);
251-
self.visit_item(i, None, om, Some(id));
259+
self.visit_item(i, None, om, Some(def_id));
252260
}
253261
self.inlining = prev;
254262
true
255263
}
256264
Node::Item(it) if !glob => {
257265
let prev = mem::replace(&mut self.inlining, true);
258-
self.visit_item(it, renamed, om, Some(id));
266+
self.visit_item(it, renamed, om, Some(def_id));
259267
self.inlining = prev;
260268
true
261269
}
@@ -267,7 +275,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
267275
}
268276
_ => false,
269277
};
270-
self.view_item_stack.remove(&res_hir_id);
278+
self.view_item_stack.remove(&res_did);
271279
ret
272280
}
273281

@@ -276,7 +284,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
276284
item: &'tcx hir::Item<'_>,
277285
renamed: Option<Symbol>,
278286
om: &mut Module<'tcx>,
279-
parent_id: Option<hir::HirId>,
287+
parent_id: Option<LocalDefId>,
280288
) {
281289
debug!("visiting item {:?}", item);
282290
let name = renamed.unwrap_or(item.ident.name);
@@ -321,7 +329,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
321329
let is_glob = kind == hir::UseKind::Glob;
322330
let ident = if is_glob { None } else { Some(name) };
323331
if self.maybe_inline_local(
324-
item.hir_id(),
332+
item.owner_id.def_id,
325333
res,
326334
ident,
327335
is_glob,
@@ -356,7 +364,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
356364
}
357365
}
358366
hir::ItemKind::Mod(ref m) => {
359-
om.mods.push(self.visit_mod_contents(item.hir_id(), m, name, parent_id));
367+
om.mods.push(self.visit_mod_contents(item.owner_id.def_id, m, name, parent_id));
360368
}
361369
hir::ItemKind::Fn(..)
362370
| hir::ItemKind::ExternCrate(..)

0 commit comments

Comments
 (0)
Please sign in to comment.