Skip to content

Commit a8486b6

Browse files
committed
Auto merge of rust-lang#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 rust-lang#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`
2 parents 6b56603 + 33aaead commit a8486b6

File tree

11 files changed

+73
-18
lines changed

11 files changed

+73
-18
lines changed

src/librustdoc/clean/inline.rs

+4-2
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,10 @@ 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

627+
let trait_ = clean::TraitWithExtraInfo {
628+
trait_,
629+
is_spotlight: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight),
630+
};
629631
cx.external_traits.borrow_mut().insert(did, trait_);
630632
cx.active_extern_traits.remove(&did);
631633
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ 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, TraitWithExtraInfo>>>,
6161
crate masked_crates: FxHashSet<CrateNum>,
6262
crate collapsed: bool,
6363
}
6464

65+
/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
66+
#[derive(Clone, Debug)]
67+
crate struct TraitWithExtraInfo {
68+
crate trait_: Trait,
69+
crate is_spotlight: bool,
70+
}
71+
6572
#[derive(Clone, Debug)]
6673
crate struct ExternalCrate {
6774
crate name: Symbol,
@@ -1185,7 +1192,6 @@ crate struct Trait {
11851192
crate items: Vec<Item>,
11861193
crate generics: Generics,
11871194
crate bounds: Vec<GenericBound>,
1188-
crate is_spotlight: bool,
11891195
crate is_auto: bool,
11901196
}
11911197

src/librustdoc/clean/utils.rs

+16
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,19 @@ crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<De
520520
None
521521
}
522522
}
523+
524+
/// Checks for the existence of `hidden` in the attribute below if `flag` is `sym::hidden`:
525+
///
526+
/// ```
527+
/// #[doc(hidden)]
528+
/// pub fn foo() {}
529+
/// ```
530+
///
531+
/// This function exists because it runs on `hir::Attributes` whereas the other is a
532+
/// `clean::Attributes` method.
533+
crate fn has_doc_flag(attrs: ty::Attributes<'_>, flag: Symbol) -> bool {
534+
attrs.iter().any(|attr| {
535+
attr.has_name(sym::doc)
536+
&& attr.meta_item_list().map_or(false, |l| rustc_attr::list_contains_name(&l, flag))
537+
})
538+
}

src/librustdoc/core.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::{cell::RefCell, collections::hash_map::Entry};
3030

3131
use crate::clean;
3232
use crate::clean::inline::build_external_trait;
33-
use crate::clean::{AttributesExt, MAX_DEF_IDX};
33+
use crate::clean::{AttributesExt, TraitWithExtraInfo, MAX_DEF_IDX};
3434
use crate::config::{Options as RustdocOptions, RenderOptions};
3535
use crate::config::{OutputFormat, RenderInfo};
3636
use crate::formats::cache::Cache;
@@ -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::TraitWithExtraInfo>>>,
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>,
@@ -538,7 +538,10 @@ crate fn run_global_ctxt(
538538
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
539539
let mut sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
540540
sized_trait.is_auto = true;
541-
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
541+
ctxt.external_traits.borrow_mut().insert(
542+
sized_trait_did,
543+
TraitWithExtraInfo { trait_: sized_trait, is_spotlight: false },
544+
);
542545
}
543546

544547
debug!("crate: {:?}", tcx.hir().krate());

src/librustdoc/fold.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ crate trait DocFolder: Sized {
9292
{
9393
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
9494
for (k, mut v) in external_traits {
95-
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
95+
v.trait_.items =
96+
v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
9697
c.external_traits.borrow_mut().insert(k, v);
9798
}
9899
}

src/librustdoc/formats/cache.rs

+6-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,7 @@ 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+
crate traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
6869

6970
/// When rendering traits, it's often useful to be able to list all
7071
/// implementors of the trait, and this mapping is exactly, that: a mapping
@@ -247,7 +248,10 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
247248
// Propagate a trait method's documentation to all implementors of the
248249
// trait.
249250
if let clean::TraitItem(ref t) = *item.kind {
250-
self.cache.traits.entry(item.def_id).or_insert_with(|| t.clone());
251+
self.cache.traits.entry(item.def_id).or_insert_with(|| clean::TraitWithExtraInfo {
252+
trait_: t.clone(),
253+
is_spotlight: item.attrs.has_doc_flag(sym::spotlight),
254+
});
251255
}
252256

253257
// 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(|t| t.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.trait_),
39833984
show_def_docs,
39843985
);
39853986
}
@@ -4028,7 +4029,7 @@ fn render_impl(
40284029
render_default_items(
40294030
w,
40304031
cx,
4031-
t,
4032+
&t.trait_,
40324033
&i.inner_impl(),
40334034
&i.impl_item,
40344035
render_mode,

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
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl JsonRenderer<'tcx> {
8787
.filter_map(|(&id, trait_item)| {
8888
// only need to synthesize items for external traits
8989
if !id.is_local() {
90+
let trait_item = &trait_item.trait_;
9091
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
9192
Some((
9293
from_def_id(id),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![crate_name = "foo"]
2+
3+
use std::iter::Iterator;
4+
5+
// @has foo/struct.Odd.html
6+
// @has - '//h4[@id="method.new"]//span[@class="notable-traits"]//code/span' 'impl Iterator for Odd'
7+
pub struct Odd {
8+
current: usize,
9+
}
10+
11+
impl Odd {
12+
pub fn new() -> Odd {
13+
Odd { current: 1 }
14+
}
15+
}
16+
17+
impl Iterator for Odd {
18+
type Item = usize;
19+
20+
fn next(&mut self) -> Option<Self::Item> {
21+
self.current += 2;
22+
Some(self.current - 2)
23+
}
24+
}

0 commit comments

Comments
 (0)