Skip to content

Commit

Permalink
Auto merge of #102384 - camelid:extrainfo, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
rustdoc: Remove `clean::TraitWithExtraInfo` and queryify `is_notable_trait`

cc `@notriddle` `@GuillaumeGomez`
  • Loading branch information
bors committed Sep 28, 2022
2 parents ce7f0f1 + 4bf789f commit b30c886
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 33 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,11 @@ rustc_queries! {
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`.
///
/// Do not use this directly, use `tcx.get_attrs` instead.
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

/// 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.
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
matches!(tcx.fn_sig(def_id).abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
}

pub fn provide(providers: &mut ty::query::Providers) {
*providers =
ty::query::Providers { normalize_opaque_types, is_doc_hidden, is_intrinsic, ..*providers }
*providers = ty::query::Providers {
normalize_opaque_types,
is_doc_hidden,
is_doc_notable_trait,
is_intrinsic,
..*providers
}
}
4 changes: 0 additions & 4 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,6 @@ pub(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_notable: clean::utils::has_doc_flag(cx.tcx, did, sym::notable_trait),
};
cx.external_traits.borrow_mut().insert(did, trait_);
cx.active_extern_traits.remove(&did);
}
12 changes: 4 additions & 8 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub(crate) struct Crate {
pub(crate) module: Item,
pub(crate) primitives: ThinVec<(DefId, PrimitiveType)>,
/// 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 {
Expand All @@ -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)]
pub(crate) struct ExternalCrate {
pub(crate) crate_num: CrateNum,
Expand Down Expand Up @@ -1530,6 +1523,9 @@ impl Trait {
pub(crate) fn is_auto(&self, tcx: TyCtxt<'_>) -> bool {
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 {
tcx.trait_def(self.def_id).unsafety
}
Expand Down
8 changes: 3 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::rc::Rc;
use std::sync::LazyLock;

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::formats::cache::Cache;
use crate::passes::collect_intra_doc_links::PreprocessedMarkdownLink;
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) struct DocContext<'tcx> {
/// Most of this logic is copied from rustc_lint::late.
pub(crate) param_env: ParamEnv<'tcx>,
/// 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
/// the same time.
pub(crate) active_extern_traits: FxHashSet<DefId>,
Expand Down Expand Up @@ -388,9 +388,7 @@ pub(crate) fn run_global_ctxt(
// Note that in case of `#![no_core]`, the trait is not available.
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
ctxt.external_traits
.borrow_mut()
.insert(sized_trait_did, TraitWithExtraInfo { trait_: sized_trait, is_notable: false });
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
}

debug!("crate: {:?}", tcx.hir().krate());
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub(crate) trait DocFolder: Sized {

let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
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);
}

Expand Down
11 changes: 3 additions & 8 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_middle::middle::privacy::AccessLevels;
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::core::DocContext;
Expand Down Expand Up @@ -62,7 +62,7 @@ pub(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.
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
/// implementors of the trait, and this mapping is exactly, that: a mapping
Expand Down Expand Up @@ -225,12 +225,7 @@ 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.item_id.expect_def_id()).or_insert_with(|| {
clean::TraitWithExtraInfo {
trait_: *t.clone(),
is_notable: item.attrs.has_doc_flag(sym::notable_trait),
}
});
self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
}

// Collect all the implementors of traits.
Expand Down
11 changes: 8 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,12 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
if let Some(trait_) = &impl_.trait_ {
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() {
write!(
&mut out,
Expand Down Expand Up @@ -1598,7 +1603,7 @@ fn render_impl(
link,
render_mode,
false,
trait_.map(|t| &t.trait_),
trait_,
rendering_params,
);
}
Expand Down Expand Up @@ -1658,7 +1663,7 @@ fn render_impl(
&mut default_impl_items,
&mut impl_items,
cx,
&t.trait_,
t,
i.inner_impl(),
&i.impl_item,
parent,
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl<'tcx> 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_;
for item in &trait_item.items {
trace!("Adding subitem to {id:?}: {:?}", item.item_id);
self.item(item.clone()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) trait DocVisitor: Sized {
// 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()) };
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);
}
}
Expand Down

0 comments on commit b30c886

Please sign in to comment.