Skip to content

Commit b5ba592

Browse files
committed
Auto merge of #33074 - mitaa:rdoc-irlst, r=alexcrichton
rustdoc: Fix the strip-hidden `ImplStripper` Instead of stripping impls which reference *stripped* items, we keep impls which reference *retained* items. We do this because when we strip an item we immediately return, and do not recurse into it - leaving the contained items non-stripped from the point of view of the `ImplStripper`. fixes #33069 r? @alexcrichton
2 parents 6e03608 + 574450a commit b5ba592

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/librustdoc/passes.rs

+22-41
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,37 @@ use fold::FoldItem::Strip;
2424

2525
/// Strip items marked `#[doc(hidden)]`
2626
pub fn strip_hidden(krate: clean::Crate) -> plugins::PluginResult {
27-
let mut stripped = DefIdSet();
27+
let mut retained = DefIdSet();
2828

2929
// strip all #[doc(hidden)] items
3030
let krate = {
3131
struct Stripper<'a> {
32-
stripped: &'a mut DefIdSet
32+
retained: &'a mut DefIdSet
3333
}
3434
impl<'a> fold::DocFolder for Stripper<'a> {
3535
fn fold_item(&mut self, i: Item) -> Option<Item> {
3636
if i.attrs.list("doc").has_word("hidden") {
3737
debug!("found one in strip_hidden; removing");
38-
self.stripped.insert(i.def_id);
39-
4038
// use a dedicated hidden item for given item type if any
4139
match i.inner {
4240
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
4341
return Strip(i).fold()
4442
}
4543
_ => return None,
4644
}
45+
} else {
46+
self.retained.insert(i.def_id);
4747
}
4848
self.fold_item_recur(i)
4949
}
5050
}
51-
let mut stripper = Stripper{ stripped: &mut stripped };
51+
let mut stripper = Stripper{ retained: &mut retained };
5252
stripper.fold_crate(krate)
5353
};
5454

55-
// strip any traits implemented on stripped items
56-
{
57-
struct ImplStripper<'a> {
58-
stripped: &'a mut DefIdSet
59-
}
60-
impl<'a> fold::DocFolder for ImplStripper<'a> {
61-
fn fold_item(&mut self, i: Item) -> Option<Item> {
62-
if let clean::ImplItem(clean::Impl{
63-
for_: clean::ResolvedPath{ did, .. },
64-
ref trait_, ..
65-
}) = i.inner {
66-
// Impls for stripped types don't need to exist
67-
if self.stripped.contains(&did) {
68-
return None;
69-
}
70-
// Impls of stripped traits also don't need to exist
71-
if let Some(did) = trait_.def_id() {
72-
if self.stripped.contains(&did) {
73-
return None;
74-
}
75-
}
76-
}
77-
self.fold_item_recur(i)
78-
}
79-
}
80-
let mut stripper = ImplStripper{ stripped: &mut stripped };
81-
stripper.fold_crate(krate)
82-
}
55+
// strip all impls referencing stripped items
56+
let mut stripper = ImplStripper { retained: &retained };
57+
stripper.fold_crate(krate)
8358
}
8459

8560
/// Strip private items from the point of view of a crate or externally from a
@@ -98,11 +73,9 @@ pub fn strip_private(mut krate: clean::Crate) -> plugins::PluginResult {
9873
krate = ImportStripper.fold_crate(stripper.fold_crate(krate));
9974
}
10075

101-
// strip all private implementations of traits
102-
{
103-
let mut stripper = ImplStripper(&retained);
104-
stripper.fold_crate(krate)
105-
}
76+
// strip all impls referencing private items
77+
let mut stripper = ImplStripper { retained: &retained };
78+
stripper.fold_crate(krate)
10679
}
10780

10881
struct Stripper<'a> {
@@ -201,13 +174,21 @@ impl<'a> fold::DocFolder for Stripper<'a> {
201174
}
202175
}
203176

204-
// This stripper discards all private impls of traits
205-
struct ImplStripper<'a>(&'a DefIdSet);
177+
// This stripper discards all impls which reference stripped items
178+
struct ImplStripper<'a> {
179+
retained: &'a DefIdSet
180+
}
181+
206182
impl<'a> fold::DocFolder for ImplStripper<'a> {
207183
fn fold_item(&mut self, i: Item) -> Option<Item> {
208184
if let clean::ImplItem(ref imp) = i.inner {
185+
if let Some(did) = imp.for_.def_id() {
186+
if did.is_local() && !self.retained.contains(&did) {
187+
return None;
188+
}
189+
}
209190
if let Some(did) = imp.trait_.def_id() {
210-
if did.is_local() && !self.0.contains(&did) {
191+
if did.is_local() && !self.retained.contains(&did) {
211192
return None;
212193
}
213194
}

src/test/rustdoc/issue-33069.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub trait Bar {}
12+
13+
#[doc(hidden)]
14+
pub mod hidden {
15+
pub struct Foo;
16+
}
17+
18+
// @has issue_33069/trait.Bar.html
19+
// @!has - '//code' 'impl Bar for Foo'
20+
impl Bar for hidden::Foo {}

0 commit comments

Comments
 (0)