Skip to content

Commit

Permalink
Store links in Cache instead of on items directly
Browse files Browse the repository at this point in the history
Items are first built after rustdoc creates the TyCtxt. To allow
resolving the links before the TyCtxt is built, the links can't be
stored on `clean::Item` directly.
  • Loading branch information
jyn514 committed Apr 6, 2021
1 parent 16143d1 commit 9e11902
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 82 deletions.
137 changes: 64 additions & 73 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,69 @@ impl Item {
}

crate fn links(&self, cache: &Cache) -> Vec<RenderedLink> {
self.attrs.links(self.def_id.krate, cache)
use crate::html::format::href;
use crate::html::render::CURRENT_DEPTH;

cache
.intra_doc_links
.get(&self.def_id)
.map_or(&[][..], |v| v.as_slice())
.iter()
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
match *did {
Some(did) => {
if let Some((mut href, ..)) = href(did, cache) {
if let Some(ref fragment) = *fragment {
href.push('#');
href.push_str(fragment);
}
Some(RenderedLink {
original_text: s.clone(),
new_text: link_text.clone(),
href,
})
} else {
None
}
}
None => {
if let Some(ref fragment) = *fragment {
let url = match cache.extern_locations.get(&self.def_id.krate) {
Some(&(_, _, ExternalLocation::Local)) => {
let depth = CURRENT_DEPTH.with(|l| l.get());
"../".repeat(depth)
}
Some(&(_, _, ExternalLocation::Remote(ref s))) => s.to_string(),
Some(&(_, _, ExternalLocation::Unknown)) | None => String::from(
// NOTE: intentionally doesn't pass crate name to avoid having
// different primitive links between crates
if UnstableFeatures::from_environment(None).is_nightly_build() {
"https://doc.rust-lang.org/nightly"
} else {
"https://doc.rust-lang.org"
},
),
};
// This is a primitive so the url is done "by hand".
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
Some(RenderedLink {
original_text: s.clone(),
new_text: link_text.clone(),
href: format!(
"{}{}std/primitive.{}.html{}",
url,
if !url.ends_with('/') { "/" } else { "" },
&fragment[..tail],
&fragment[tail..]
),
})
} else {
panic!("This isn't a primitive?!");
}
}
}
})
.collect()
}

crate fn is_crate(&self) -> bool {
Expand Down Expand Up @@ -570,15 +632,13 @@ crate struct Attributes {
crate other_attrs: Vec<ast::Attribute>,
crate cfg: Option<Arc<Cfg>>,
crate span: Option<rustc_span::Span>,
/// map from Rust paths to resolved defs and potential URL fragments
crate links: Vec<ItemLink>,
crate inner_docs: bool,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
/// A link that has not yet been rendered.
///
/// This link will be turned into a rendered link by [`Attributes::links`]
/// This link will be turned into a rendered link by [`Item::links`].
crate struct ItemLink {
/// The original link written in the markdown
pub(crate) link: String,
Expand Down Expand Up @@ -804,7 +864,6 @@ impl Attributes {
other_attrs,
cfg: if cfg == Cfg::True { None } else { Some(Arc::new(cfg)) },
span: sp,
links: vec![],
inner_docs,
}
}
Expand Down Expand Up @@ -848,72 +907,6 @@ impl Attributes {
if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) }
}

/// Gets links as a vector
///
/// Cache must be populated before call
crate fn links(&self, krate: CrateNum, cache: &Cache) -> Vec<RenderedLink> {
use crate::html::format::href;
use crate::html::render::CURRENT_DEPTH;

self.links
.iter()
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
match *did {
Some(did) => {
if let Some((mut href, ..)) = href(did, cache) {
if let Some(ref fragment) = *fragment {
href.push('#');
href.push_str(fragment);
}
Some(RenderedLink {
original_text: s.clone(),
new_text: link_text.clone(),
href,
})
} else {
None
}
}
None => {
if let Some(ref fragment) = *fragment {
let url = match cache.extern_locations.get(&krate) {
Some(&(_, _, ExternalLocation::Local)) => {
let depth = CURRENT_DEPTH.with(|l| l.get());
"../".repeat(depth)
}
Some(&(_, _, ExternalLocation::Remote(ref s))) => s.to_string(),
Some(&(_, _, ExternalLocation::Unknown)) | None => String::from(
// NOTE: intentionally doesn't pass crate name to avoid having
// different primitive links between crates
if UnstableFeatures::from_environment(None).is_nightly_build() {
"https://doc.rust-lang.org/nightly"
} else {
"https://doc.rust-lang.org"
},
),
};
// This is a primitive so the url is done "by hand".
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
Some(RenderedLink {
original_text: s.clone(),
new_text: link_text.clone(),
href: format!(
"{}{}std/primitive.{}.html{}",
url,
if !url.ends_with('/') { "/" } else { "" },
&fragment[..tail],
&fragment[tail..]
),
})
} else {
panic!("This isn't a primitive?!");
}
}
}
})
.collect()
}

crate fn get_doc_aliases(&self) -> Box<[String]> {
let mut aliases = FxHashSet::default();

Expand All @@ -940,7 +933,6 @@ impl PartialEq for Attributes {
self.doc_strings == rhs.doc_strings
&& self.cfg == rhs.cfg
&& self.span == rhs.span
&& self.links == rhs.links
&& self
.other_attrs
.iter()
Expand All @@ -956,7 +948,6 @@ impl Hash for Attributes {
self.doc_strings.hash(hasher);
self.cfg.hash(hasher);
self.span.hash(hasher);
self.links.hash(hasher);
for attr in &self.other_attrs {
attr.id.hash(hasher);
}
Expand Down
5 changes: 5 additions & 0 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ crate struct Cache {
// when gathering trait documentation on a type, hold impls here while
// folding and add them to the cache later on if we find the trait.
orphan_trait_impls: Vec<(DefId, FxHashSet<DefId>, Impl)>,

/// All intra-doc links resolved so far.
///
/// Links are indexed by the DefId of the item they document.
crate intra_doc_links: BTreeMap<DefId, Vec<clean::ItemLink>>,
}

/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.
Expand Down
18 changes: 11 additions & 7 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ use std::collections::HashSet;
impl JsonRenderer<'_> {
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
let deprecation = item.deprecation(self.tcx);
let links = self
.cache
.intra_doc_links
.get(&item.def_id)
.into_iter()
.flatten()
.filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link.clone(), from_def_id(did)))
})
.collect();
let clean::Item { span, name, attrs, kind, visibility, def_id } = item;
let inner = match *kind {
clean::StrippedItem(_) => return None,
Expand All @@ -36,20 +46,14 @@ impl JsonRenderer<'_> {
span: self.convert_span(span),
visibility: self.convert_visibility(visibility),
docs: attrs.collapsed_doc_value(),
links: attrs
.links
.into_iter()
.filter_map(|clean::ItemLink { link, did, .. }| {
did.map(|did| (link, from_def_id(did)))
})
.collect(),
attrs: attrs
.other_attrs
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(),
deprecation: deprecation.map(from_deprecation),
inner,
links,
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ fn is_derive_trait_collision<T>(ns: &PerNS<Result<(Res, T), ResolutionFailure<'_
}

impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
fn fold_item(&mut self, mut item: Item) -> Option<Item> {
fn fold_item(&mut self, item: Item) -> Option<Item> {
use rustc_middle::ty::DefIdTree;

let parent_node = if item.is_fake() {
Expand Down Expand Up @@ -873,7 +873,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
for md_link in markdown_links(&doc) {
let link = self.resolve_link(&item, &doc, &self_name, parent_node, krate, md_link);
if let Some(link) = link {
item.attrs.links.push(link);
self.cx.cache.intra_doc_links.entry(item.def_id).or_default().push(link);
}
}
}
Expand Down

0 comments on commit 9e11902

Please sign in to comment.