Skip to content

Commit b30c886

Browse files
committed
Auto merge of #102384 - camelid:extrainfo, r=GuillaumeGomez
rustdoc: Remove `clean::TraitWithExtraInfo` and queryify `is_notable_trait` cc `@notriddle` `@GuillaumeGomez`
2 parents ce7f0f1 + 4bf789f commit b30c886

File tree

10 files changed

+39
-33
lines changed

10 files changed

+39
-33
lines changed

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,11 @@ rustc_queries! {
11261126
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
11271127
}
11281128

1129+
/// Determines whether an item is annotated with `doc(notable_trait)`.
1130+
query is_doc_notable_trait(def_id: DefId) -> bool {
1131+
desc { |tcx| "checking whether `{}` is `doc(notable_trait)`", tcx.def_path_str(def_id) }
1132+
}
1133+
11291134
/// Returns the attributes on the item at `def_id`.
11301135
///
11311136
/// Do not use this directly, use `tcx.get_attrs` instead.

compiler/rustc_middle/src/ty/util.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1289,12 +1289,24 @@ pub fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
12891289
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
12901290
}
12911291

1292+
/// Determines whether an item is annotated with `doc(notable_trait)`.
1293+
pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1294+
tcx.get_attrs(def_id, sym::doc)
1295+
.filter_map(|attr| attr.meta_item_list())
1296+
.any(|items| items.iter().any(|item| item.has_name(sym::notable_trait)))
1297+
}
1298+
12921299
/// Determines whether an item is an intrinsic by Abi.
12931300
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
12941301
matches!(tcx.fn_sig(def_id).abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
12951302
}
12961303

12971304
pub fn provide(providers: &mut ty::query::Providers) {
1298-
*providers =
1299-
ty::query::Providers { normalize_opaque_types, is_doc_hidden, is_intrinsic, ..*providers }
1305+
*providers = ty::query::Providers {
1306+
normalize_opaque_types,
1307+
is_doc_hidden,
1308+
is_doc_notable_trait,
1309+
is_intrinsic,
1310+
..*providers
1311+
}
13001312
}

src/librustdoc/clean/inline.rs

-4
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,6 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
718718
debug!("record_extern_trait: {:?}", did);
719719
let trait_ = build_external_trait(cx, did);
720720

721-
let trait_ = clean::TraitWithExtraInfo {
722-
trait_,
723-
is_notable: clean::utils::has_doc_flag(cx.tcx, did, sym::notable_trait),
724-
};
725721
cx.external_traits.borrow_mut().insert(did, trait_);
726722
cx.active_extern_traits.remove(&did);
727723
}

src/librustdoc/clean/types.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub(crate) struct Crate {
119119
pub(crate) module: Item,
120120
pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>,
121121
/// Only here so that they can be filtered through the rustdoc passes.
122-
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
122+
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
123123
}
124124

125125
impl Crate {
@@ -132,13 +132,6 @@ impl Crate {
132132
}
133133
}
134134

135-
/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
136-
#[derive(Clone, Debug)]
137-
pub(crate) struct TraitWithExtraInfo {
138-
pub(crate) trait_: Trait,
139-
pub(crate) is_notable: bool,
140-
}
141-
142135
#[derive(Copy, Clone, Debug)]
143136
pub(crate) struct ExternalCrate {
144137
pub(crate) crate_num: CrateNum,
@@ -1530,6 +1523,9 @@ impl Trait {
15301523
pub(crate) fn is_auto(&self, tcx: TyCtxt<'_>) -> bool {
15311524
tcx.trait_is_auto(self.def_id)
15321525
}
1526+
pub(crate) fn is_notable_trait(&self, tcx: TyCtxt<'_>) -> bool {
1527+
tcx.is_doc_notable_trait(self.def_id)
1528+
}
15331529
pub(crate) fn unsafety(&self, tcx: TyCtxt<'_>) -> hir::Unsafety {
15341530
tcx.trait_def(self.def_id).unsafety
15351531
}

src/librustdoc/core.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::rc::Rc;
2525
use std::sync::LazyLock;
2626

2727
use crate::clean::inline::build_external_trait;
28-
use crate::clean::{self, ItemId, TraitWithExtraInfo};
28+
use crate::clean::{self, ItemId};
2929
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
3030
use crate::formats::cache::Cache;
3131
use crate::passes::collect_intra_doc_links::PreprocessedMarkdownLink;
@@ -58,7 +58,7 @@ pub(crate) struct DocContext<'tcx> {
5858
/// Most of this logic is copied from rustc_lint::late.
5959
pub(crate) param_env: ParamEnv<'tcx>,
6060
/// Later on moved through `clean::Crate` into `cache`
61-
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
61+
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
6262
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
6363
/// the same time.
6464
pub(crate) active_extern_traits: FxHashSet<DefId>,
@@ -388,9 +388,7 @@ pub(crate) fn run_global_ctxt(
388388
// Note that in case of `#![no_core]`, the trait is not available.
389389
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
390390
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
391-
ctxt.external_traits
392-
.borrow_mut()
393-
.insert(sized_trait_did, TraitWithExtraInfo { trait_: sized_trait, is_notable: false });
391+
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
394392
}
395393

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

src/librustdoc/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(crate) trait DocFolder: Sized {
9494

9595
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
9696
for (k, mut v) in external_traits {
97-
v.trait_.items = v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
97+
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
9898
c.external_traits.borrow_mut().insert(k, v);
9999
}
100100

src/librustdoc/formats/cache.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_hir::def_id::{CrateNum, DefId};
55
use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::{self, TyCtxt};
7-
use rustc_span::{sym, Symbol};
7+
use rustc_span::Symbol;
88

99
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
1010
use crate::core::DocContext;
@@ -62,7 +62,7 @@ pub(crate) struct Cache {
6262
/// Implementations of a crate should inherit the documentation of the
6363
/// parent trait if no extra documentation is specified, and default methods
6464
/// should show up in documentation about trait implementations.
65-
pub(crate) traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
65+
pub(crate) traits: FxHashMap<DefId, clean::Trait>,
6666

6767
/// When rendering traits, it's often useful to be able to list all
6868
/// implementors of the trait, and this mapping is exactly, that: a mapping
@@ -225,12 +225,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
225225
// Propagate a trait method's documentation to all implementors of the
226226
// trait.
227227
if let clean::TraitItem(ref t) = *item.kind {
228-
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| {
229-
clean::TraitWithExtraInfo {
230-
trait_: *t.clone(),
231-
is_notable: item.attrs.has_doc_flag(sym::notable_trait),
232-
}
233-
});
228+
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
234229
}
235230

236231
// Collect all the implementors of traits.

src/librustdoc/html/render/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,12 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12941294
if let Some(trait_) = &impl_.trait_ {
12951295
let trait_did = trait_.def_id();
12961296

1297-
if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable) {
1297+
if cx
1298+
.cache()
1299+
.traits
1300+
.get(&trait_did)
1301+
.map_or(false, |t| t.is_notable_trait(cx.tcx()))
1302+
{
12981303
if out.is_empty() {
12991304
write!(
13001305
&mut out,
@@ -1598,7 +1603,7 @@ fn render_impl(
15981603
link,
15991604
render_mode,
16001605
false,
1601-
trait_.map(|t| &t.trait_),
1606+
trait_,
16021607
rendering_params,
16031608
);
16041609
}
@@ -1658,7 +1663,7 @@ fn render_impl(
16581663
&mut default_impl_items,
16591664
&mut impl_items,
16601665
cx,
1661-
&t.trait_,
1666+
t,
16621667
i.inner_impl(),
16631668
&i.impl_item,
16641669
parent,

src/librustdoc/json/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ impl<'tcx> JsonRenderer<'tcx> {
108108
.filter_map(|(&id, trait_item)| {
109109
// only need to synthesize items for external traits
110110
if !id.is_local() {
111-
let trait_item = &trait_item.trait_;
112111
for item in &trait_item.items {
113112
trace!("Adding subitem to {id:?}: {:?}", item.item_id);
114113
self.item(item.clone()).unwrap();

src/librustdoc/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) trait DocVisitor: Sized {
6565
// FIXME: make this a simple by-ref for loop once external_traits is cleaned up
6666
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
6767
for (k, v) in external_traits {
68-
v.trait_.items.iter().for_each(|i| self.visit_item(i));
68+
v.items.iter().for_each(|i| self.visit_item(i));
6969
c.external_traits.borrow_mut().insert(k, v);
7070
}
7171
}

0 commit comments

Comments
 (0)