Put clean::Trait extra information into a new struct to make it more coherent

This commit is contained in:
Guillaume Gomez 2021-02-12 14:33:32 +01:00
parent fa51c0f472
commit b5c8eea55d
7 changed files with 33 additions and 18 deletions

View File

@ -624,8 +624,12 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
debug!("record_extern_trait: {:?}", did);
let trait_ = build_external_trait(cx, did);
cx.external_traits
.borrow_mut()
.insert(did, (trait_, clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight)));
cx.external_traits.borrow_mut().insert(
did,
clean::TraitWithExtraInfo {
trait_,
is_spotlight: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight),
},
);
cx.active_extern_traits.remove(&did);
}

View File

@ -57,11 +57,18 @@ crate struct Crate {
crate primitives: Vec<(DefId, PrimitiveType)>,
// These are later on moved into `CACHEKEY`, leaving the map empty.
// Only here so that they can be filtered through the rustdoc passes.
crate external_traits: Rc<RefCell<FxHashMap<DefId, (Trait, bool)>>>,
crate external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
crate masked_crates: FxHashSet<CrateNum>,
crate collapsed: bool,
}
/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
#[derive(Clone, Debug)]
crate struct TraitWithExtraInfo {
crate trait_: Trait,
crate is_spotlight: bool,
}
#[derive(Clone, Debug)]
crate struct ExternalCrate {
crate name: Symbol,

View File

@ -55,7 +55,7 @@ crate struct DocContext<'tcx> {
/// Later on moved into `cache`
crate renderinfo: RenderInfo,
/// Later on moved through `clean::Crate` into `cache`
crate external_traits: Rc<RefCell<FxHashMap<DefId, (clean::Trait, bool)>>>,
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
crate active_extern_traits: FxHashSet<DefId>,

View File

@ -91,9 +91,10 @@ crate trait DocFolder: Sized {
{
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, (mut v, is_spotlight)) in external_traits {
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
c.external_traits.borrow_mut().insert(k, (v, is_spotlight));
for (k, mut v) in external_traits {
v.trait_.items =
v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
c.external_traits.borrow_mut().insert(k, v);
}
}
c

View File

@ -65,9 +65,7 @@ crate struct Cache {
/// Implementations of a crate should inherit the documentation of the
/// parent trait if no extra documentation is specified, and default methods
/// should show up in documentation about trait implementations.
///
/// The `bool` parameter is about if the trait is `spotlight`.
crate traits: FxHashMap<DefId, (clean::Trait, bool)>,
crate traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
/// When rendering traits, it's often useful to be able to list all
/// implementors of the trait, and this mapping is exactly, that: a mapping
@ -251,8 +249,12 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// Propagate a trait method's documentation to all implementors of the
// trait.
if let clean::TraitItem(ref t) = *item.kind {
self.cache.traits.entry(item.def_id).or_insert_with(|| {
(t.clone(), clean::utils::has_doc_flag(tcx.get_attrs(item.def_id), sym::spotlight))
self.cache.traits.entry(item.def_id).or_insert_with(|| clean::TraitWithExtraInfo {
trait_: t.clone(),
is_spotlight: clean::utils::has_doc_flag(
tcx.get_attrs(item.def_id),
sym::spotlight,
),
});
}

View File

@ -3688,7 +3688,7 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String {
for i in impls {
let impl_ = i.inner_impl();
if impl_.trait_.def_id().map_or(false, |d| {
cache.traits.get(&d).map(|(_, is_spotlight)| *is_spotlight).unwrap_or(false)
cache.traits.get(&d).map(|t| t.is_spotlight).unwrap_or(false)
}) {
if out.is_empty() {
write!(
@ -3980,7 +3980,7 @@ fn render_impl(
false,
outer_version,
outer_const_version,
trait_.map(|(t, _)| t),
trait_.map(|t| &t.trait_),
show_def_docs,
);
}
@ -4025,11 +4025,11 @@ fn render_impl(
// We don't emit documentation for default items if they appear in the
// Implementations on Foreign Types or Implementors sections.
if show_default_items {
if let Some((t, _)) = trait_ {
if let Some(t) = trait_ {
render_default_items(
w,
cx,
t,
&t.trait_,
&i.inner_impl(),
&i.impl_item,
render_mode,

View File

@ -84,9 +84,10 @@ impl JsonRenderer<'tcx> {
Rc::clone(&self.cache)
.traits
.iter()
.filter_map(|(&id, (trait_item, _))| {
.filter_map(|(&id, trait_item)| {
// only need to synthesize items for external traits
if !id.is_local() {
let trait_item = &trait_item.trait_;
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
Some((
from_def_id(id),