From b5c8eea55d160f524305d09ad45c0d79493537b4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 12 Feb 2021 14:33:32 +0100 Subject: [PATCH] Put clean::Trait extra information into a new struct to make it more coherent --- src/librustdoc/clean/inline.rs | 10 +++++++--- src/librustdoc/clean/types.rs | 9 ++++++++- src/librustdoc/core.rs | 2 +- src/librustdoc/fold.rs | 7 ++++--- src/librustdoc/formats/cache.rs | 12 +++++++----- src/librustdoc/html/render/mod.rs | 8 ++++---- src/librustdoc/json/mod.rs | 3 ++- 7 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 519ec7216e3..51cdcd74147 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -624,8 +624,12 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) { debug!("record_extern_trait: {:?}", did); let trait_ = build_external_trait(cx, did); - cx.external_traits - .borrow_mut() - .insert(did, (trait_, clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight))); + cx.external_traits.borrow_mut().insert( + did, + clean::TraitWithExtraInfo { + trait_, + is_spotlight: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::spotlight), + }, + ); cx.active_extern_traits.remove(&did); } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 0289ee9afb7..51bef344e67 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -57,11 +57,18 @@ crate struct Crate { crate primitives: Vec<(DefId, PrimitiveType)>, // These are later on moved into `CACHEKEY`, leaving the map empty. // Only here so that they can be filtered through the rustdoc passes. - crate external_traits: Rc>>, + crate external_traits: Rc>>, crate masked_crates: FxHashSet, crate collapsed: bool, } +/// This struct is used to wrap additional information added by rustdoc on a `trait` item. +#[derive(Clone, Debug)] +crate struct TraitWithExtraInfo { + crate trait_: Trait, + crate is_spotlight: bool, +} + #[derive(Clone, Debug)] crate struct ExternalCrate { crate name: Symbol, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 5f24ec7b3f7..3883652375f 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -55,7 +55,7 @@ crate struct DocContext<'tcx> { /// Later on moved into `cache` crate renderinfo: RenderInfo, /// Later on moved through `clean::Crate` into `cache` - crate external_traits: Rc>>, + crate external_traits: Rc>>, /// Used while populating `external_traits` to ensure we don't process the same trait twice at /// the same time. crate active_extern_traits: FxHashSet, diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index 752233ade37..2b980ebe592 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -91,9 +91,10 @@ crate trait DocFolder: Sized { { let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) }; - for (k, (mut v, is_spotlight)) in external_traits { - v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect(); - c.external_traits.borrow_mut().insert(k, (v, is_spotlight)); + for (k, mut v) in external_traits { + v.trait_.items = + v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect(); + c.external_traits.borrow_mut().insert(k, v); } } c diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index a64eeb5bc58..ef4e0e0d57c 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -65,9 +65,7 @@ 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. - /// - /// The `bool` parameter is about if the trait is `spotlight`. - crate traits: FxHashMap, + crate traits: FxHashMap, /// 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 @@ -251,8 +249,12 @@ 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.def_id).or_insert_with(|| { - (t.clone(), clean::utils::has_doc_flag(tcx.get_attrs(item.def_id), sym::spotlight)) + self.cache.traits.entry(item.def_id).or_insert_with(|| clean::TraitWithExtraInfo { + trait_: t.clone(), + is_spotlight: clean::utils::has_doc_flag( + tcx.get_attrs(item.def_id), + sym::spotlight, + ), }); } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index ab3c9bab64f..b21f6a13392 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -3688,7 +3688,7 @@ fn spotlight_decl(decl: &clean::FnDecl, cache: &Cache) -> String { for i in impls { let impl_ = i.inner_impl(); if impl_.trait_.def_id().map_or(false, |d| { - cache.traits.get(&d).map(|(_, is_spotlight)| *is_spotlight).unwrap_or(false) + cache.traits.get(&d).map(|t| t.is_spotlight).unwrap_or(false) }) { if out.is_empty() { write!( @@ -3980,7 +3980,7 @@ fn render_impl( false, outer_version, outer_const_version, - trait_.map(|(t, _)| t), + trait_.map(|t| &t.trait_), show_def_docs, ); } @@ -4025,11 +4025,11 @@ fn render_impl( // We don't emit documentation for default items if they appear in the // Implementations on Foreign Types or Implementors sections. if show_default_items { - if let Some((t, _)) = trait_ { + if let Some(t) = trait_ { render_default_items( w, cx, - t, + &t.trait_, &i.inner_impl(), &i.impl_item, render_mode, diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index aa093ba0a77..a3b21467794 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -84,9 +84,10 @@ impl JsonRenderer<'tcx> { Rc::clone(&self.cache) .traits .iter() - .filter_map(|(&id, (trait_item, _))| { + .filter_map(|(&id, trait_item)| { // only need to synthesize items for external traits if !id.is_local() { + let trait_item = &trait_item.trait_; trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap()); Some(( from_def_id(id),