Remove is_spotlight field from `Trait`

This commit is contained in:
Guillaume Gomez 2021-01-28 17:05:22 +01:00
parent fe1bf8e05c
commit 58a34a4dde
10 changed files with 39 additions and 20 deletions

View File

@ -195,14 +195,12 @@ crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Tra
let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
let generics = filter_non_trait_generics(did, generics);
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
let is_spotlight = load_attrs(cx, did).clean(cx).has_doc_flag(sym::spotlight);
let is_auto = cx.tcx.trait_is_auto(did);
clean::Trait {
unsafety: cx.tcx.trait_def(did).unsafety,
generics,
items: trait_items,
bounds: supertrait_bounds,
is_spotlight,
is_auto,
}
}
@ -626,6 +624,8 @@ 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_);
cx.external_traits
.borrow_mut()
.insert(did, (trait_, clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight)));
cx.active_extern_traits.remove(&did);
}

View File

@ -2003,14 +2003,11 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
.iter()
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
.collect();
let attrs = item.attrs.clean(cx);
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
TraitItem(Trait {
unsafety,
items,
generics: generics.clean(cx),
bounds: bounds.clean(cx),
is_spotlight,
is_auto: is_auto.clean(cx),
})
}

View File

@ -57,7 +57,7 @@ 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>>>,
crate external_traits: Rc<RefCell<FxHashMap<DefId, (Trait, bool)>>>,
crate masked_crates: FxHashSet<CrateNum>,
crate collapsed: bool,
}
@ -1185,7 +1185,6 @@ crate struct Trait {
crate items: Vec<Item>,
crate generics: Generics,
crate bounds: Vec<GenericBound>,
crate is_spotlight: bool,
crate is_auto: bool,
}

View File

@ -7,13 +7,14 @@ use crate::clean::{
};
use crate::core::DocContext;
use rustc_attr::list_contains_name;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
use rustc_middle::ty::{self, Attributes, DefIdTree, Ty, TyCtxt};
use rustc_span::symbol::{kw, sym, Symbol};
use std::mem;
@ -520,3 +521,18 @@ crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<De
None
}
}
/// Checks that one attribute is `doc`. For example:
///
/// ```text
/// #[doc(spotlight)]
/// ```
///
/// This function has to exists because it runs on `hir::Attributes` whereas the other runs on
/// `clean::Attributes`.
crate fn has_doc_flag(attrs: Attributes<'_>, flag: Symbol) -> bool {
attrs.iter().any(|attr| {
attr.has_name(sym::doc)
&& attr.meta_item_list().map_or(false, |l| list_contains_name(&l, flag))
})
}

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>>>,
crate external_traits: Rc<RefCell<FxHashMap<DefId, (clean::Trait, bool)>>>,
/// 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,9 @@ crate trait DocFolder: Sized {
{
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, mut v) in external_traits {
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);
c.external_traits.borrow_mut().insert(k, (v, is_spotlight));
}
}
c

View File

@ -7,6 +7,7 @@ use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::TyCtxt;
use rustc_span::source_map::FileName;
use rustc_span::symbol::sym;
use rustc_span::Symbol;
use crate::clean::{self, GetDefId};
@ -64,7 +65,9 @@ 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.
crate traits: FxHashMap<DefId, clean::Trait>,
///
/// The `bool` parameter is about if the trait is `spotlight`.
crate traits: FxHashMap<DefId, (clean::Trait, bool)>,
/// 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
@ -244,10 +247,13 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
}
}
let tcx = self.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());
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))
});
}
// Collect all the implementors of traits.

View File

@ -3687,8 +3687,9 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String {
if let Some(impls) = cache.impls.get(&did) {
for i in impls {
let impl_ = i.inner_impl();
if impl_.trait_.def_id_full(cache).map_or(false, |d| cache.traits[&d].is_spotlight)
{
if impl_.trait_.def_id().map_or(false, |d| {
cache.traits.get(&d).map(|(_, is_spotlight)| *is_spotlight).unwrap_or(false)
}) {
if out.is_empty() {
write!(
&mut out,
@ -3979,7 +3980,7 @@ fn render_impl(
false,
outer_version,
outer_const_version,
trait_,
trait_.map(|(t, _)| t),
show_def_docs,
);
}
@ -4024,7 +4025,7 @@ 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,

View File

@ -408,7 +408,7 @@ impl From<clean::FnDecl> for FnDecl {
impl From<clean::Trait> for Trait {
fn from(trait_: clean::Trait) -> Self {
let clean::Trait { unsafety, items, generics, bounds, is_spotlight: _, is_auto } = trait_;
let clean::Trait { unsafety, items, generics, bounds, is_auto } = trait_;
Trait {
is_auto,
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,

View File

@ -84,7 +84,7 @@ 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() {
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());