Skip to content

Commit eee6648

Browse files
authored
Rollup merge of rust-lang#107325 - petrochenkov:hiddoc2, r=GuillaumeGomez
rustdoc: Stop using `HirId`s Use `LocalDefId`s instead. Rustdoc doesn't work with item bodies, so it almost never needs fine-grained HIR IDs.
2 parents 74945ca + 347fa7a commit eee6648

10 files changed

+113
-151
lines changed

src/librustdoc/clean/mod.rs

+18-18
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

+1-15
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

+12-14
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

+12-32
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};
@@ -801,45 +800,26 @@ pub(crate) fn find_testable_code<T: doctest::Tester>(
801800
}
802801

803802
pub(crate) struct ExtraInfo<'tcx> {
804-
id: ExtraInfoId,
803+
def_id: DefId,
805804
sp: Span,
806805
tcx: TyCtxt<'tcx>,
807806
}
808807

809-
enum ExtraInfoId {
810-
Hir(HirId),
811-
Def(DefId),
812-
}
813-
814808
impl<'tcx> ExtraInfo<'tcx> {
815-
pub(crate) fn new(tcx: TyCtxt<'tcx>, hir_id: HirId, sp: Span) -> ExtraInfo<'tcx> {
816-
ExtraInfo { id: ExtraInfoId::Hir(hir_id), sp, tcx }
817-
}
818-
819-
pub(crate) fn new_did(tcx: TyCtxt<'tcx>, did: DefId, sp: Span) -> ExtraInfo<'tcx> {
820-
ExtraInfo { id: ExtraInfoId::Def(did), sp, tcx }
809+
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: DefId, sp: Span) -> ExtraInfo<'tcx> {
810+
ExtraInfo { def_id, sp, tcx }
821811
}
822812

823813
fn error_invalid_codeblock_attr(&self, msg: &str, help: &str) {
824-
let hir_id = match self.id {
825-
ExtraInfoId::Hir(hir_id) => hir_id,
826-
ExtraInfoId::Def(item_did) => {
827-
match item_did.as_local() {
828-
Some(item_did) => self.tcx.hir().local_def_id_to_hir_id(item_did),
829-
None => {
830-
// If non-local, no need to check anything.
831-
return;
832-
}
833-
}
834-
}
835-
};
836-
self.tcx.struct_span_lint_hir(
837-
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
838-
hir_id,
839-
self.sp,
840-
msg,
841-
|lint| lint.help(help),
842-
);
814+
if let Some(def_id) = self.def_id.as_local() {
815+
self.tcx.struct_span_lint_hir(
816+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
817+
self.tcx.hir().local_def_id_to_hir_id(def_id),
818+
self.sp,
819+
msg,
820+
|lint| lint.help(help),
821+
);
822+
}
843823
}
844824
}
845825

src/librustdoc/passes/calculate_doc_coverage.rs

+1-7
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

+11-8
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

+3-8
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
{

0 commit comments

Comments
 (0)