1
Fork 0

rustdoc: Store DefId's in ItemId on heap for decreasing Item's size

This commit is contained in:
Justus K 2021-06-26 20:47:33 +02:00
parent acd4dc2d0c
commit 45d3daece3
No known key found for this signature in database
GPG key ID: 8C62FE98A62FC462
16 changed files with 115 additions and 109 deletions

View file

@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
name: None, name: None,
attrs: Default::default(), attrs: Default::default(),
visibility: Inherited, visibility: Inherited,
def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id }, def_id: ItemId::Auto(box ImplId { trait_: trait_def_id, for_: item_def_id }),
kind: box ImplItem(Impl { kind: box ImplItem(Impl {
span: Span::dummy(), span: Span::dummy(),
unsafety: hir::Unsafety::Normal, unsafety: hir::Unsafety::Normal,

View file

@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
name: None, name: None,
attrs: Default::default(), attrs: Default::default(),
visibility: Inherited, visibility: Inherited,
def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id }, def_id: ItemId::Blanket(box ImplId { trait_: trait_def_id, for_: item_def_id }),
kind: box ImplItem(Impl { kind: box ImplItem(Impl {
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx), span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
unsafety: hir::Unsafety::Normal, unsafety: hir::Unsafety::Normal,

View file

@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX}; use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::{BodyId, Mutability}; use rustc_hir::{BodyId, Mutability};
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
@ -50,61 +50,59 @@ use self::Type::*;
crate type ItemIdSet = FxHashSet<ItemId>; crate type ItemIdSet = FxHashSet<ItemId>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] #[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)]
crate enum ItemId { crate enum ItemId {
/// A "normal" item that uses a [`DefId`] for identification. /// A "normal" item that uses a [`DefId`] for identification.
DefId(DefId), DefId(DefId),
/// Identifier that is used for auto traits. /// Identifier that is used for auto traits.
Auto { trait_: DefId, for_: DefId }, Auto(Box<ImplId>),
/// Identifier that is used for blanket implementations. /// Identifier that is used for blanket implementations.
Blanket { trait_: DefId, for_: DefId }, Blanket(Box<ImplId>),
/// Identifier for primitive types. /// Identifier for primitive types.
Primitive(CrateNum), Primitive(CrateNum),
} }
impl ItemId { impl ItemId {
#[inline] #[inline]
crate fn is_local(self) -> bool { crate fn is_local(&self) -> bool {
match self { match self {
ItemId::Auto { for_: id, .. } ItemId::Auto(box ImplId { for_: id, .. })
| ItemId::Blanket { for_: id, .. } | ItemId::Blanket(box ImplId { for_: id, .. })
| ItemId::DefId(id) => id.is_local(), | ItemId::DefId(id) => id.is_local(),
ItemId::Primitive(krate) => krate == LOCAL_CRATE, ItemId::Primitive(krate) => *krate == LOCAL_CRATE,
} }
} }
#[inline] #[inline]
#[track_caller] #[track_caller]
crate fn expect_def_id(self) -> DefId { crate fn expect_def_id(&self) -> DefId {
self.as_def_id() self.as_def_id()
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self)) .unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self))
} }
#[inline] #[inline]
crate fn as_def_id(self) -> Option<DefId> { crate fn as_def_id(&self) -> Option<DefId> {
match self { match self {
ItemId::DefId(id) => Some(id), ItemId::DefId(id) => Some(*id),
_ => None, _ => None,
} }
} }
#[inline] #[inline]
crate fn krate(self) -> CrateNum { crate fn krate(&self) -> CrateNum {
match self { match *self {
ItemId::Auto { for_: id, .. } ItemId::Auto(box ImplId { for_: id, .. })
| ItemId::Blanket { for_: id, .. } | ItemId::Blanket(box ImplId { for_: id, .. })
| ItemId::DefId(id) => id.krate, | ItemId::DefId(id) => id.krate,
ItemId::Primitive(krate) => 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 { impl From<DefId> for ItemId {
@ -379,7 +377,7 @@ impl Item {
{ {
*span *span
} else { } 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)
} }
} }

View file

@ -128,8 +128,8 @@ impl<'tcx> DocContext<'tcx> {
/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
/// (This avoids a slice-index-out-of-bounds panic.) /// (This avoids a slice-index-out-of-bounds panic.)
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option<HirId> { crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: &ItemId) -> Option<HirId> {
match def_id { match *def_id {
ItemId::DefId(real_id) => { ItemId::DefId(real_id) => {
real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
} }
@ -432,7 +432,7 @@ crate fn run_global_ctxt(
); );
tcx.struct_lint_node( tcx.struct_lint_node(
crate::lint::MISSING_CRATE_LEVEL_DOCS, 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| { |lint| {
let mut diag = let mut diag =
lint.build("no documentation found for this crate's top-level module"); lint.build("no documentation found for this crate's top-level module");

View file

@ -290,7 +290,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// A crate has a module at its root, containing all items, // A crate has a module at its root, containing all items,
// which should not be indexed. The crate-item itself is // which should not be indexed. The crate-item itself is
// inserted later on when serializing the search-index. // inserted later on when serializing the search-index.
if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) { if item.def_id.as_def_id().map_or(false, |did| did.index != CRATE_DEF_INDEX) {
let desc = item.doc_value().map_or_else(String::new, |x| { let desc = item.doc_value().map_or_else(String::new, |x| {
short_markdown_summary(&x.as_str(), &item.link_names(&self.cache)) short_markdown_summary(&x.as_str(), &item.link_names(&self.cache))
}); });

View file

@ -753,7 +753,7 @@ fn assoc_const(
w, w,
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}", "{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
extra, extra,
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
naive_assoc_href(it, link, cx), naive_assoc_href(it, link, cx),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
ty.print(cx) ty.print(cx)
@ -872,7 +872,7 @@ fn render_assoc_item(
.unwrap_or_else(|| format!("#{}.{}", ty, name)) .unwrap_or_else(|| format!("#{}.{}", ty, name))
} }
}; };
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string(); let vis = meth.visibility.print_with_space(meth.def_id.clone(), cx).to_string();
let constness = let constness =
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx())); print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
let asyncness = header.asyncness.print_with_space(); let asyncness = header.asyncness.print_with_space();
@ -984,7 +984,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) {
} }
} }
#[derive(Copy, Clone)] #[derive(Clone)]
enum AssocItemLink<'a> { enum AssocItemLink<'a> {
Anchor(Option<&'a str>), Anchor(Option<&'a str>),
GotoSource(ItemId, &'a FxHashSet<Symbol>), GotoSource(ItemId, &'a FxHashSet<Symbol>),
@ -994,7 +994,7 @@ impl<'a> AssocItemLink<'a> {
fn anchor(&self, id: &'a str) -> Self { fn anchor(&self, id: &'a str) -> Self {
match *self { match *self {
AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)), AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)),
ref other => *other, ref other => other.clone(),
} }
} }
} }
@ -1306,7 +1306,14 @@ fn render_impl(
} else { } else {
// In case the item isn't documented, // In case the item isn't documented,
// provide short documentation from the trait. // provide short documentation from the trait.
document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs); document_short(
&mut doc_buffer,
it,
cx,
link.clone(),
parent,
show_def_docs,
);
} }
} }
} else { } else {
@ -1317,7 +1324,7 @@ fn render_impl(
} }
} }
} else { } else {
document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs); document_short(&mut doc_buffer, item, cx, link.clone(), parent, show_def_docs);
} }
} }
let w = if short_documented && trait_.is_some() { interesting } else { boring }; let w = if short_documented && trait_.is_some() { interesting } else { boring };
@ -1445,7 +1452,7 @@ fn render_impl(
trait_item, trait_item,
if trait_.is_some() { &i.impl_item } else { parent }, if trait_.is_some() { &i.impl_item } else { parent },
parent, parent,
link, link.clone(),
render_mode, render_mode,
false, false,
trait_.map(|t| &t.trait_), trait_.map(|t| &t.trait_),

View file

@ -245,7 +245,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
// (which is the position in the vector). // (which is the position in the vector).
indices.dedup_by_key(|i| { indices.dedup_by_key(|i| {
( (
items[*i].def_id, items[*i].def_id.clone(),
if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None }, if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None },
items[*i].type_(), items[*i].type_(),
if items[*i].is_import() { *i } else { 0 }, if items[*i].is_import() { *i } else { 0 },
@ -288,14 +288,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
Some(ref src) => write!( Some(ref src) => write!(
w, w,
"<div class=\"item-left\"><code>{}extern crate {} as {};", "<div class=\"item-left\"><code>{}extern crate {} as {};",
myitem.visibility.print_with_space(myitem.def_id, cx), myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx), anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx),
myitem.name.as_ref().unwrap(), myitem.name.as_ref().unwrap(),
), ),
None => write!( None => write!(
w, w,
"<div class=\"item-left\"><code>{}extern crate {};", "<div class=\"item-left\"><code>{}extern crate {};",
myitem.visibility.print_with_space(myitem.def_id, cx), myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
anchor( anchor(
myitem.def_id.expect_def_id(), myitem.def_id.expect_def_id(),
&*myitem.name.as_ref().unwrap().as_str(), &*myitem.name.as_ref().unwrap().as_str(),
@ -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>", <div class=\"item-right docblock-short\">{stab_tags}</div>",
stab = stab.unwrap_or_default(), stab = stab.unwrap_or_default(),
add = add, add = add,
vis = myitem.visibility.print_with_space(myitem.def_id, cx), vis = myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
imp = import.print(cx), imp = import.print(cx),
stab_tags = stab_tags.unwrap_or_default(), stab_tags = stab_tags.unwrap_or_default(),
); );
@ -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) { fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
let vis = it.visibility.print_with_space(it.def_id, cx).to_string(); let vis = it.visibility.print_with_space(it.def_id.clone(), cx).to_string();
let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx())); let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx()));
let asyncness = f.header.asyncness.print_with_space(); let asyncness = f.header.asyncness.print_with_space();
let unsafety = f.header.unsafety.print_with_space(); let unsafety = f.header.unsafety.print_with_space();
@ -489,7 +489,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
write!( write!(
w, w,
"{}{}{}trait {}{}{}", "{}{}{}trait {}{}{}",
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
t.unsafety.print_with_space(), t.unsafety.print_with_space(),
if t.is_auto { "auto " } else { "" }, if t.is_auto { "auto " } else { "" },
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
@ -710,8 +710,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
for implementor in foreign { for implementor in foreign {
let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx()); let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx());
let assoc_link = let assoc_link = AssocItemLink::GotoSource(
AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods); implementor.impl_item.def_id.clone(),
&provided_methods,
);
render_impl( render_impl(
w, w,
cx, cx,
@ -915,7 +917,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
write!( write!(
w, w,
"{}enum {}{}{}", "{}enum {}{}{}",
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
e.generics.print(cx), e.generics.print(cx),
print_where_clause(&e.generics, cx, 0, true), print_where_clause(&e.generics, cx, 0, true),
@ -1103,7 +1105,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
write!( write!(
w, w,
"{vis}const {name}: {typ}", "{vis}const {name}: {typ}",
vis = it.visibility.print_with_space(it.def_id, cx), vis = it.visibility.print_with_space(it.def_id.clone(), cx),
name = it.name.as_ref().unwrap(), name = it.name.as_ref().unwrap(),
typ = c.type_.print(cx), typ = c.type_.print(cx),
); );
@ -1193,7 +1195,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
write!( write!(
w, w,
"{vis}static {mutability}{name}: {typ}</pre>", "{vis}static {mutability}{name}: {typ}</pre>",
vis = it.visibility.print_with_space(it.def_id, cx), vis = it.visibility.print_with_space(it.def_id.clone(), cx),
mutability = s.mutability.print_with_space(), mutability = s.mutability.print_with_space(),
name = it.name.as_ref().unwrap(), name = it.name.as_ref().unwrap(),
typ = s.type_.print(cx) typ = s.type_.print(cx)
@ -1207,7 +1209,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
write!( write!(
w, w,
" {}type {};\n}}</pre>", " {}type {};\n}}</pre>",
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
it.name.as_ref().unwrap(), it.name.as_ref().unwrap(),
); );
@ -1362,7 +1364,7 @@ fn render_union(
write!( write!(
w, w,
"{}{}{}", "{}{}{}",
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
if structhead { "union " } else { "" }, if structhead { "union " } else { "" },
it.name.as_ref().unwrap() it.name.as_ref().unwrap()
); );
@ -1384,7 +1386,7 @@ fn render_union(
write!( write!(
w, w,
" {}{}: {},\n{}", " {}{}: {},\n{}",
field.visibility.print_with_space(field.def_id, cx), field.visibility.print_with_space(field.def_id.clone(), cx),
field.name.as_ref().unwrap(), field.name.as_ref().unwrap(),
ty.print(cx), ty.print(cx),
tab tab
@ -1414,7 +1416,7 @@ fn render_struct(
write!( write!(
w, w,
"{}{}{}", "{}{}{}",
it.visibility.print_with_space(it.def_id, cx), it.visibility.print_with_space(it.def_id.clone(), cx),
if structhead { "struct " } else { "" }, if structhead { "struct " } else { "" },
it.name.as_ref().unwrap() it.name.as_ref().unwrap()
); );
@ -1440,7 +1442,7 @@ fn render_struct(
w, w,
"\n{} {}{}: {},", "\n{} {}{}: {},",
tab, tab,
field.visibility.print_with_space(field.def_id, cx), field.visibility.print_with_space(field.def_id.clone(), cx),
field.name.as_ref().unwrap(), field.name.as_ref().unwrap(),
ty.print(cx), ty.print(cx),
); );
@ -1474,7 +1476,7 @@ fn render_struct(
write!( write!(
w, w,
"{}{}", "{}{}",
field.visibility.print_with_space(field.def_id, cx), field.visibility.print_with_space(field.def_id.clone(), cx),
ty.print(cx), ty.print(cx),
) )
} }

View file

@ -30,7 +30,7 @@ impl JsonRenderer<'_> {
.into_iter() .into_iter()
.flatten() .flatten()
.filter_map(|clean::ItemLink { link, did, .. }| { .filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link.clone(), from_item_id(did.into()))) did.map(|did| (link.clone(), from_item_id(&did.into())))
}) })
.collect(); .collect();
let docs = item.attrs.collapsed_doc_value(); let docs = item.attrs.collapsed_doc_value();
@ -41,14 +41,15 @@ impl JsonRenderer<'_> {
.map(rustc_ast_pretty::pprust::attribute_to_string) .map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(); .collect();
let span = item.span(self.tcx); let span = item.span(self.tcx);
let clean::Item { name, attrs: _, kind: _, visibility, def_id, cfg: _ } = item; let clean::Item { name, attrs: _, kind: _, visibility, ref def_id, cfg: _ } = item;
let def_id = def_id.clone();
let inner = match *item.kind { let inner = match *item.kind {
clean::StrippedItem(_) => return None, clean::StrippedItem(_) => return None,
_ => from_clean_item(item, self.tcx), _ => from_clean_item(item, self.tcx),
}; };
Some(Item { Some(Item {
id: from_item_id(def_id),
crate_id: def_id.krate().as_u32(), crate_id: def_id.krate().as_u32(),
id: from_item_id(&def_id),
name: name.map(|sym| sym.to_string()), name: name.map(|sym| sym.to_string()),
span: self.convert_span(span), span: self.convert_span(span),
visibility: self.convert_visibility(visibility), visibility: self.convert_visibility(visibility),
@ -86,7 +87,7 @@ impl JsonRenderer<'_> {
Inherited => Visibility::Default, Inherited => Visibility::Default,
Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate, Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
Restricted(did) => Visibility::Restricted { Restricted(did) => Visibility::Restricted {
parent: from_item_id(did.into()), parent: from_item_id(&did.into()),
path: self.tcx.def_path(did).to_string_no_crate_verbose(), path: self.tcx.def_path(did).to_string_no_crate_verbose(),
}, },
} }
@ -170,7 +171,7 @@ impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
} }
} }
crate fn from_item_id(did: ItemId) -> Id { crate fn from_item_id(did: &ItemId) -> Id {
match did { match did {
ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))), ItemId::DefId(did) => Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index))),
_ => todo!("how should json ItemId's be represented?"), _ => todo!("how should json ItemId's be represented?"),
@ -373,7 +374,7 @@ impl FromWithTcx<clean::Type> for Type {
match ty { match ty {
ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath { ResolvedPath { path, did, is_generic: _ } => Type::ResolvedPath {
name: path.whole_name(), name: path.whole_name(),
id: from_item_id(did.into()), id: from_item_id(&did.into()),
args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))), args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
param_names: Vec::new(), param_names: Vec::new(),
}, },
@ -385,7 +386,7 @@ impl FromWithTcx<clean::Type> for Type {
Type::ResolvedPath { Type::ResolvedPath {
name: path.whole_name(), name: path.whole_name(),
id: from_item_id(id.into()), id: from_item_id(&id.into()),
args: path args: path
.segments .segments
.last() .last()
@ -566,13 +567,13 @@ impl FromWithTcx<clean::Import> for Import {
Simple(s) => Import { Simple(s) => Import {
source: import.source.path.whole_name(), source: import.source.path.whole_name(),
name: s.to_string(), name: s.to_string(),
id: import.source.did.map(ItemId::from).map(from_item_id), id: import.source.did.map(ItemId::from).as_ref().map(from_item_id),
glob: false, glob: false,
}, },
Glob => Import { Glob => Import {
source: import.source.path.whole_name(), source: import.source.path.whole_name(),
name: import.source.path.last_name().to_string(), name: import.source.path.last_name().to_string(),
id: import.source.did.map(ItemId::from).map(from_item_id), id: import.source.did.map(ItemId::from).as_ref().map(from_item_id),
glob: true, glob: true,
}, },
} }
@ -666,5 +667,5 @@ impl FromWithTcx<ItemType> for ItemKind {
} }
fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> { fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(i.def_id)).collect() items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_item_id(&i.def_id)).collect()
} }

View file

@ -53,7 +53,7 @@ impl JsonRenderer<'tcx> {
.map(|i| { .map(|i| {
let item = &i.impl_item; let item = &i.impl_item;
self.item(item.clone()).unwrap(); self.item(item.clone()).unwrap();
from_item_id(item.def_id) from_item_id(&item.def_id)
}) })
.collect() .collect()
}) })
@ -71,7 +71,7 @@ impl JsonRenderer<'tcx> {
let item = &i.impl_item; let item = &i.impl_item;
if item.def_id.is_local() { if item.def_id.is_local() {
self.item(item.clone()).unwrap(); self.item(item.clone()).unwrap();
Some(from_item_id(item.def_id)) Some(from_item_id(&item.def_id))
} else { } else {
None None
} }
@ -91,9 +91,9 @@ impl JsonRenderer<'tcx> {
let trait_item = &trait_item.trait_; let trait_item = &trait_item.trait_;
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
Some(( Some((
from_item_id(id.into()), from_item_id(&id.into()),
types::Item { types::Item {
id: from_item_id(id.into()), id: from_item_id(&id.into()),
crate_id: id.krate.as_u32(), crate_id: id.krate.as_u32(),
name: self name: self
.cache .cache
@ -161,7 +161,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
// Flatten items that recursively store other items // Flatten items that recursively store other items
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap()); item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
let id = item.def_id; let id = item.def_id.clone();
if let Some(mut new_item) = self.convert_item(item) { if let Some(mut new_item) = self.convert_item(item) {
if let types::ItemEnum::Trait(ref mut t) = new_item.inner { if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
t.implementors = self.get_trait_implementors(id.expect_def_id()) t.implementors = self.get_trait_implementors(id.expect_def_id())
@ -170,7 +170,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
e.impls = self.get_impls(id.expect_def_id()) e.impls = self.get_impls(id.expect_def_id())
} }
let removed = self.index.borrow_mut().insert(from_item_id(id), new_item.clone()); let removed = self.index.borrow_mut().insert(from_item_id(&id), new_item.clone());
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
// to make sure the items are unique. The main place this happens is when an item, is // to make sure the items are unique. The main place this happens is when an item, is
@ -207,7 +207,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
.chain(self.cache.external_paths.clone().into_iter()) .chain(self.cache.external_paths.clone().into_iter())
.map(|(k, (path, kind))| { .map(|(k, (path, kind))| {
( (
from_item_id(k.into()), from_item_id(&k.into()),
types::ItemSummary { types::ItemSummary {
crate_id: k.krate.as_u32(), crate_id: k.krate.as_u32(),
path, path,

View file

@ -58,7 +58,7 @@ crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> { impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> {
fn fold_item(&mut self, item: Item) -> Option<Item> { fn fold_item(&mut self, item: Item) -> Option<Item> {
let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, &item.def_id) {
Some(hir_id) => hir_id, Some(hir_id) => hir_id,
None => { None => {
// If non-local, no need to check anything. // If non-local, no need to check anything.

View file

@ -838,41 +838,34 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
// find item's parent to resolve `Self` in item's docs below // find item's parent to resolve `Self` in item's docs below
debug!("looking for the `Self` type"); debug!("looking for the `Self` type");
let self_id = match item.def_id.as_def_id() { let self_id = item.def_id.as_def_id().and_then(|did| {
None => None, if (matches!(self.cx.tcx.def_kind(did), DefKind::Field)
Some(did) && matches!(
if (matches!(self.cx.tcx.def_kind(did), DefKind::Field) self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()),
&& matches!( DefKind::Variant
self.cx.tcx.def_kind(self.cx.tcx.parent(did).unwrap()), ))
DefKind::Variant
)) =>
{ {
self.cx.tcx.parent(did).and_then(|item_id| self.cx.tcx.parent(item_id)) self.cx.tcx.parent(did).and_then(|item_id| self.cx.tcx.parent(item_id))
} } else if matches!(
Some(did) self.cx.tcx.def_kind(did),
if matches!( DefKind::AssocConst
self.cx.tcx.def_kind(did), | DefKind::AssocFn
DefKind::AssocConst | DefKind::AssocTy
| DefKind::AssocFn | DefKind::Variant
| DefKind::AssocTy | DefKind::Field
| DefKind::Variant ) {
| DefKind::Field
) =>
{
self.cx.tcx.parent(did) self.cx.tcx.parent(did)
} } else if let Some(parent) = self.cx.tcx.parent(did) {
Some(did) => match self.cx.tcx.parent(did) {
// HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`. // HACK(jynelson): `clean` marks associated types as `TypedefItem`, not as `AssocTypeItem`.
// Fixing this breaks `fn render_deref_methods`. // Fixing this breaks `fn render_deref_methods`.
// As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item, // As a workaround, see if the parent of the item is an `impl`; if so this must be an associated item,
// regardless of what rustdoc wants to call it. // regardless of what rustdoc wants to call it.
Some(parent) => { let parent_kind = self.cx.tcx.def_kind(parent);
let parent_kind = self.cx.tcx.def_kind(parent); Some(if parent_kind == DefKind::Impl { parent } else { did })
Some(if parent_kind == DefKind::Impl { parent } else { did }) } else {
} Some(did)
None => Some(did), }
}, });
};
// FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly // FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly
let self_name = self_id.and_then(|self_id| { let self_name = self_id.and_then(|self_id| {
@ -916,7 +909,12 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
for md_link in markdown_links(&doc) { for md_link in markdown_links(&doc) {
let link = self.resolve_link(&item, &doc, &self_name, parent_node, krate, md_link); let link = self.resolve_link(&item, &doc, &self_name, parent_node, krate, md_link);
if let Some(link) = link { if let Some(link) = link {
self.cx.cache.intra_doc_links.entry(item.def_id).or_default().push(link); self.cx
.cache
.intra_doc_links
.entry(item.def_id.clone())
.or_default()
.push(link);
} }
} }
} }
@ -1712,7 +1710,7 @@ fn report_diagnostic(
DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>, DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>,
decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>), decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
) { ) {
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) {
Some(hir_id) => hir_id, Some(hir_id) => hir_id,
None => { None => {
// If non-local, no need to check anything. // If non-local, no need to check anything.

View file

@ -46,7 +46,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
// FIXME(eddyb) is this `doc(hidden)` check needed? // FIXME(eddyb) is this `doc(hidden)` check needed?
if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) { if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
let impls = get_auto_trait_and_blanket_impls(cx, def_id.into()); let impls = get_auto_trait_and_blanket_impls(cx, def_id.into());
new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id))); new_items.extend(impls.filter(|i| cx.inlined.insert(i.def_id.clone())));
} }
}); });
} }
@ -165,7 +165,7 @@ impl ItemCollector {
impl DocFolder for ItemCollector { impl DocFolder for ItemCollector {
fn fold_item(&mut self, i: Item) -> Option<Item> { fn fold_item(&mut self, i: Item) -> Option<Item> {
self.items.insert(i.def_id); self.items.insert(i.def_id.clone());
Some(self.fold_item_recur(i)) Some(self.fold_item_recur(i))
} }

View file

@ -84,7 +84,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
} }
crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) { let hir_id = match DocContext::as_local_hir_id(cx.tcx, &item.def_id) {
Some(hir_id) => hir_id, Some(hir_id) => hir_id,
None => { None => {
// If non-local, no need to check anything. // If non-local, no need to check anything.

View file

@ -168,7 +168,7 @@ fn extract_tags(
impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
fn fold_item(&mut self, item: Item) -> Option<Item> { fn fold_item(&mut self, item: Item) -> Option<Item> {
let tcx = self.cx.tcx; let tcx = self.cx.tcx;
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { let hir_id = match DocContext::as_local_hir_id(tcx, &item.def_id) {
Some(hir_id) => hir_id, Some(hir_id) => hir_id,
None => { None => {
// If non-local, no need to check anything. // If non-local, no need to check anything.

View file

@ -52,7 +52,7 @@ impl<'a> DocFolder for Stripper<'a> {
} }
} else { } else {
if self.update_retained { if self.update_retained {
self.retained.insert(i.def_id); self.retained.insert(i.def_id.clone());
} }
} }
Some(self.fold_item_recur(i)) Some(self.fold_item_recur(i))

View file

@ -100,7 +100,7 @@ impl<'a> DocFolder for Stripper<'a> {
let i = if fastreturn { let i = if fastreturn {
if self.update_retained { if self.update_retained {
self.retained.insert(i.def_id); self.retained.insert(i.def_id.clone());
} }
return Some(i); return Some(i);
} else { } else {
@ -108,7 +108,7 @@ impl<'a> DocFolder for Stripper<'a> {
}; };
if self.update_retained { if self.update_retained {
self.retained.insert(i.def_id); self.retained.insert(i.def_id.clone());
} }
Some(i) Some(i)
} }