1
Fork 0

Auto merge of #102384 - camelid:extrainfo, r=GuillaumeGomez

rustdoc: Remove `clean::TraitWithExtraInfo` and queryify `is_notable_trait`

cc `@notriddle` `@GuillaumeGomez`
This commit is contained in:
bors 2022-09-28 22:15:28 +00:00
commit b30c88623c
10 changed files with 39 additions and 33 deletions

View file

@ -1126,6 +1126,11 @@ rustc_queries! {
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) } desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
} }
/// Determines whether an item is annotated with `doc(notable_trait)`.
query is_doc_notable_trait(def_id: DefId) -> bool {
desc { |tcx| "checking whether `{}` is `doc(notable_trait)`", tcx.def_path_str(def_id) }
}
/// Returns the attributes on the item at `def_id`. /// Returns the attributes on the item at `def_id`.
/// ///
/// Do not use this directly, use `tcx.get_attrs` instead. /// Do not use this directly, use `tcx.get_attrs` instead.

View file

@ -1289,12 +1289,24 @@ pub fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
.any(|items| items.iter().any(|item| item.has_name(sym::hidden))) .any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
} }
/// Determines whether an item is annotated with `doc(notable_trait)`.
pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
tcx.get_attrs(def_id, sym::doc)
.filter_map(|attr| attr.meta_item_list())
.any(|items| items.iter().any(|item| item.has_name(sym::notable_trait)))
}
/// Determines whether an item is an intrinsic by Abi. /// Determines whether an item is an intrinsic by Abi.
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool { pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
matches!(tcx.fn_sig(def_id).abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic) matches!(tcx.fn_sig(def_id).abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
} }
pub fn provide(providers: &mut ty::query::Providers) { pub fn provide(providers: &mut ty::query::Providers) {
*providers = *providers = ty::query::Providers {
ty::query::Providers { normalize_opaque_types, is_doc_hidden, is_intrinsic, ..*providers } normalize_opaque_types,
is_doc_hidden,
is_doc_notable_trait,
is_intrinsic,
..*providers
}
} }

View file

@ -718,10 +718,6 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
debug!("record_extern_trait: {:?}", did); debug!("record_extern_trait: {:?}", did);
let trait_ = build_external_trait(cx, did); let trait_ = build_external_trait(cx, did);
let trait_ = clean::TraitWithExtraInfo {
trait_,
is_notable: clean::utils::has_doc_flag(cx.tcx, did, sym::notable_trait),
};
cx.external_traits.borrow_mut().insert(did, trait_); cx.external_traits.borrow_mut().insert(did, trait_);
cx.active_extern_traits.remove(&did); cx.active_extern_traits.remove(&did);
} }

View file

@ -119,7 +119,7 @@ pub(crate) struct Crate {
pub(crate) module: Item, pub(crate) module: Item,
pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>, pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>,
/// Only here so that they can be filtered through the rustdoc passes. /// Only here so that they can be filtered through the rustdoc passes.
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>, pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
} }
impl Crate { impl Crate {
@ -132,13 +132,6 @@ impl Crate {
} }
} }
/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
#[derive(Clone, Debug)]
pub(crate) struct TraitWithExtraInfo {
pub(crate) trait_: Trait,
pub(crate) is_notable: bool,
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub(crate) struct ExternalCrate { pub(crate) struct ExternalCrate {
pub(crate) crate_num: CrateNum, pub(crate) crate_num: CrateNum,
@ -1530,6 +1523,9 @@ impl Trait {
pub(crate) fn is_auto(&self, tcx: TyCtxt<'_>) -> bool { pub(crate) fn is_auto(&self, tcx: TyCtxt<'_>) -> bool {
tcx.trait_is_auto(self.def_id) tcx.trait_is_auto(self.def_id)
} }
pub(crate) fn is_notable_trait(&self, tcx: TyCtxt<'_>) -> bool {
tcx.is_doc_notable_trait(self.def_id)
}
pub(crate) fn unsafety(&self, tcx: TyCtxt<'_>) -> hir::Unsafety { pub(crate) fn unsafety(&self, tcx: TyCtxt<'_>) -> hir::Unsafety {
tcx.trait_def(self.def_id).unsafety tcx.trait_def(self.def_id).unsafety
} }

View file

@ -25,7 +25,7 @@ use std::rc::Rc;
use std::sync::LazyLock; use std::sync::LazyLock;
use crate::clean::inline::build_external_trait; use crate::clean::inline::build_external_trait;
use crate::clean::{self, ItemId, TraitWithExtraInfo}; use crate::clean::{self, ItemId};
use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions}; use crate::config::{Options as RustdocOptions, OutputFormat, RenderOptions};
use crate::formats::cache::Cache; use crate::formats::cache::Cache;
use crate::passes::collect_intra_doc_links::PreprocessedMarkdownLink; use crate::passes::collect_intra_doc_links::PreprocessedMarkdownLink;
@ -58,7 +58,7 @@ pub(crate) struct DocContext<'tcx> {
/// Most of this logic is copied from rustc_lint::late. /// Most of this logic is copied from rustc_lint::late.
pub(crate) param_env: ParamEnv<'tcx>, pub(crate) param_env: ParamEnv<'tcx>,
/// Later on moved through `clean::Crate` into `cache` /// Later on moved through `clean::Crate` into `cache`
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>, pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at /// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time. /// the same time.
pub(crate) active_extern_traits: FxHashSet<DefId>, pub(crate) active_extern_traits: FxHashSet<DefId>,
@ -388,9 +388,7 @@ pub(crate) fn run_global_ctxt(
// Note that in case of `#![no_core]`, the trait is not available. // Note that in case of `#![no_core]`, the trait is not available.
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() { if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did); let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
ctxt.external_traits ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
.borrow_mut()
.insert(sized_trait_did, TraitWithExtraInfo { trait_: sized_trait, is_notable: false });
} }
debug!("crate: {:?}", tcx.hir().krate()); debug!("crate: {:?}", tcx.hir().krate());

View file

@ -94,7 +94,7 @@ pub(crate) trait DocFolder: Sized {
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) }; let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, mut v) in external_traits { for (k, mut v) in external_traits {
v.trait_.items = v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect(); 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);
} }

View file

@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{CrateNum, DefId}; use rustc_hir::def_id::{CrateNum, DefId};
use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::middle::privacy::AccessLevels;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_span::{sym, Symbol}; use rustc_span::Symbol;
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType}; use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
use crate::core::DocContext; use crate::core::DocContext;
@ -62,7 +62,7 @@ pub(crate) struct Cache {
/// Implementations of a crate should inherit the documentation of the /// Implementations of a crate should inherit the documentation of the
/// parent trait if no extra documentation is specified, and default methods /// parent trait if no extra documentation is specified, and default methods
/// should show up in documentation about trait implementations. /// should show up in documentation about trait implementations.
pub(crate) traits: FxHashMap<DefId, clean::TraitWithExtraInfo>, pub(crate) traits: FxHashMap<DefId, clean::Trait>,
/// When rendering traits, it's often useful to be able to list all /// 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 /// implementors of the trait, and this mapping is exactly, that: a mapping
@ -225,12 +225,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
// Propagate a trait method's documentation to all implementors of the // Propagate a trait method's documentation to all implementors of the
// trait. // trait.
if let clean::TraitItem(ref t) = *item.kind { if let clean::TraitItem(ref t) = *item.kind {
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| { self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
clean::TraitWithExtraInfo {
trait_: *t.clone(),
is_notable: item.attrs.has_doc_flag(sym::notable_trait),
}
});
} }
// Collect all the implementors of traits. // Collect all the implementors of traits.

View file

@ -1294,7 +1294,12 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
if let Some(trait_) = &impl_.trait_ { if let Some(trait_) = &impl_.trait_ {
let trait_did = trait_.def_id(); let trait_did = trait_.def_id();
if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable) { if cx
.cache()
.traits
.get(&trait_did)
.map_or(false, |t| t.is_notable_trait(cx.tcx()))
{
if out.is_empty() { if out.is_empty() {
write!( write!(
&mut out, &mut out,
@ -1598,7 +1603,7 @@ fn render_impl(
link, link,
render_mode, render_mode,
false, false,
trait_.map(|t| &t.trait_), trait_,
rendering_params, rendering_params,
); );
} }
@ -1658,7 +1663,7 @@ fn render_impl(
&mut default_impl_items, &mut default_impl_items,
&mut impl_items, &mut impl_items,
cx, cx,
&t.trait_, t,
i.inner_impl(), i.inner_impl(),
&i.impl_item, &i.impl_item,
parent, parent,

View file

@ -108,7 +108,6 @@ impl<'tcx> JsonRenderer<'tcx> {
.filter_map(|(&id, trait_item)| { .filter_map(|(&id, trait_item)| {
// only need to synthesize items for external traits // only need to synthesize items for external traits
if !id.is_local() { if !id.is_local() {
let trait_item = &trait_item.trait_;
for item in &trait_item.items { for item in &trait_item.items {
trace!("Adding subitem to {id:?}: {:?}", item.item_id); trace!("Adding subitem to {id:?}: {:?}", item.item_id);
self.item(item.clone()).unwrap(); self.item(item.clone()).unwrap();

View file

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