Auto merge of #80914 - GuillaumeGomez:remove-is_spotlight, r=jyn514
Remove is_spotlight field from `Trait` Small PR, only the last commit is relevant here. The rest is coming from #80883 because I need the `TyCtxt` stored inside `Cache`. The point is to make ItemKind looks as close as possible to the compiler type so that it makes the switch simpler (which is why I make all these "small" PRs). r? `@jyn514`
This commit is contained in:
commit
a8486b64b0
11 changed files with 73 additions and 18 deletions
|
@ -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,10 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
|
|||
debug!("record_extern_trait: {:?}", did);
|
||||
let trait_ = build_external_trait(cx, did);
|
||||
|
||||
let trait_ = clean::TraitWithExtraInfo {
|
||||
trait_,
|
||||
is_spotlight: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight),
|
||||
};
|
||||
cx.external_traits.borrow_mut().insert(did, trait_);
|
||||
cx.active_extern_traits.remove(&did);
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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>>>,
|
||||
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,
|
||||
|
@ -1185,7 +1192,6 @@ crate struct Trait {
|
|||
crate items: Vec<Item>,
|
||||
crate generics: Generics,
|
||||
crate bounds: Vec<GenericBound>,
|
||||
crate is_spotlight: bool,
|
||||
crate is_auto: bool,
|
||||
}
|
||||
|
||||
|
|
|
@ -520,3 +520,19 @@ crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<De
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks for the existence of `hidden` in the attribute below if `flag` is `sym::hidden`:
|
||||
///
|
||||
/// ```
|
||||
/// #[doc(hidden)]
|
||||
/// pub fn foo() {}
|
||||
/// ```
|
||||
///
|
||||
/// This function exists because it runs on `hir::Attributes` whereas the other is a
|
||||
/// `clean::Attributes` method.
|
||||
crate fn has_doc_flag(attrs: ty::Attributes<'_>, flag: Symbol) -> bool {
|
||||
attrs.iter().any(|attr| {
|
||||
attr.has_name(sym::doc)
|
||||
&& attr.meta_item_list().map_or(false, |l| rustc_attr::list_contains_name(&l, flag))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ use std::{cell::RefCell, collections::hash_map::Entry};
|
|||
|
||||
use crate::clean;
|
||||
use crate::clean::inline::build_external_trait;
|
||||
use crate::clean::{AttributesExt, MAX_DEF_IDX};
|
||||
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
|
||||
use crate::config::{Options as RustdocOptions, RenderOptions};
|
||||
use crate::config::{OutputFormat, RenderInfo};
|
||||
use crate::formats::cache::Cache;
|
||||
|
@ -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::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>,
|
||||
|
@ -538,7 +538,10 @@ crate fn run_global_ctxt(
|
|||
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
|
||||
let mut sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
|
||||
sized_trait.is_auto = true;
|
||||
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
|
||||
ctxt.external_traits.borrow_mut().insert(
|
||||
sized_trait_did,
|
||||
TraitWithExtraInfo { trait_: sized_trait, is_spotlight: false },
|
||||
);
|
||||
}
|
||||
|
||||
debug!("crate: {:?}", tcx.hir().krate());
|
||||
|
|
|
@ -92,7 +92,8 @@ crate trait DocFolder: Sized {
|
|||
{
|
||||
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
|
||||
for (k, mut v) in external_traits {
|
||||
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
|
||||
v.trait_.items =
|
||||
v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
|
||||
c.external_traits.borrow_mut().insert(k, v);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,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.
|
||||
crate traits: FxHashMap<DefId, clean::Trait>,
|
||||
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
|
||||
|
@ -247,7 +248,10 @@ 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());
|
||||
self.cache.traits.entry(item.def_id).or_insert_with(|| clean::TraitWithExtraInfo {
|
||||
trait_: t.clone(),
|
||||
is_spotlight: item.attrs.has_doc_flag(sym::spotlight),
|
||||
});
|
||||
}
|
||||
|
||||
// Collect all the implementors of traits.
|
||||
|
|
|
@ -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(|t| t.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.trait_),
|
||||
show_def_docs,
|
||||
);
|
||||
}
|
||||
|
@ -4028,7 +4029,7 @@ fn render_impl(
|
|||
render_default_items(
|
||||
w,
|
||||
cx,
|
||||
t,
|
||||
&t.trait_,
|
||||
&i.inner_impl(),
|
||||
&i.impl_item,
|
||||
render_mode,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -87,6 +87,7 @@ impl JsonRenderer<'tcx> {
|
|||
.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),
|
||||
|
|
24
src/test/rustdoc/spotlight-from-dependency.rs
Normal file
24
src/test/rustdoc/spotlight-from-dependency.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
#![crate_name = "foo"]
|
||||
|
||||
use std::iter::Iterator;
|
||||
|
||||
// @has foo/struct.Odd.html
|
||||
// @has - '//h4[@id="method.new"]//span[@class="notable-traits"]//code/span' 'impl Iterator for Odd'
|
||||
pub struct Odd {
|
||||
current: usize,
|
||||
}
|
||||
|
||||
impl Odd {
|
||||
pub fn new() -> Odd {
|
||||
Odd { current: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Odd {
|
||||
type Item = usize;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.current += 2;
|
||||
Some(self.current - 2)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue