Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9ad8e26

Browse files
committed
rustdoc: use LocalDefId for inline stmt
It's never a cross-crate DefId, so save space by not storing it.
1 parent 0d63418 commit 9ad8e26

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::def_id::{DefId, DefIdSet, LocalModDefId};
8+
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
99
use rustc_hir::Mutability;
1010
use rustc_metadata::creader::{CStore, LoadedMacro};
1111
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -42,7 +42,7 @@ pub(crate) fn try_inline(
4242
cx: &mut DocContext<'_>,
4343
res: Res,
4444
name: Symbol,
45-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
45+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
4646
visited: &mut DefIdSet,
4747
) -> Option<Vec<clean::Item>> {
4848
let did = res.opt_def_id()?;
@@ -156,7 +156,7 @@ pub(crate) fn try_inline(
156156
kind,
157157
did,
158158
name,
159-
import_def_id.and_then(|def_id| def_id.as_local()),
159+
import_def_id,
160160
None,
161161
);
162162
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
@@ -197,7 +197,7 @@ pub(crate) fn try_inline_glob(
197197
visited,
198198
inlined_names,
199199
Some(&reexports),
200-
Some((attrs, Some(import.owner_id.def_id.to_def_id()))),
200+
Some((attrs, Some(import.owner_id.def_id))),
201201
);
202202
items.retain(|item| {
203203
if let Some(name) = item.name {
@@ -371,7 +371,7 @@ fn build_type_alias(
371371
pub(crate) fn build_impls(
372372
cx: &mut DocContext<'_>,
373373
did: DefId,
374-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
374+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
375375
ret: &mut Vec<clean::Item>,
376376
) {
377377
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
@@ -404,7 +404,7 @@ pub(crate) fn build_impls(
404404
pub(crate) fn merge_attrs(
405405
cx: &mut DocContext<'_>,
406406
old_attrs: &[ast::Attribute],
407-
new_attrs: Option<(&[ast::Attribute], Option<DefId>)>,
407+
new_attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
408408
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
409409
// NOTE: If we have additional attributes (from a re-export),
410410
// always insert them first. This ensure that re-export
@@ -415,7 +415,7 @@ pub(crate) fn merge_attrs(
415415
both.extend_from_slice(old_attrs);
416416
(
417417
if let Some(item_id) = item_id {
418-
Attributes::from_ast_with_additional(old_attrs, (inner, item_id))
418+
Attributes::from_ast_with_additional(old_attrs, (inner, item_id.to_def_id()))
419419
} else {
420420
Attributes::from_ast(&both)
421421
},
@@ -430,7 +430,7 @@ pub(crate) fn merge_attrs(
430430
pub(crate) fn build_impl(
431431
cx: &mut DocContext<'_>,
432432
did: DefId,
433-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
433+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
434434
ret: &mut Vec<clean::Item>,
435435
) {
436436
if !cx.inlined.insert(did.into()) {
@@ -640,7 +640,7 @@ fn build_module_items(
640640
visited: &mut DefIdSet,
641641
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
642642
allowed_def_ids: Option<&DefIdSet>,
643-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
643+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
644644
) -> Vec<clean::Item> {
645645
let mut items = Vec::new();
646646

@@ -744,15 +744,15 @@ fn build_macro(
744744
cx: &mut DocContext<'_>,
745745
def_id: DefId,
746746
name: Symbol,
747-
import_def_id: Option<DefId>,
747+
import_def_id: Option<LocalDefId>,
748748
macro_kind: MacroKind,
749749
is_doc_hidden: bool,
750750
) -> clean::ItemKind {
751751
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
752752
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
753753
MacroKind::Bang => {
754754
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
755-
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
755+
let vis = cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
756756
clean::MacroItem(clean::Macro {
757757
source: utils::display_macro_source(
758758
cx,

src/librustdoc/clean/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn generate_item_with_correct_attrs(
203203

204204
let name = renamed.or(Some(name));
205205
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
206-
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
206+
item.inline_stmt_id = import_id;
207207
item
208208
}
209209

@@ -2926,7 +2926,7 @@ fn clean_extern_crate<'tcx>(
29262926
})
29272927
&& !cx.output_format.is_json();
29282928

2929-
let krate_owner_def_id = krate.owner_id.to_def_id();
2929+
let krate_owner_def_id = krate.owner_id.def_id;
29302930
if please_inline {
29312931
if let Some(items) = inline::try_inline(
29322932
cx,
@@ -2940,7 +2940,7 @@ fn clean_extern_crate<'tcx>(
29402940
}
29412941

29422942
vec![Item::from_def_id_and_parts(
2943-
krate_owner_def_id,
2943+
krate_owner_def_id.to_def_id(),
29442944
Some(name),
29452945
ExternCrateItem { src: orig_name },
29462946
cx,
@@ -2987,7 +2987,7 @@ fn clean_use_statement_inner<'tcx>(
29872987
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
29882988
let pub_underscore = visibility.is_public() && name == kw::Underscore;
29892989
let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
2990-
let import_def_id = import.owner_id.def_id.to_def_id();
2990+
let import_def_id = import.owner_id.def_id;
29912991

29922992
// The parent of the module in which this import resides. This
29932993
// is the same as `current_mod` if that's already the top
@@ -3070,7 +3070,7 @@ fn clean_use_statement_inner<'tcx>(
30703070
)
30713071
{
30723072
items.push(Item::from_def_id_and_parts(
3073-
import_def_id,
3073+
import_def_id.to_def_id(),
30743074
None,
30753075
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
30763076
cx,
@@ -3080,7 +3080,7 @@ fn clean_use_statement_inner<'tcx>(
30803080
Import::new_simple(name, resolve_use_source(cx, path), true)
30813081
};
30823082

3083-
vec![Item::from_def_id_and_parts(import_def_id, None, ImportItem(inner), cx)]
3083+
vec![Item::from_def_id_and_parts(import_def_id.to_def_id(), None, ImportItem(inner), cx)]
30843084
}
30853085

30863086
fn clean_maybe_renamed_foreign_item<'tcx>(

src/librustdoc/clean/types.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ pub(crate) struct Item {
324324
/// E.g., struct vs enum vs function.
325325
pub(crate) kind: Box<ItemKind>,
326326
pub(crate) item_id: ItemId,
327-
/// This is the `DefId` of the `use` statement if the item was inlined.
328-
pub(crate) inline_stmt_id: Option<DefId>,
327+
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
328+
/// The crate metadata doesn't hold this information, so the `use` statement
329+
/// always belongs to the current crate.
330+
pub(crate) inline_stmt_id: Option<LocalDefId>,
329331
pub(crate) cfg: Option<Arc<Cfg>>,
330332
}
331333

@@ -707,7 +709,7 @@ impl Item {
707709
_ => {}
708710
}
709711
let def_id = match self.inline_stmt_id {
710-
Some(inlined) => inlined,
712+
Some(inlined) => inlined.to_def_id(),
711713
None => def_id,
712714
};
713715
Some(tcx.visibility(def_id))

0 commit comments

Comments
 (0)