Skip to content

Commit

Permalink
Revert "rustdoc: Store DefId's in ItemId on heap for decreasing Item'…
Browse files Browse the repository at this point in the history
…s size"

This reverts commit 41a345d4c46dad1a98c9993bc78513415994e8ba.
  • Loading branch information
Stupremee committed Jul 5, 2021
1 parent 45d3dae commit 97c82d8
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/auto_trait.rs
Expand Up @@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
name: None,
attrs: Default::default(),
visibility: Inherited,
def_id: ItemId::Auto(box ImplId { trait_: trait_def_id, for_: item_def_id }),
def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
kind: box ImplItem(Impl {
span: Span::dummy(),
unsafety: hir::Unsafety::Normal,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Expand Up @@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
name: None,
attrs: Default::default(),
visibility: Inherited,
def_id: ItemId::Blanket(box ImplId { trait_: trait_def_id, for_: item_def_id }),
def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id },
kind: box ImplItem(Impl {
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
unsafety: hir::Unsafety::Normal,
Expand Down
46 changes: 24 additions & 22 deletions src/librustdoc/clean/types.rs
Expand Up @@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{BodyId, Mutability};
use rustc_index::vec::IndexVec;
Expand Down Expand Up @@ -50,59 +50,61 @@ use self::Type::*;

crate type ItemIdSet = FxHashSet<ItemId>;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
crate struct ImplId {
crate trait_: DefId,
crate for_: DefId,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
crate enum ItemId {
/// A "normal" item that uses a [`DefId`] for identification.
DefId(DefId),
/// Identifier that is used for auto traits.
Auto(Box<ImplId>),
Auto { trait_: DefId, for_: DefId },
/// Identifier that is used for blanket implementations.
Blanket(Box<ImplId>),
Blanket { trait_: DefId, for_: DefId },
/// Identifier for primitive types.
Primitive(CrateNum),
}

impl ItemId {
#[inline]
crate fn is_local(&self) -> bool {
crate fn is_local(self) -> bool {
match self {
ItemId::Auto(box ImplId { for_: id, .. })
| ItemId::Blanket(box ImplId { for_: id, .. })
ItemId::Auto { for_: id, .. }
| ItemId::Blanket { for_: id, .. }
| ItemId::DefId(id) => id.is_local(),
ItemId::Primitive(krate) => *krate == LOCAL_CRATE,
ItemId::Primitive(krate) => krate == LOCAL_CRATE,
}
}

#[inline]
#[track_caller]
crate fn expect_def_id(&self) -> DefId {
crate fn expect_def_id(self) -> DefId {
self.as_def_id()
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self))
}

#[inline]
crate fn as_def_id(&self) -> Option<DefId> {
crate fn as_def_id(self) -> Option<DefId> {
match self {
ItemId::DefId(id) => Some(*id),
ItemId::DefId(id) => Some(id),
_ => None,
}
}

#[inline]
crate fn krate(&self) -> CrateNum {
match *self {
ItemId::Auto(box ImplId { for_: id, .. })
| ItemId::Blanket(box ImplId { for_: id, .. })
crate fn krate(self) -> CrateNum {
match self {
ItemId::Auto { for_: id, .. }
| ItemId::Blanket { for_: id, .. }
| ItemId::DefId(id) => id.krate,
ItemId::Primitive(krate) => krate,
}
}

#[inline]
crate fn index(self) -> Option<DefIndex> {
match self {
ItemId::DefId(id) => Some(id.index),
_ => None,
}
}
}

impl From<DefId> for ItemId {
Expand Down Expand Up @@ -377,7 +379,7 @@ impl Item {
{
*span
} else {
self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy)
self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy())
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/core.rs
Expand Up @@ -128,8 +128,8 @@ impl<'tcx> DocContext<'tcx> {

/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
/// (This avoids a slice-index-out-of-bounds panic.)
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: &ItemId) -> Option<HirId> {
match *def_id {
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option<HirId> {
match def_id {
ItemId::DefId(real_id) => {
real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}
Expand Down Expand Up @@ -432,7 +432,7 @@ crate fn run_global_ctxt(
);
tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS,
DocContext::as_local_hir_id(tcx, &krate.module.def_id).unwrap(),
DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(),
|lint| {
let mut diag =
lint.build("no documentation found for this crate's top-level module");
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/cache.rs
Expand Up @@ -290,7 +290,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// A crate has a module at its root, containing all items,
// which should not be indexed. The crate-item itself is
// inserted later on when serializing the search-index.
if item.def_id.as_def_id().map_or(false, |did| did.index != CRATE_DEF_INDEX) {
if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) {
let desc = item.doc_value().map_or_else(String::new, |x| {
short_markdown_summary(&x.as_str(), &item.link_names(&self.cache))
});
Expand Down
21 changes: 7 additions & 14 deletions src/librustdoc/html/render/mod.rs
Expand Up @@ -753,7 +753,7 @@ fn assoc_const(
w,
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
extra,
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
naive_assoc_href(it, link, cx),
it.name.as_ref().unwrap(),
ty.print(cx)
Expand Down Expand Up @@ -872,7 +872,7 @@ fn render_assoc_item(
.unwrap_or_else(|| format!("#{}.{}", ty, name))
}
};
let vis = meth.visibility.print_with_space(meth.def_id.clone(), cx).to_string();
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
let constness =
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
let asyncness = header.asyncness.print_with_space();
Expand Down Expand Up @@ -984,7 +984,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) {
}
}

#[derive(Clone)]
#[derive(Copy, Clone)]
enum AssocItemLink<'a> {
Anchor(Option<&'a str>),
GotoSource(ItemId, &'a FxHashSet<Symbol>),
Expand All @@ -994,7 +994,7 @@ impl<'a> AssocItemLink<'a> {
fn anchor(&self, id: &'a str) -> Self {
match *self {
AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)),
ref other => other.clone(),
ref other => *other,
}
}
}
Expand Down Expand Up @@ -1306,14 +1306,7 @@ fn render_impl(
} else {
// In case the item isn't documented,
// provide short documentation from the trait.
document_short(
&mut doc_buffer,
it,
cx,
link.clone(),
parent,
show_def_docs,
);
document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs);
}
}
} else {
Expand All @@ -1324,7 +1317,7 @@ fn render_impl(
}
}
} else {
document_short(&mut doc_buffer, item, cx, link.clone(), parent, show_def_docs);
document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs);
}
}
let w = if short_documented && trait_.is_some() { interesting } else { boring };
Expand Down Expand Up @@ -1452,7 +1445,7 @@ fn render_impl(
trait_item,
if trait_.is_some() { &i.impl_item } else { parent },
parent,
link.clone(),
link,
render_mode,
false,
trait_.map(|t| &t.trait_),
Expand Down
36 changes: 17 additions & 19 deletions src/librustdoc/html/render/print_item.rs
Expand Up @@ -245,7 +245,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
// (which is the position in the vector).
indices.dedup_by_key(|i| {
(
items[*i].def_id.clone(),
items[*i].def_id,
if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None },
items[*i].type_(),
if items[*i].is_import() { *i } else { 0 },
Expand Down Expand Up @@ -288,14 +288,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
Some(ref src) => write!(
w,
"<div class=\"item-left\"><code>{}extern crate {} as {};",
myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
myitem.visibility.print_with_space(myitem.def_id, cx),
anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx),
myitem.name.as_ref().unwrap(),
),
None => write!(
w,
"<div class=\"item-left\"><code>{}extern crate {};",
myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
myitem.visibility.print_with_space(myitem.def_id, cx),
anchor(
myitem.def_id.expect_def_id(),
&*myitem.name.as_ref().unwrap().as_str(),
Expand Down Expand Up @@ -336,7 +336,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
<div class=\"item-right docblock-short\">{stab_tags}</div>",
stab = stab.unwrap_or_default(),
add = add,
vis = myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
vis = myitem.visibility.print_with_space(myitem.def_id, cx),
imp = import.print(cx),
stab_tags = stab_tags.unwrap_or_default(),
);
Expand Down Expand Up @@ -437,7 +437,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
}

fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
let vis = it.visibility.print_with_space(it.def_id.clone(), cx).to_string();
let vis = it.visibility.print_with_space(it.def_id, cx).to_string();
let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx()));
let asyncness = f.header.asyncness.print_with_space();
let unsafety = f.header.unsafety.print_with_space();
Expand Down Expand Up @@ -489,7 +489,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
write!(
w,
"{}{}{}trait {}{}{}",
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
t.unsafety.print_with_space(),
if t.is_auto { "auto " } else { "" },
it.name.as_ref().unwrap(),
Expand Down Expand Up @@ -710,10 +710,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra

for implementor in foreign {
let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx());
let assoc_link = AssocItemLink::GotoSource(
implementor.impl_item.def_id.clone(),
&provided_methods,
);
let assoc_link =
AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods);
render_impl(
w,
cx,
Expand Down Expand Up @@ -917,7 +915,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
write!(
w,
"{}enum {}{}{}",
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
it.name.as_ref().unwrap(),
e.generics.print(cx),
print_where_clause(&e.generics, cx, 0, true),
Expand Down Expand Up @@ -1105,7 +1103,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
write!(
w,
"{vis}const {name}: {typ}",
vis = it.visibility.print_with_space(it.def_id.clone(), cx),
vis = it.visibility.print_with_space(it.def_id, cx),
name = it.name.as_ref().unwrap(),
typ = c.type_.print(cx),
);
Expand Down Expand Up @@ -1195,7 +1193,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
write!(
w,
"{vis}static {mutability}{name}: {typ}</pre>",
vis = it.visibility.print_with_space(it.def_id.clone(), cx),
vis = it.visibility.print_with_space(it.def_id, cx),
mutability = s.mutability.print_with_space(),
name = it.name.as_ref().unwrap(),
typ = s.type_.print(cx)
Expand All @@ -1209,7 +1207,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
write!(
w,
" {}type {};\n}}</pre>",
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
it.name.as_ref().unwrap(),
);

Expand Down Expand Up @@ -1364,7 +1362,7 @@ fn render_union(
write!(
w,
"{}{}{}",
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
if structhead { "union " } else { "" },
it.name.as_ref().unwrap()
);
Expand All @@ -1386,7 +1384,7 @@ fn render_union(
write!(
w,
" {}{}: {},\n{}",
field.visibility.print_with_space(field.def_id.clone(), cx),
field.visibility.print_with_space(field.def_id, cx),
field.name.as_ref().unwrap(),
ty.print(cx),
tab
Expand Down Expand Up @@ -1416,7 +1414,7 @@ fn render_struct(
write!(
w,
"{}{}{}",
it.visibility.print_with_space(it.def_id.clone(), cx),
it.visibility.print_with_space(it.def_id, cx),
if structhead { "struct " } else { "" },
it.name.as_ref().unwrap()
);
Expand All @@ -1442,7 +1440,7 @@ fn render_struct(
w,
"\n{} {}{}: {},",
tab,
field.visibility.print_with_space(field.def_id.clone(), cx),
field.visibility.print_with_space(field.def_id, cx),
field.name.as_ref().unwrap(),
ty.print(cx),
);
Expand Down Expand Up @@ -1476,7 +1474,7 @@ fn render_struct(
write!(
w,
"{}{}",
field.visibility.print_with_space(field.def_id.clone(), cx),
field.visibility.print_with_space(field.def_id, cx),
ty.print(cx),
)
}
Expand Down

0 comments on commit 97c82d8

Please sign in to comment.