Skip to content

Commit 58a34a4

Browse files
Remove is_spotlight field from Trait
1 parent fe1bf8e commit 58a34a4

File tree

10 files changed

+39
-20
lines changed

10 files changed

+39
-20
lines changed

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,12 @@ crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Tra
195195
let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
196196
let generics = filter_non_trait_generics(did, generics);
197197
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
198-
let is_spotlight = load_attrs(cx, did).clean(cx).has_doc_flag(sym::spotlight);
199198
let is_auto = cx.tcx.trait_is_auto(did);
200199
clean::Trait {
201200
unsafety: cx.tcx.trait_def(did).unsafety,
202201
generics,
203202
items: trait_items,
204203
bounds: supertrait_bounds,
205-
is_spotlight,
206204
is_auto,
207205
}
208206
}
@@ -626,6 +624,8 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
626624
debug!("record_extern_trait: {:?}", did);
627625
let trait_ = build_external_trait(cx, did);
628626

629-
cx.external_traits.borrow_mut().insert(did, trait_);
627+
cx.external_traits
628+
.borrow_mut()
629+
.insert(did, (trait_, clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight)));
630630
cx.active_extern_traits.remove(&did);
631631
}

src/librustdoc/clean/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2003,14 +2003,11 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20032003
.iter()
20042004
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
20052005
.collect();
2006-
let attrs = item.attrs.clean(cx);
2007-
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
20082006
TraitItem(Trait {
20092007
unsafety,
20102008
items,
20112009
generics: generics.clean(cx),
20122010
bounds: bounds.clean(cx),
2013-
is_spotlight,
20142011
is_auto: is_auto.clean(cx),
20152012
})
20162013
}

src/librustdoc/clean/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ crate struct Crate {
5757
crate primitives: Vec<(DefId, PrimitiveType)>,
5858
// These are later on moved into `CACHEKEY`, leaving the map empty.
5959
// Only here so that they can be filtered through the rustdoc passes.
60-
crate external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
60+
crate external_traits: Rc<RefCell<FxHashMap<DefId, (Trait, bool)>>>,
6161
crate masked_crates: FxHashSet<CrateNum>,
6262
crate collapsed: bool,
6363
}
@@ -1185,7 +1185,6 @@ crate struct Trait {
11851185
crate items: Vec<Item>,
11861186
crate generics: Generics,
11871187
crate bounds: Vec<GenericBound>,
1188-
crate is_spotlight: bool,
11891188
crate is_auto: bool,
11901189
}
11911190

src/librustdoc/clean/utils.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use crate::clean::{
77
};
88
use crate::core::DocContext;
99

10+
use rustc_attr::list_contains_name;
1011
use rustc_data_structures::fx::FxHashSet;
1112
use rustc_hir as hir;
1213
use rustc_hir::def::{DefKind, Res};
1314
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1415
use rustc_middle::mir::interpret::ConstValue;
1516
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
16-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
17+
use rustc_middle::ty::{self, Attributes, DefIdTree, Ty, TyCtxt};
1718
use rustc_span::symbol::{kw, sym, Symbol};
1819
use std::mem;
1920

@@ -520,3 +521,18 @@ crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<De
520521
None
521522
}
522523
}
524+
525+
/// Checks that one attribute is `doc`. For example:
526+
///
527+
/// ```text
528+
/// #[doc(spotlight)]
529+
/// ```
530+
///
531+
/// This function has to exists because it runs on `hir::Attributes` whereas the other runs on
532+
/// `clean::Attributes`.
533+
crate fn has_doc_flag(attrs: Attributes<'_>, flag: Symbol) -> bool {
534+
attrs.iter().any(|attr| {
535+
attr.has_name(sym::doc)
536+
&& attr.meta_item_list().map_or(false, |l| list_contains_name(&l, flag))
537+
})
538+
}

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ crate struct DocContext<'tcx> {
5555
/// Later on moved into `cache`
5656
crate renderinfo: RenderInfo,
5757
/// Later on moved through `clean::Crate` into `cache`
58-
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
58+
crate external_traits: Rc<RefCell<FxHashMap<DefId, (clean::Trait, bool)>>>,
5959
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
6060
/// the same time.
6161
crate active_extern_traits: FxHashSet<DefId>,

src/librustdoc/fold.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ crate trait DocFolder: Sized {
9191

9292
{
9393
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
94-
for (k, mut v) in external_traits {
94+
for (k, (mut v, is_spotlight)) in external_traits {
9595
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
96-
c.external_traits.borrow_mut().insert(k, v);
96+
c.external_traits.borrow_mut().insert(k, (v, is_spotlight));
9797
}
9898
}
9999
c

src/librustdoc/formats/cache.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
77
use rustc_middle::middle::privacy::AccessLevels;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_span::source_map::FileName;
10+
use rustc_span::symbol::sym;
1011
use rustc_span::Symbol;
1112

1213
use crate::clean::{self, GetDefId};
@@ -64,7 +65,9 @@ crate struct Cache {
6465
/// Implementations of a crate should inherit the documentation of the
6566
/// parent trait if no extra documentation is specified, and default methods
6667
/// should show up in documentation about trait implementations.
67-
crate traits: FxHashMap<DefId, clean::Trait>,
68+
///
69+
/// The `bool` parameter is about if the trait is `spotlight`.
70+
crate traits: FxHashMap<DefId, (clean::Trait, bool)>,
6871

6972
/// When rendering traits, it's often useful to be able to list all
7073
/// implementors of the trait, and this mapping is exactly, that: a mapping
@@ -244,10 +247,13 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
244247
}
245248
}
246249

250+
let tcx = self.tcx;
247251
// Propagate a trait method's documentation to all implementors of the
248252
// trait.
249253
if let clean::TraitItem(ref t) = *item.kind {
250-
self.cache.traits.entry(item.def_id).or_insert_with(|| t.clone());
254+
self.cache.traits.entry(item.def_id).or_insert_with(|| {
255+
(t.clone(), clean::utils::has_doc_flag(tcx.get_attrs(item.def_id), sym::spotlight))
256+
});
251257
}
252258

253259
// Collect all the implementors of traits.

src/librustdoc/html/render/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3687,8 +3687,9 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String {
36873687
if let Some(impls) = cache.impls.get(&did) {
36883688
for i in impls {
36893689
let impl_ = i.inner_impl();
3690-
if impl_.trait_.def_id_full(cache).map_or(false, |d| cache.traits[&d].is_spotlight)
3691-
{
3690+
if impl_.trait_.def_id().map_or(false, |d| {
3691+
cache.traits.get(&d).map(|(_, is_spotlight)| *is_spotlight).unwrap_or(false)
3692+
}) {
36923693
if out.is_empty() {
36933694
write!(
36943695
&mut out,
@@ -3979,7 +3980,7 @@ fn render_impl(
39793980
false,
39803981
outer_version,
39813982
outer_const_version,
3982-
trait_,
3983+
trait_.map(|(t, _)| t),
39833984
show_def_docs,
39843985
);
39853986
}
@@ -4024,7 +4025,7 @@ fn render_impl(
40244025
// We don't emit documentation for default items if they appear in the
40254026
// Implementations on Foreign Types or Implementors sections.
40264027
if show_default_items {
4027-
if let Some(t) = trait_ {
4028+
if let Some((t, _)) = trait_ {
40284029
render_default_items(
40294030
w,
40304031
cx,

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl From<clean::FnDecl> for FnDecl {
408408

409409
impl From<clean::Trait> for Trait {
410410
fn from(trait_: clean::Trait) -> Self {
411-
let clean::Trait { unsafety, items, generics, bounds, is_spotlight: _, is_auto } = trait_;
411+
let clean::Trait { unsafety, items, generics, bounds, is_auto } = trait_;
412412
Trait {
413413
is_auto,
414414
is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,

src/librustdoc/json/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl JsonRenderer<'tcx> {
8484
Rc::clone(&self.cache)
8585
.traits
8686
.iter()
87-
.filter_map(|(&id, trait_item)| {
87+
.filter_map(|(&id, (trait_item, _))| {
8888
// only need to synthesize items for external traits
8989
if !id.is_local() {
9090
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());

0 commit comments

Comments
 (0)