From bd7caf477c8e77f0db286c3fd59d7708b3176fdc Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 22 Jun 2021 04:15:13 +0800 Subject: [PATCH 1/3] Do not list impl when trait has doc(hidden) --- src/librustdoc/clean/inline.rs | 19 ++++++++++++++- .../rustdoc/auxiliary/cross-crate-hidden.rs | 2 ++ src/test/rustdoc/cross-crate-hidden.rs | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc/auxiliary/cross-crate-hidden.rs create mode 100644 src/test/rustdoc/cross-crate-hidden.rs diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 111827aacdff8..db2e0d0d241e8 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -1,5 +1,6 @@ //! Support for inlining external documentation into the current AST. +use std::collections::VecDeque; use std::iter::once; use std::sync::Arc; @@ -15,7 +16,9 @@ use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; -use crate::clean::{self, Attributes, AttributesExt, FakeDefId, GetDefId, ToSource}; +use crate::clean::{ + self, Attributes, AttributesExt, FakeDefId, GetDefId, NestedAttributesExt, ToSource, Type, +}; use crate::core::DocContext; use crate::formats::item_type::ItemType; @@ -420,6 +423,20 @@ crate fn build_impl( if trait_.def_id() == tcx.lang_items().deref_trait() { super::build_deref_target_impls(cx, &trait_items, ret); } + + // Return if the trait itself or any types of the generic parameters are doc(hidden). + let mut deque: VecDeque<&Type> = trait_.iter().collect(); + while let Some(ty) = deque.pop_back() { + if let Some(did) = ty.def_id() { + if cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) { + return; + } + } + if let Some(generics) = ty.generics() { + deque.extend(generics); + } + } + if let Some(trait_did) = trait_.def_id() { record_extern_trait(cx, trait_did); } diff --git a/src/test/rustdoc/auxiliary/cross-crate-hidden.rs b/src/test/rustdoc/auxiliary/cross-crate-hidden.rs new file mode 100644 index 0000000000000..ec0ced2a82d00 --- /dev/null +++ b/src/test/rustdoc/auxiliary/cross-crate-hidden.rs @@ -0,0 +1,2 @@ +#[doc(hidden)] +pub enum HiddenType {} diff --git a/src/test/rustdoc/cross-crate-hidden.rs b/src/test/rustdoc/cross-crate-hidden.rs new file mode 100644 index 0000000000000..b1bfc7c49bff3 --- /dev/null +++ b/src/test/rustdoc/cross-crate-hidden.rs @@ -0,0 +1,23 @@ +// Issue #86448: test for cross-crate `doc(hidden)` +#![crate_name = "foo"] + +// aux-build:cross-crate-hidden.rs +extern crate cross_crate_hidden; + +pub use ::cross_crate_hidden::HiddenType; // OK, not re-exported + +pub enum MyLibType {} + +// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3CHiddenType%3E"]' 'impl From for MyLibType' +impl From for MyLibType { + fn from(it: HiddenType) -> MyLibType { + match it {} + } +} + +// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3COption%3COption%3COption%3COption%3CHiddenType%3E%3E%3E%3E%3E"]' 'impl From>>>> for MyLibType' +impl From>>>> for MyLibType { + fn from(it: Option>>>) -> MyLibType { + todo!() + } +} From 0aaefff0096b04d46b9502508ec4c935ed10ae25 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 22 Jun 2021 11:16:59 +0800 Subject: [PATCH 2/3] Account for more cases --- src/librustdoc/clean/inline.rs | 8 ++++---- .../rustdoc/auxiliary/cross-crate-hidden.rs | 3 +++ src/test/rustdoc/cross-crate-hidden.rs | 20 +++++++++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index db2e0d0d241e8..ab6254dcf10b5 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -1,6 +1,5 @@ //! Support for inlining external documentation into the current AST. -use std::collections::VecDeque; use std::iter::once; use std::sync::Arc; @@ -425,15 +424,16 @@ crate fn build_impl( } // Return if the trait itself or any types of the generic parameters are doc(hidden). - let mut deque: VecDeque<&Type> = trait_.iter().collect(); - while let Some(ty) = deque.pop_back() { + let mut stack: Vec<&Type> = trait_.iter().collect(); + stack.push(&for_); + while let Some(ty) = stack.pop() { if let Some(did) = ty.def_id() { if cx.tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) { return; } } if let Some(generics) = ty.generics() { - deque.extend(generics); + stack.extend(generics); } } diff --git a/src/test/rustdoc/auxiliary/cross-crate-hidden.rs b/src/test/rustdoc/auxiliary/cross-crate-hidden.rs index ec0ced2a82d00..15953122280f3 100644 --- a/src/test/rustdoc/auxiliary/cross-crate-hidden.rs +++ b/src/test/rustdoc/auxiliary/cross-crate-hidden.rs @@ -1,2 +1,5 @@ #[doc(hidden)] pub enum HiddenType {} + +#[doc(hidden)] +pub trait HiddenTrait {} diff --git a/src/test/rustdoc/cross-crate-hidden.rs b/src/test/rustdoc/cross-crate-hidden.rs index b1bfc7c49bff3..e3e7506d9d374 100644 --- a/src/test/rustdoc/cross-crate-hidden.rs +++ b/src/test/rustdoc/cross-crate-hidden.rs @@ -4,7 +4,7 @@ // aux-build:cross-crate-hidden.rs extern crate cross_crate_hidden; -pub use ::cross_crate_hidden::HiddenType; // OK, not re-exported +pub use ::cross_crate_hidden::{HiddenType, HiddenTrait}; // OK, not re-exported pub enum MyLibType {} @@ -15,9 +15,21 @@ impl From for MyLibType { } } -// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3COption%3COption%3COption%3COption%3CHiddenType%3E%3E%3E%3E%3E"]' 'impl From>>>> for MyLibType' -impl From>>>> for MyLibType { - fn from(it: Option>>>) -> MyLibType { +pub struct T(T); + +// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3CT%3CT%3CT%3CT%3CHiddenType%3E%3E%3E%3E%3E"]' 'impl From>>>> for MyLibType' +impl From>>>> for MyLibType { + fn from(it: T>>>) -> MyLibType { todo!() } } + +// @!has foo/enum.MyLibType.html '//*[@id="impl-HiddenTrait"]' 'impl HiddenTrait for MyLibType' +impl HiddenTrait for MyLibType {} + +// @!has foo/struct.T.html '//*[@id="impl-From%3CMyLibType%3E"]' 'impl From for T>>>' +impl From for T>>> { + fn from(it: MyLibType) -> T>>> { + match it {} + } +} From 9a6343478ce9765c299ad594dd8a8a8d38202c68 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 25 Jun 2021 14:08:06 +0800 Subject: [PATCH 3/3] Renamed test and added test for same crate --- ...s => cross-crate-hidden-impl-parameter.rs} | 0 ...s => cross-crate-hidden-impl-parameter.rs} | 6 ++-- .../same-crate-hidden-impl-parameter.rs | 36 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) rename src/test/rustdoc/auxiliary/{cross-crate-hidden.rs => cross-crate-hidden-impl-parameter.rs} (100%) rename src/test/rustdoc/{cross-crate-hidden.rs => cross-crate-hidden-impl-parameter.rs} (83%) create mode 100644 src/test/rustdoc/same-crate-hidden-impl-parameter.rs diff --git a/src/test/rustdoc/auxiliary/cross-crate-hidden.rs b/src/test/rustdoc/auxiliary/cross-crate-hidden-impl-parameter.rs similarity index 100% rename from src/test/rustdoc/auxiliary/cross-crate-hidden.rs rename to src/test/rustdoc/auxiliary/cross-crate-hidden-impl-parameter.rs diff --git a/src/test/rustdoc/cross-crate-hidden.rs b/src/test/rustdoc/cross-crate-hidden-impl-parameter.rs similarity index 83% rename from src/test/rustdoc/cross-crate-hidden.rs rename to src/test/rustdoc/cross-crate-hidden-impl-parameter.rs index e3e7506d9d374..eb2ced2f7f413 100644 --- a/src/test/rustdoc/cross-crate-hidden.rs +++ b/src/test/rustdoc/cross-crate-hidden-impl-parameter.rs @@ -1,10 +1,10 @@ // Issue #86448: test for cross-crate `doc(hidden)` #![crate_name = "foo"] -// aux-build:cross-crate-hidden.rs -extern crate cross_crate_hidden; +// aux-build:cross-crate-hidden-impl-parameter.rs +extern crate cross_crate_hidden_impl_parameter; -pub use ::cross_crate_hidden::{HiddenType, HiddenTrait}; // OK, not re-exported +pub use ::cross_crate_hidden_impl_parameter::{HiddenType, HiddenTrait}; // OK, not re-exported pub enum MyLibType {} diff --git a/src/test/rustdoc/same-crate-hidden-impl-parameter.rs b/src/test/rustdoc/same-crate-hidden-impl-parameter.rs new file mode 100644 index 0000000000000..d55393af8599b --- /dev/null +++ b/src/test/rustdoc/same-crate-hidden-impl-parameter.rs @@ -0,0 +1,36 @@ +// test for `doc(hidden)` with impl parameters in the same crate. +#![crate_name = "foo"] + +#[doc(hidden)] +pub enum HiddenType {} + +#[doc(hidden)] +pub trait HiddenTrait {} + +pub enum MyLibType {} + +// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3CHiddenType%3E"]' 'impl From for MyLibType' +impl From for MyLibType { + fn from(it: HiddenType) -> MyLibType { + match it {} + } +} + +pub struct T(T); + +// @!has foo/enum.MyLibType.html '//*[@id="impl-From%3CT%3CT%3CT%3CT%3CHiddenType%3E%3E%3E%3E%3E"]' 'impl From>>>> for MyLibType' +impl From>>>> for MyLibType { + fn from(it: T>>>) -> MyLibType { + todo!() + } +} + +// @!has foo/enum.MyLibType.html '//*[@id="impl-HiddenTrait"]' 'impl HiddenTrait for MyLibType' +impl HiddenTrait for MyLibType {} + +// @!has foo/struct.T.html '//*[@id="impl-From%3CMyLibType%3E"]' 'impl From for T>>>' +impl From for T>>> { + fn from(it: MyLibType) -> T>>> { + match it {} + } +}