1
Fork 0

Use empty Cache for methods requiring it when filling Cache itself

This commit is contained in:
Guillaume Gomez 2021-01-13 22:06:41 +01:00
parent 9fd5a67984
commit c448270099

View file

@ -193,7 +193,11 @@ impl Cache {
}
cache.stack.push(krate.name.to_string());
krate = cache.fold_crate(krate);
krate = {
let mut cache_wrapper = CacheWrapper { cache: &mut cache, tmp_cache: Cache::default() };
cache_wrapper.fold_crate(krate)
};
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
if cache.traits.contains_key(&trait_did) {
@ -207,7 +211,15 @@ impl Cache {
}
}
impl DocFolder for Cache {
/// This struct is needed because we need to use an empty `Cache` for all functions requiring
/// a `Cache`. If we use the already filled one (`cache` in here), it'll provide information
/// about implementations that aren't related to the type being checked.
struct CacheWrapper<'a> {
cache: &'a mut Cache,
tmp_cache: Cache,
}
impl<'a> DocFolder for CacheWrapper<'a> {
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
if item.def_id.is_local() {
debug!("folding {} \"{:?}\", id {:?}", item.type_(), item.name, item.def_id);
@ -217,17 +229,21 @@ impl DocFolder for Cache {
// we don't want it or its children in the search index.
let orig_stripped_mod = match *item.kind {
clean::StrippedItem(box clean::ModuleItem(..)) => {
mem::replace(&mut self.stripped_mod, true)
mem::replace(&mut self.cache.stripped_mod, true)
}
_ => self.stripped_mod,
_ => self.cache.stripped_mod,
};
// If the impl is from a masked crate or references something from a
// masked crate then remove it completely.
if let clean::ImplItem(ref i) = *item.kind {
if self.masked_crates.contains(&item.def_id.krate)
|| i.trait_.def_id(self).map_or(false, |d| self.masked_crates.contains(&d.krate))
|| i.for_.def_id(self).map_or(false, |d| self.masked_crates.contains(&d.krate))
if self.cache.masked_crates.contains(&item.def_id.krate)
|| i.trait_
.def_id(&self.tmp_cache)
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
|| i.for_
.def_id(&self.tmp_cache)
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
{
return None;
}
@ -236,14 +252,15 @@ impl DocFolder for Cache {
// Propagate a trait method's documentation to all implementors of the
// trait.
if let clean::TraitItem(ref t) = *item.kind {
self.traits.entry(item.def_id).or_insert_with(|| t.clone());
self.cache.traits.entry(item.def_id).or_insert_with(|| t.clone());
}
// Collect all the implementors of traits.
if let clean::ImplItem(ref i) = *item.kind {
if let Some(did) = i.trait_.def_id(self) {
if let Some(did) = i.trait_.def_id(&self.tmp_cache) {
if i.blanket_impl.is_none() {
self.implementors
self.cache
.implementors
.entry(did)
.or_default()
.push(Impl { impl_item: item.clone() });
@ -256,7 +273,7 @@ impl DocFolder for Cache {
let (parent, is_inherent_impl_item) = match *item.kind {
clean::StrippedItem(..) => ((None, None), false),
clean::AssocConstItem(..) | clean::TypedefItem(_, true)
if self.parent_is_trait_impl =>
if self.cache.parent_is_trait_impl =>
{
// skip associated items in trait impls
((None, None), false)
@ -266,18 +283,18 @@ impl DocFolder for Cache {
| clean::StructFieldItem(..)
| clean::VariantItem(..) => (
(
Some(*self.parent_stack.last().expect("parent_stack is empty")),
Some(&self.stack[..self.stack.len() - 1]),
Some(*self.cache.parent_stack.last().expect("parent_stack is empty")),
Some(&self.cache.stack[..self.cache.stack.len() - 1]),
),
false,
),
clean::MethodItem(..) | clean::AssocConstItem(..) => {
if self.parent_stack.is_empty() {
if self.cache.parent_stack.is_empty() {
((None, None), false)
} else {
let last = self.parent_stack.last().expect("parent_stack is empty 2");
let last = self.cache.parent_stack.last().expect("parent_stack is empty 2");
let did = *last;
let path = match self.paths.get(&did) {
let path = match self.cache.paths.get(&did) {
// The current stack not necessarily has correlation
// for where the type was defined. On the other
// hand, `paths` always has the right
@ -289,24 +306,24 @@ impl DocFolder for Cache {
| ItemType::Union
| ItemType::Enum,
)) => Some(&fqp[..fqp.len() - 1]),
Some(..) => Some(&*self.stack),
Some(..) => Some(&*self.cache.stack),
None => None,
};
((Some(*last), path), true)
}
}
_ => ((None, Some(&*self.stack)), false),
_ => ((None, Some(&*self.cache.stack)), false),
};
match parent {
(parent, Some(path)) if is_inherent_impl_item || !self.stripped_mod => {
(parent, Some(path)) if is_inherent_impl_item || !self.cache.stripped_mod => {
debug_assert!(!item.is_stripped());
// 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.index != CRATE_DEF_INDEX {
self.search_index.push(IndexItem {
self.cache.search_index.push(IndexItem {
ty: item.type_(),
name: s.to_string(),
path: path.join("::"),
@ -315,21 +332,22 @@ impl DocFolder for Cache {
.map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
parent,
parent_idx: None,
search_type: get_index_search_type(&item, self),
search_type: get_index_search_type(&item, &self.tmp_cache),
});
for alias in item.attrs.get_doc_aliases() {
self.aliases
self.cache
.aliases
.entry(alias.to_lowercase())
.or_insert(Vec::new())
.push(self.search_index.len() - 1);
.push(self.cache.search_index.len() - 1);
}
}
}
(Some(parent), None) if is_inherent_impl_item => {
// We have a parent, but we don't know where they're
// defined yet. Wait for later to index this item.
self.orphan_impl_items.push((parent, item.clone()));
self.cache.orphan_impl_items.push((parent, item.clone()));
}
_ => {}
}
@ -338,7 +356,7 @@ impl DocFolder for Cache {
// Keep track of the fully qualified path for this item.
let pushed = match item.name {
Some(n) if !n.is_empty() => {
self.stack.push(n.to_string());
self.cache.stack.push(n.to_string());
true
}
_ => false,
@ -360,7 +378,7 @@ impl DocFolder for Cache {
| clean::MacroItem(..)
| clean::ProcMacroItem(..)
| clean::VariantItem(..)
if !self.stripped_mod =>
if !self.cache.stripped_mod =>
{
// Re-exported items mean that the same id can show up twice
// in the rustdoc ast that we're looking at. We know,
@ -368,21 +386,21 @@ impl DocFolder for Cache {
// `public_items` map, so we can skip inserting into the
// paths map if there was already an entry present and we're
// not a public item.
if !self.paths.contains_key(&item.def_id)
|| self.access_levels.is_public(item.def_id)
if !self.cache.paths.contains_key(&item.def_id)
|| self.cache.access_levels.is_public(item.def_id)
{
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
self.cache.paths.insert(item.def_id, (self.cache.stack.clone(), item.type_()));
}
}
clean::PrimitiveItem(..) => {
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
self.cache.paths.insert(item.def_id, (self.cache.stack.clone(), item.type_()));
}
_ => {}
}
// Maintain the parent stack
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
let orig_parent_is_trait_impl = self.cache.parent_is_trait_impl;
let parent_pushed = match *item.kind {
clean::TraitItem(..)
| clean::EnumItem(..)
@ -390,24 +408,24 @@ impl DocFolder for Cache {
| clean::StructItem(..)
| clean::UnionItem(..)
| clean::VariantItem(..) => {
self.parent_stack.push(item.def_id);
self.parent_is_trait_impl = false;
self.cache.parent_stack.push(item.def_id);
self.cache.parent_is_trait_impl = false;
true
}
clean::ImplItem(ref i) => {
self.parent_is_trait_impl = i.trait_.is_some();
self.cache.parent_is_trait_impl = i.trait_.is_some();
match i.for_ {
clean::ResolvedPath { did, .. } => {
self.parent_stack.push(did);
self.cache.parent_stack.push(did);
true
}
ref t => {
let prim_did = t
.primitive_type()
.and_then(|t| self.primitive_locations.get(&t).cloned());
.and_then(|t| self.cache.primitive_locations.get(&t).cloned());
match prim_did {
Some(did) => {
self.parent_stack.push(did);
self.cache.parent_stack.push(did);
true
}
None => false,
@ -432,8 +450,9 @@ impl DocFolder for Cache {
dids.insert(did);
}
ref t => {
let did =
t.primitive_type().and_then(|t| self.primitive_locations.get(&t).cloned());
let did = t
.primitive_type()
.and_then(|t| self.cache.primitive_locations.get(&t).cloned());
if let Some(did) = did {
dids.insert(did);
@ -443,19 +462,22 @@ impl DocFolder for Cache {
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
for bound in generics {
if let Some(did) = bound.def_id(self) {
if let Some(did) = bound.def_id(&self.tmp_cache) {
dids.insert(did);
}
}
}
let impl_item = Impl { impl_item: item };
if impl_item.trait_did(self).map_or(true, |d| self.traits.contains_key(&d)) {
if impl_item
.trait_did(&self.tmp_cache)
.map_or(true, |d| self.cache.traits.contains_key(&d))
{
for did in dids {
self.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
self.cache.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
}
} else {
let trait_did = impl_item.trait_did(self).expect("no trait did");
self.orphan_trait_impls.push((trait_did, dids, impl_item));
let trait_did = impl_item.trait_did(&self.tmp_cache).expect("no trait did");
self.cache.orphan_trait_impls.push((trait_did, dids, impl_item));
}
None
} else {
@ -463,13 +485,13 @@ impl DocFolder for Cache {
};
if pushed {
self.stack.pop().expect("stack already empty");
self.cache.stack.pop().expect("stack already empty");
}
if parent_pushed {
self.parent_stack.pop().expect("parent stack already empty");
self.cache.parent_stack.pop().expect("parent stack already empty");
}
self.stripped_mod = orig_stripped_mod;
self.parent_is_trait_impl = orig_parent_is_trait_impl;
self.cache.stripped_mod = orig_stripped_mod;
self.cache.parent_is_trait_impl = orig_parent_is_trait_impl;
ret
}
}