Skip to content

Commit 222c572

Browse files
committed
Auto merge of #94053 - GuillaumeGomez:fields-stripped, r=notriddle
rustdoc: Remove fields_stripped fields (and equivalents) Fixes #90588. r? `@camelid`
2 parents 7f997f5 + 8323b05 commit 222c572

File tree

10 files changed

+75
-54
lines changed

10 files changed

+75
-54
lines changed

src/librustdoc/clean/inline.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
236236

237237
clean::Enum {
238238
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
239-
variants_stripped: false,
240239
variants: cx.tcx.adt_def(did).variants().iter().map(|v| v.clean(cx)).collect(),
241240
}
242241
}
@@ -249,7 +248,6 @@ fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
249248
struct_type: variant.ctor_kind,
250249
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
251250
fields: variant.fields.iter().map(|x| x.clean(cx)).collect(),
252-
fields_stripped: false,
253251
}
254252
}
255253

@@ -259,7 +257,7 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
259257

260258
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
261259
let fields = variant.fields.iter().map(|x| x.clean(cx)).collect();
262-
clean::Union { generics, fields, fields_stripped: false }
260+
clean::Union { generics, fields }
263261
}
264262

265263
fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::Typedef {

src/librustdoc/clean/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,6 @@ impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
17841784
VariantStruct {
17851785
struct_type: CtorKind::from_hir(self),
17861786
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
1787-
fields_stripped: false,
17881787
}
17891788
}
17901789
}
@@ -1804,7 +1803,6 @@ impl Clean<Item> for ty::VariantDef {
18041803
}
18051804
CtorKind::Fictive => Variant::Struct(VariantStruct {
18061805
struct_type: CtorKind::Fictive,
1807-
fields_stripped: false,
18081806
fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
18091807
}),
18101808
};
@@ -1914,7 +1912,6 @@ fn clean_maybe_renamed_item(
19141912
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
19151913
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
19161914
generics: generics.clean(cx),
1917-
variants_stripped: false,
19181915
}),
19191916
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
19201917
generics: generics.clean(cx),
@@ -1923,13 +1920,11 @@ fn clean_maybe_renamed_item(
19231920
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
19241921
generics: generics.clean(cx),
19251922
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
1926-
fields_stripped: false,
19271923
}),
19281924
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
19291925
struct_type: CtorKind::from_hir(variant_data),
19301926
generics: generics.clean(cx),
19311927
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
1932-
fields_stripped: false,
19331928
}),
19341929
ItemKind::Impl(ref impl_) => return clean_impl(impl_, item.hir_id(), cx),
19351930
// proc macros can have a name set by attributes

src/librustdoc/clean/types.rs

+42-8
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,12 @@ impl Item {
619619
_ => false,
620620
}
621621
}
622-
pub(crate) fn has_stripped_fields(&self) -> Option<bool> {
622+
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
623623
match *self.kind {
624-
StructItem(ref _struct) => Some(_struct.fields_stripped),
625-
UnionItem(ref union) => Some(union.fields_stripped),
626-
VariantItem(Variant::Struct(ref vstruct)) => Some(vstruct.fields_stripped),
624+
StructItem(ref struct_) => Some(struct_.has_stripped_entries()),
625+
UnionItem(ref union_) => Some(union_.has_stripped_entries()),
626+
EnumItem(ref enum_) => Some(enum_.has_stripped_entries()),
627+
VariantItem(ref v) => v.has_stripped_entries(),
627628
_ => None,
628629
}
629630
}
@@ -2028,14 +2029,24 @@ pub(crate) struct Struct {
20282029
pub(crate) struct_type: CtorKind,
20292030
pub(crate) generics: Generics,
20302031
pub(crate) fields: Vec<Item>,
2031-
pub(crate) fields_stripped: bool,
2032+
}
2033+
2034+
impl Struct {
2035+
pub(crate) fn has_stripped_entries(&self) -> bool {
2036+
self.fields.iter().any(|f| f.is_stripped())
2037+
}
20322038
}
20332039

20342040
#[derive(Clone, Debug)]
20352041
pub(crate) struct Union {
20362042
pub(crate) generics: Generics,
20372043
pub(crate) fields: Vec<Item>,
2038-
pub(crate) fields_stripped: bool,
2044+
}
2045+
2046+
impl Union {
2047+
pub(crate) fn has_stripped_entries(&self) -> bool {
2048+
self.fields.iter().any(|f| f.is_stripped())
2049+
}
20392050
}
20402051

20412052
/// This is a more limited form of the standard Struct, different in that
@@ -2045,14 +2056,28 @@ pub(crate) struct Union {
20452056
pub(crate) struct VariantStruct {
20462057
pub(crate) struct_type: CtorKind,
20472058
pub(crate) fields: Vec<Item>,
2048-
pub(crate) fields_stripped: bool,
2059+
}
2060+
2061+
impl VariantStruct {
2062+
pub(crate) fn has_stripped_entries(&self) -> bool {
2063+
self.fields.iter().any(|f| f.is_stripped())
2064+
}
20492065
}
20502066

20512067
#[derive(Clone, Debug)]
20522068
pub(crate) struct Enum {
20532069
pub(crate) variants: IndexVec<VariantIdx, Item>,
20542070
pub(crate) generics: Generics,
2055-
pub(crate) variants_stripped: bool,
2071+
}
2072+
2073+
impl Enum {
2074+
pub(crate) fn has_stripped_entries(&self) -> bool {
2075+
self.variants.iter().any(|f| f.is_stripped())
2076+
}
2077+
2078+
pub(crate) fn variants(&self) -> impl Iterator<Item = &Item> {
2079+
self.variants.iter().filter(|v| !v.is_stripped())
2080+
}
20562081
}
20572082

20582083
#[derive(Clone, Debug)]
@@ -2062,6 +2087,15 @@ pub(crate) enum Variant {
20622087
Struct(VariantStruct),
20632088
}
20642089

2090+
impl Variant {
2091+
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
2092+
match *self {
2093+
Self::Struct(ref struct_) => Some(struct_.has_stripped_entries()),
2094+
Self::CLike | Self::Tuple(_) => None,
2095+
}
2096+
}
2097+
}
2098+
20652099
/// Small wrapper around [`rustc_span::Span`] that adds helper methods
20662100
/// and enforces calling [`rustc_span::Span::source_callsite()`].
20672101
#[derive(Copy, Clone, Debug)]

src/librustdoc/fold.rs

-20
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,15 @@ pub(crate) trait DocFolder: Sized {
1818
StrippedItem(..) => unreachable!(),
1919
ModuleItem(i) => ModuleItem(self.fold_mod(i)),
2020
StructItem(mut i) => {
21-
let num_fields = i.fields.len();
2221
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
23-
if !i.fields_stripped {
24-
i.fields_stripped =
25-
num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
26-
}
2722
StructItem(i)
2823
}
2924
UnionItem(mut i) => {
30-
let num_fields = i.fields.len();
3125
i.fields = i.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
32-
if !i.fields_stripped {
33-
i.fields_stripped =
34-
num_fields != i.fields.len() || i.fields.iter().any(|f| f.is_stripped());
35-
}
3626
UnionItem(i)
3727
}
3828
EnumItem(mut i) => {
39-
let num_variants = i.variants.len();
4029
i.variants = i.variants.into_iter().filter_map(|x| self.fold_item(x)).collect();
41-
if !i.variants_stripped {
42-
i.variants_stripped = num_variants != i.variants.len()
43-
|| i.variants.iter().any(|f| f.is_stripped());
44-
}
4530
EnumItem(i)
4631
}
4732
TraitItem(mut i) => {
@@ -54,12 +39,7 @@ pub(crate) trait DocFolder: Sized {
5439
}
5540
VariantItem(i) => match i {
5641
Variant::Struct(mut j) => {
57-
let num_fields = j.fields.len();
5842
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
59-
if !j.fields_stripped {
60-
j.fields_stripped = num_fields != j.fields.len()
61-
|| j.fields.iter().any(|f| f.is_stripped());
62-
}
6343
VariantItem(Variant::Struct(j))
6444
}
6545
Variant::Tuple(fields) => {

src/librustdoc/html/render/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2363,8 +2363,7 @@ fn sidebar_enum(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, e: &clean:
23632363
let mut sidebar = Buffer::new();
23642364

23652365
let mut variants = e
2366-
.variants
2367-
.iter()
2366+
.variants()
23682367
.filter_map(|v| {
23692368
v.name
23702369
.as_ref()

src/librustdoc/html/render/print_item.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]
11361136
}
11371137

11381138
fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
1139+
let count_variants = e.variants().count();
11391140
wrap_into_docblock(w, |w| {
11401141
wrap_item(w, "enum", |w| {
11411142
render_attributes_in_pre(w, it, "");
@@ -1147,16 +1148,16 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
11471148
e.generics.print(cx),
11481149
print_where_clause(&e.generics, cx, 0, true),
11491150
);
1150-
if e.variants.is_empty() && !e.variants_stripped {
1151+
let variants_stripped = e.has_stripped_entries();
1152+
if count_variants == 0 && !variants_stripped {
11511153
w.write_str(" {}");
11521154
} else {
11531155
w.write_str(" {\n");
1154-
let count_variants = e.variants.len();
11551156
let toggle = should_hide_fields(count_variants);
11561157
if toggle {
11571158
toggle_open(w, format_args!("{} variants", count_variants));
11581159
}
1159-
for v in &e.variants {
1160+
for v in e.variants() {
11601161
w.write_str(" ");
11611162
let name = v.name.unwrap();
11621163
match *v.kind {
@@ -1185,7 +1186,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
11851186
w.write_str(",\n");
11861187
}
11871188

1188-
if e.variants_stripped {
1189+
if variants_stripped {
11891190
w.write_str(" // some variants omitted\n");
11901191
}
11911192
if toggle {
@@ -1198,15 +1199,15 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
11981199

11991200
document(w, cx, it, None, HeadingOffset::H2);
12001201

1201-
if !e.variants.is_empty() {
1202+
if count_variants != 0 {
12021203
write!(
12031204
w,
12041205
"<h2 id=\"variants\" class=\"variants small-section-header\">\
12051206
Variants{}<a href=\"#variants\" class=\"anchor\"></a></h2>",
12061207
document_non_exhaustive_header(it)
12071208
);
12081209
document_non_exhaustive(w, it);
1209-
for variant in &e.variants {
1210+
for variant in e.variants() {
12101211
let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
12111212
write!(
12121213
w,
@@ -1650,7 +1651,7 @@ fn render_union(
16501651
}
16511652
}
16521653

1653-
if it.has_stripped_fields().unwrap() {
1654+
if it.has_stripped_entries().unwrap() {
16541655
write!(w, " /* private fields */\n{}", tab);
16551656
}
16561657
if toggle {
@@ -1706,11 +1707,11 @@ fn render_struct(
17061707
}
17071708

17081709
if has_visible_fields {
1709-
if it.has_stripped_fields().unwrap() {
1710+
if it.has_stripped_entries().unwrap() {
17101711
write!(w, "\n{} /* private fields */", tab);
17111712
}
17121713
write!(w, "\n{}", tab);
1713-
} else if it.has_stripped_fields().unwrap() {
1714+
} else if it.has_stripped_entries().unwrap() {
17141715
write!(w, " /* private fields */ ");
17151716
}
17161717
if toggle {

src/librustdoc/json/conversions.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
249249

250250
impl FromWithTcx<clean::Struct> for Struct {
251251
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
252-
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
252+
let fields_stripped = struct_.has_stripped_entries();
253+
let clean::Struct { struct_type, generics, fields } = struct_;
253254
Struct {
254255
struct_type: from_ctor_kind(struct_type),
255256
generics: generics.into_tcx(tcx),
@@ -261,8 +262,9 @@ impl FromWithTcx<clean::Struct> for Struct {
261262
}
262263

263264
impl FromWithTcx<clean::Union> for Union {
264-
fn from_tcx(struct_: clean::Union, tcx: TyCtxt<'_>) -> Self {
265-
let clean::Union { generics, fields, fields_stripped } = struct_;
265+
fn from_tcx(union_: clean::Union, tcx: TyCtxt<'_>) -> Self {
266+
let fields_stripped = union_.has_stripped_entries();
267+
let clean::Union { generics, fields } = union_;
266268
Union {
267269
generics: generics.into_tcx(tcx),
268270
fields_stripped,
@@ -586,7 +588,8 @@ pub(crate) fn from_function_method(
586588

587589
impl FromWithTcx<clean::Enum> for Enum {
588590
fn from_tcx(enum_: clean::Enum, tcx: TyCtxt<'_>) -> Self {
589-
let clean::Enum { variants, generics, variants_stripped } = enum_;
591+
let variants_stripped = enum_.has_stripped_entries();
592+
let clean::Enum { variants, generics } = enum_;
590593
Enum {
591594
generics: generics.into_tcx(tcx),
592595
variants_stripped,
@@ -598,7 +601,8 @@ impl FromWithTcx<clean::Enum> for Enum {
598601

599602
impl FromWithTcx<clean::VariantStruct> for Struct {
600603
fn from_tcx(struct_: clean::VariantStruct, _tcx: TyCtxt<'_>) -> Self {
601-
let clean::VariantStruct { struct_type, fields, fields_stripped } = struct_;
604+
let fields_stripped = struct_.has_stripped_entries();
605+
let clean::VariantStruct { struct_type, fields } = struct_;
602606
Struct {
603607
struct_type: from_ctor_kind(struct_type),
604608
generics: Default::default(),

src/librustdoc/passes/strip_hidden.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ impl<'a> DocFolder for Stripper<'a> {
3838
fn fold_item(&mut self, i: Item) -> Option<Item> {
3939
if i.attrs.lists(sym::doc).has_word(sym::hidden) {
4040
debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
41-
// use a dedicated hidden item for given item type if any
41+
// Use a dedicated hidden item for fields, variants, and modules.
42+
// We need to keep private fields and variants, so that the docs
43+
// can show a placeholder "// some variants omitted". We need to keep
44+
// private modules, because they can contain impl blocks, and impl
45+
// block privacy is inherited from the type and trait, not from the
46+
// module it's defined in. Both of these are marked "stripped," and
47+
// not included in the final docs, but since they still have an effect
48+
// on the final doc, cannot be completely removed from the Clean IR.
4249
match *i.kind {
43-
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
50+
clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
4451
// We need to recurse into stripped modules to
4552
// strip things like impl methods but when doing so
4653
// we must not add any items to the `retained` set.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ul><li><a href="#variant.Shown">Shown</a></li></ul>

src/test/rustdoc/strip-enum-variant.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// @has - '//code' 'Shown'
33
// @!has - '//code' 'NotShown'
44
// @has - '//code' '// some variants omitted'
5+
// Also check that `NotShown` isn't displayed in the sidebar.
6+
// @snapshot no-not-shown - '//*[@class="sidebar-elems"]/section/*[@class="block"][1]/ul'
57
pub enum MyThing {
68
Shown,
79
#[doc(hidden)]

0 commit comments

Comments
 (0)