Skip to content

Commit bb561da

Browse files
committed
Rollup merge of rust-lang#32678 - mitaa:rdoc-stripped, r=alexcrichton
rustdoc: make rustdoc less pass-aware Instead of hardcoding knowledge about the strip-private pass into the rendering process we represent (some) stripped items as `ItemEnum::StrippedItem`. Rustdoc will, for example, generate redirect pages for public items contained in private modules which have been re-exported to somewhere externally reachable - this will now not only work for the `strip-private` pass, but for other passes as well, such as the `strip-hidden` pass. r? @alexcrichton
2 parents 3c4d5f9 + 95eb8a6 commit bb561da

File tree

9 files changed

+364
-239
lines changed

9 files changed

+364
-239
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
pub use self::Type::*;
1515
pub use self::PrimitiveType::*;
1616
pub use self::TypeKind::*;
17-
pub use self::StructField::*;
1817
pub use self::VariantKind::*;
1918
pub use self::Mutability::*;
2019
pub use self::Import::*;
@@ -53,6 +52,7 @@ use std::env::current_dir;
5352
use core::DocContext;
5453
use doctree;
5554
use visit_ast;
55+
use html::item_type::ItemType;
5656

5757
/// A stable identifier to the particular version of JSON output.
5858
/// Increment this when the `Crate` and related structures change.
@@ -273,36 +273,49 @@ impl Item {
273273
}
274274
pub fn is_crate(&self) -> bool {
275275
match self.inner {
276-
ModuleItem(Module { items: _, is_crate: true }) => true,
277-
_ => false
276+
StrippedItem(box ModuleItem(Module { is_crate: true, ..})) |
277+
ModuleItem(Module { is_crate: true, ..}) => true,
278+
_ => false,
278279
}
279280
}
280281
pub fn is_mod(&self) -> bool {
281-
match self.inner { ModuleItem(..) => true, _ => false }
282+
ItemType::from_item(self) == ItemType::Module
282283
}
283284
pub fn is_trait(&self) -> bool {
284-
match self.inner { TraitItem(..) => true, _ => false }
285+
ItemType::from_item(self) == ItemType::Trait
285286
}
286287
pub fn is_struct(&self) -> bool {
287-
match self.inner { StructItem(..) => true, _ => false }
288+
ItemType::from_item(self) == ItemType::Struct
288289
}
289290
pub fn is_enum(&self) -> bool {
290-
match self.inner { EnumItem(..) => true, _ => false }
291+
ItemType::from_item(self) == ItemType::Module
291292
}
292293
pub fn is_fn(&self) -> bool {
293-
match self.inner { FunctionItem(..) => true, _ => false }
294+
ItemType::from_item(self) == ItemType::Function
294295
}
295296
pub fn is_associated_type(&self) -> bool {
296-
match self.inner { AssociatedTypeItem(..) => true, _ => false }
297+
ItemType::from_item(self) == ItemType::AssociatedType
297298
}
298299
pub fn is_associated_const(&self) -> bool {
299-
match self.inner { AssociatedConstItem(..) => true, _ => false }
300+
ItemType::from_item(self) == ItemType::AssociatedConst
300301
}
301302
pub fn is_method(&self) -> bool {
302-
match self.inner { MethodItem(..) => true, _ => false }
303+
ItemType::from_item(self) == ItemType::Method
303304
}
304305
pub fn is_ty_method(&self) -> bool {
305-
match self.inner { TyMethodItem(..) => true, _ => false }
306+
ItemType::from_item(self) == ItemType::TyMethod
307+
}
308+
pub fn is_stripped(&self) -> bool {
309+
match self.inner { StrippedItem(..) => true, _ => false }
310+
}
311+
pub fn has_stripped_fields(&self) -> Option<bool> {
312+
match self.inner {
313+
StructItem(ref _struct) => Some(_struct.fields_stripped),
314+
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
315+
Some(vstruct.fields_stripped)
316+
},
317+
_ => None,
318+
}
306319
}
307320

308321
pub fn stability_class(&self) -> String {
@@ -341,7 +354,7 @@ pub enum ItemEnum {
341354
TyMethodItem(TyMethod),
342355
/// A method with a body.
343356
MethodItem(Method),
344-
StructFieldItem(StructField),
357+
StructFieldItem(Type),
345358
VariantItem(Variant),
346359
/// `fn`s from an extern block
347360
ForeignFunctionItem(Function),
@@ -352,6 +365,8 @@ pub enum ItemEnum {
352365
AssociatedConstItem(Type, Option<String>),
353366
AssociatedTypeItem(Vec<TyParamBound>, Option<Type>),
354367
DefaultImplItem(DefaultImpl),
368+
/// An item that has been stripped by a rustdoc pass
369+
StrippedItem(Box<ItemEnum>),
355370
}
356371

357372
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -1733,12 +1748,6 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
17331748
}
17341749
}
17351750

1736-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1737-
pub enum StructField {
1738-
HiddenStructField, // inserted later by strip passes
1739-
TypedStructField(Type),
1740-
}
1741-
17421751
impl Clean<Item> for hir::StructField {
17431752
fn clean(&self, cx: &DocContext) -> Item {
17441753
Item {
@@ -1749,7 +1758,7 @@ impl Clean<Item> for hir::StructField {
17491758
stability: get_stability(cx, cx.map.local_def_id(self.id)),
17501759
deprecation: get_deprecation(cx, cx.map.local_def_id(self.id)),
17511760
def_id: cx.map.local_def_id(self.id),
1752-
inner: StructFieldItem(TypedStructField(self.ty.clean(cx))),
1761+
inner: StructFieldItem(self.ty.clean(cx)),
17531762
}
17541763
}
17551764
}
@@ -1766,7 +1775,7 @@ impl<'tcx> Clean<Item> for ty::FieldDefData<'tcx, 'static> {
17661775
stability: get_stability(cx, self.did),
17671776
deprecation: get_deprecation(cx, self.did),
17681777
def_id: self.did,
1769-
inner: StructFieldItem(TypedStructField(self.unsubst_ty().clean(cx))),
1778+
inner: StructFieldItem(self.unsubst_ty().clean(cx)),
17701779
}
17711780
}
17721781
}
@@ -1897,9 +1906,7 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
18971906
def_id: field.did,
18981907
stability: get_stability(cx, field.did),
18991908
deprecation: get_deprecation(cx, field.did),
1900-
inner: StructFieldItem(
1901-
TypedStructField(field.unsubst_ty().clean(cx))
1902-
)
1909+
inner: StructFieldItem(field.unsubst_ty().clean(cx))
19031910
}
19041911
}).collect()
19051912
})

src/librustdoc/fold.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,50 @@
1010

1111
use clean::*;
1212

13+
pub enum FoldItem {
14+
Retain(Item),
15+
Strip(Item),
16+
Erase,
17+
}
18+
19+
impl FoldItem {
20+
pub fn fold(self) -> Option<Item> {
21+
match self {
22+
FoldItem::Erase => None,
23+
FoldItem::Retain(i) => Some(i),
24+
FoldItem::Strip(item@ Item { inner: StrippedItem(..), .. } ) => Some(item),
25+
FoldItem::Strip(mut i) => {
26+
i.inner = StrippedItem(box i.inner);
27+
Some(i)
28+
}
29+
}
30+
}
31+
}
32+
1333
pub trait DocFolder : Sized {
1434
fn fold_item(&mut self, item: Item) -> Option<Item> {
1535
self.fold_item_recur(item)
1636
}
1737

1838
/// don't override!
19-
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
20-
let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item;
21-
let inner = match inner {
39+
fn fold_inner_recur(&mut self, inner: ItemEnum) -> ItemEnum {
40+
match inner {
41+
StrippedItem(..) => unreachable!(),
42+
ModuleItem(i) => {
43+
ModuleItem(self.fold_mod(i))
44+
},
2245
StructItem(mut i) => {
2346
let num_fields = i.fields.len();
2447
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
25-
i.fields_stripped |= num_fields != i.fields.len();
48+
i.fields_stripped |= num_fields != i.fields.len() ||
49+
i.fields.iter().any(|f| f.is_stripped());
2650
StructItem(i)
2751
},
28-
ModuleItem(i) => {
29-
ModuleItem(self.fold_mod(i))
30-
},
3152
EnumItem(mut i) => {
3253
let num_variants = i.variants.len();
3354
i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
34-
i.variants_stripped |= num_variants != i.variants.len();
55+
i.variants_stripped |= num_variants != i.variants.len() ||
56+
i.variants.iter().any(|f| f.is_stripped());
3557
EnumItem(i)
3658
},
3759
TraitItem(mut i) => {
@@ -48,13 +70,24 @@ pub trait DocFolder : Sized {
4870
StructVariant(mut j) => {
4971
let num_fields = j.fields.len();
5072
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
51-
j.fields_stripped |= num_fields != j.fields.len();
73+
j.fields_stripped |= num_fields != j.fields.len() ||
74+
j.fields.iter().any(|f| f.is_stripped());
5275
VariantItem(Variant {kind: StructVariant(j), ..i2})
5376
},
5477
_ => VariantItem(i2)
5578
}
5679
},
5780
x => x
81+
}
82+
}
83+
84+
/// don't override!
85+
fn fold_item_recur(&mut self, item: Item) -> Option<Item> {
86+
let Item { attrs, name, source, visibility, def_id, inner, stability, deprecation } = item;
87+
88+
let inner = match inner {
89+
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
90+
_ => self.fold_inner_recur(inner),
5891
};
5992

6093
Some(Item { attrs: attrs, name: name, source: source, inner: inner,
@@ -70,9 +103,8 @@ pub trait DocFolder : Sized {
70103
}
71104

72105
fn fold_crate(&mut self, mut c: Crate) -> Crate {
73-
c.module = c.module.and_then(|module| {
74-
self.fold_item(module)
75-
});
106+
c.module = c.module.and_then(|module| self.fold_item(module));
107+
76108
c.external_traits = c.external_traits.into_iter().map(|(k, mut v)| {
77109
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
78110
(k, v)

src/librustdoc/html/item_type.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ pub enum ItemType {
4444

4545
impl ItemType {
4646
pub fn from_item(item: &clean::Item) -> ItemType {
47-
match item.inner {
47+
let inner = match item.inner {
48+
clean::StrippedItem(box ref item) => item,
49+
ref inner@_ => inner,
50+
};
51+
52+
match *inner {
4853
clean::ModuleItem(..) => ItemType::Module,
4954
clean::ExternCrateItem(..) => ItemType::ExternCrate,
5055
clean::ImportItem(..) => ItemType::Import,
@@ -67,6 +72,7 @@ impl ItemType {
6772
clean::AssociatedConstItem(..) => ItemType::AssociatedConst,
6873
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
6974
clean::DefaultImplItem(..) => ItemType::Impl,
75+
clean::StrippedItem(..) => unreachable!(),
7076
}
7177
}
7278

0 commit comments

Comments
 (0)