Skip to content

Commit

Permalink
Auto merge of rust-lang#129789 - notriddle:notriddle/inline-stmt-loca…
Browse files Browse the repository at this point in the history
…l, r=GuillaumeGomez

rustdoc: use strategic boxing to shrink `clean::Item`

* `inline_stmt_id` is never a cross-crate DefId, so save space by not storing it.
* Instead of two inner boxes for `Item`, use one.
  • Loading branch information
bors committed Sep 8, 2024
2 parents adf8d16 + 6590336 commit b36b9af
Show file tree
Hide file tree
Showing 28 changed files with 215 additions and 197 deletions.
22 changes: 12 additions & 10 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ fn synthesize_auto_trait_impl<'tcx>(

Some(clean::Item {
name: None,
attrs: Default::default(),
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
items: Vec::new(),
polarity,
kind: clean::ImplKind::Auto,
})),
}),
item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
items: Vec::new(),
polarity,
kind: clean::ImplKind::Auto,
}))),
cfg: None,
inline_stmt_id: None,
})
Expand Down
72 changes: 37 additions & 35 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,44 @@ pub(crate) fn synthesize_blanket_impls(

blanket_impls.push(clean::Item {
name: None,
attrs: Default::default(),
item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics: clean_ty_generics(
cx,
tcx.generics_of(impl_def_id),
tcx.explicit_predicates_of(impl_def_id),
),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(clean_trait_ref_with_constraints(
cx,
ty::Binder::dummy(trait_ref.instantiate_identity()),
ThinVec::new(),
)),
for_: clean_middle_ty(
ty::Binder::dummy(ty.instantiate_identity()),
cx,
None,
None,
),
items: tcx
.associated_items(impl_def_id)
.in_definition_order()
.filter(|item| !item.is_impl_trait_in_trait())
.map(|item| clean_middle_assoc_item(item, cx))
.collect(),
polarity: ty::ImplPolarity::Positive,
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
cx,
None,
None,
))),
}))),
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics: clean_ty_generics(
cx,
tcx.generics_of(impl_def_id),
tcx.explicit_predicates_of(impl_def_id),
),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(clean_trait_ref_with_constraints(
cx,
ty::Binder::dummy(trait_ref.instantiate_identity()),
ThinVec::new(),
)),
for_: clean_middle_ty(
ty::Binder::dummy(ty.instantiate_identity()),
cx,
None,
None,
),
items: tcx
.associated_items(impl_def_id)
.in_definition_order()
.filter(|item| !item.is_impl_trait_in_trait())
.map(|item| clean_middle_assoc_item(item, cx))
.collect(),
polarity: ty::ImplPolarity::Positive,
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
cx,
None,
None,
))),
})),
}),
cfg: None,
inline_stmt_id: None,
});
Expand Down
69 changes: 33 additions & 36 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 @@ -43,7 +43,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 @@ -152,14 +152,8 @@ pub(crate) fn try_inline(
};

cx.inlined.insert(did.into());
let mut item = crate::clean::generate_item_with_correct_attrs(
cx,
kind,
did,
name,
import_def_id.and_then(|def_id| def_id.as_local()),
None,
);
let mut item =
crate::clean::generate_item_with_correct_attrs(cx, kind, did, name, import_def_id, None);
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
item.inline_stmt_id = import_def_id;
ret.push(item);
Expand Down Expand Up @@ -198,7 +192,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 @@ -372,7 +366,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 @@ -405,7 +399,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 @@ -416,7 +410,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 @@ -431,7 +425,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 @@ -623,7 +617,7 @@ pub(crate) fn build_impl(
ImplKind::Normal
},
})),
Box::new(merged_attrs),
merged_attrs,
cfg,
));
}
Expand All @@ -641,7 +635,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 @@ -673,27 +667,29 @@ fn build_module_items(
let prim_ty = clean::PrimitiveType::from(p);
items.push(clean::Item {
name: None,
attrs: Box::default(),
// We can use the item's `DefId` directly since the only information ever used
// from it is `DefId.krate`.
item_id: ItemId::DefId(did),
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
res,
segments: thin_vec![clean::PathSegment {
name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Default::default(),
constraints: ThinVec::new(),
},
}],
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
res,
segments: thin_vec![clean::PathSegment {
name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Default::default(),
constraints: ThinVec::new(),
},
}],
},
did: None,
},
did: None,
},
true,
))),
true,
)),
}),
cfg: None,
inline_stmt_id: None,
});
Expand Down Expand Up @@ -745,15 +741,16 @@ 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
14 changes: 7 additions & 7 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ fn generate_item_with_correct_attrs(
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);

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());
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
item.inline_stmt_id = import_id;
item
}

Expand Down Expand Up @@ -2927,7 +2927,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 @@ -2941,7 +2941,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 @@ -2988,7 +2988,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 @@ -3071,7 +3071,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 @@ -3081,7 +3081,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
Loading

0 comments on commit b36b9af

Please sign in to comment.