Skip to content

Commit

Permalink
rustdoc: use LocalDefId for inline stmt
Browse files Browse the repository at this point in the history
It's never a cross-crate DefId, so save space by not storing it.
  • Loading branch information
notriddle committed Aug 30, 2024
1 parent 0d63418 commit 9ad8e26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
22 changes: 11 additions & 11 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, DefIdSet, LocalModDefId};
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
use rustc_hir::Mutability;
use rustc_metadata::creader::{CStore, LoadedMacro};
use rustc_middle::ty::fast_reject::SimplifiedType;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub(crate) fn try_inline(
cx: &mut DocContext<'_>,
res: Res,
name: Symbol,
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
visited: &mut DefIdSet,
) -> Option<Vec<clean::Item>> {
let did = res.opt_def_id()?;
Expand Down Expand Up @@ -156,7 +156,7 @@ pub(crate) fn try_inline(
kind,
did,
name,
import_def_id.and_then(|def_id| def_id.as_local()),
import_def_id,
None,
);
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
Expand Down Expand Up @@ -197,7 +197,7 @@ pub(crate) fn try_inline_glob(
visited,
inlined_names,
Some(&reexports),
Some((attrs, Some(import.owner_id.def_id.to_def_id()))),
Some((attrs, Some(import.owner_id.def_id))),
);
items.retain(|item| {
if let Some(name) = item.name {
Expand Down Expand Up @@ -371,7 +371,7 @@ fn build_type_alias(
pub(crate) fn build_impls(
cx: &mut DocContext<'_>,
did: DefId,
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
ret: &mut Vec<clean::Item>,
) {
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
Expand Down Expand Up @@ -404,7 +404,7 @@ pub(crate) fn build_impls(
pub(crate) fn merge_attrs(
cx: &mut DocContext<'_>,
old_attrs: &[ast::Attribute],
new_attrs: Option<(&[ast::Attribute], Option<DefId>)>,
new_attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
// NOTE: If we have additional attributes (from a re-export),
// always insert them first. This ensure that re-export
Expand All @@ -415,7 +415,7 @@ pub(crate) fn merge_attrs(
both.extend_from_slice(old_attrs);
(
if let Some(item_id) = item_id {
Attributes::from_ast_with_additional(old_attrs, (inner, item_id))
Attributes::from_ast_with_additional(old_attrs, (inner, item_id.to_def_id()))
} else {
Attributes::from_ast(&both)
},
Expand All @@ -430,7 +430,7 @@ pub(crate) fn merge_attrs(
pub(crate) fn build_impl(
cx: &mut DocContext<'_>,
did: DefId,
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
ret: &mut Vec<clean::Item>,
) {
if !cx.inlined.insert(did.into()) {
Expand Down Expand Up @@ -640,7 +640,7 @@ fn build_module_items(
visited: &mut DefIdSet,
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
allowed_def_ids: Option<&DefIdSet>,
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
) -> Vec<clean::Item> {
let mut items = Vec::new();

Expand Down Expand Up @@ -744,15 +744,15 @@ fn build_macro(
cx: &mut DocContext<'_>,
def_id: DefId,
name: Symbol,
import_def_id: Option<DefId>,
import_def_id: Option<LocalDefId>,
macro_kind: MacroKind,
is_doc_hidden: bool,
) -> clean::ItemKind {
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
MacroKind::Bang => {
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
let vis = cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
clean::MacroItem(clean::Macro {
source: utils::display_macro_source(
cx,
Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn generate_item_with_correct_attrs(

let name = renamed.or(Some(name));
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
item.inline_stmt_id = import_id;
item
}

Expand Down Expand Up @@ -2926,7 +2926,7 @@ fn clean_extern_crate<'tcx>(
})
&& !cx.output_format.is_json();

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

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

// The parent of the module in which this import resides. This
// is the same as `current_mod` if that's already the top
Expand Down Expand Up @@ -3070,7 +3070,7 @@ fn clean_use_statement_inner<'tcx>(
)
{
items.push(Item::from_def_id_and_parts(
import_def_id,
import_def_id.to_def_id(),
None,
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
cx,
Expand All @@ -3080,7 +3080,7 @@ fn clean_use_statement_inner<'tcx>(
Import::new_simple(name, resolve_use_source(cx, path), true)
};

vec![Item::from_def_id_and_parts(import_def_id, None, ImportItem(inner), cx)]
vec![Item::from_def_id_and_parts(import_def_id.to_def_id(), None, ImportItem(inner), cx)]
}

fn clean_maybe_renamed_foreign_item<'tcx>(
Expand Down
8 changes: 5 additions & 3 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ pub(crate) struct Item {
/// E.g., struct vs enum vs function.
pub(crate) kind: Box<ItemKind>,
pub(crate) item_id: ItemId,
/// This is the `DefId` of the `use` statement if the item was inlined.
pub(crate) inline_stmt_id: Option<DefId>,
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
/// The crate metadata doesn't hold this information, so the `use` statement
/// always belongs to the current crate.
pub(crate) inline_stmt_id: Option<LocalDefId>,
pub(crate) cfg: Option<Arc<Cfg>>,
}

Expand Down Expand Up @@ -707,7 +709,7 @@ impl Item {
_ => {}
}
let def_id = match self.inline_stmt_id {
Some(inlined) => inlined,
Some(inlined) => inlined.to_def_id(),
None => def_id,
};
Some(tcx.visibility(def_id))
Expand Down

0 comments on commit 9ad8e26

Please sign in to comment.