Skip to content

Commit 6092cbb

Browse files
Remove fields_stripped fields (and equivalents)
1 parent 4f372b1 commit 6092cbb

File tree

8 files changed

+64
-53
lines changed

8 files changed

+64
-53
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
@@ -1771,7 +1771,6 @@ impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
17711771
VariantStruct {
17721772
struct_type: CtorKind::from_hir(self),
17731773
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
1774-
fields_stripped: false,
17751774
}
17761775
}
17771776
}
@@ -1791,7 +1790,6 @@ impl Clean<Item> for ty::VariantDef {
17911790
}
17921791
CtorKind::Fictive => Variant::Struct(VariantStruct {
17931792
struct_type: CtorKind::Fictive,
1794-
fields_stripped: false,
17951793
fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
17961794
}),
17971795
};
@@ -1900,7 +1898,6 @@ fn clean_maybe_renamed_item(
19001898
ItemKind::Enum(ref def, ref generics) => EnumItem(Enum {
19011899
variants: def.variants.iter().map(|v| v.clean(cx)).collect(),
19021900
generics: generics.clean(cx),
1903-
variants_stripped: false,
19041901
}),
19051902
ItemKind::TraitAlias(ref generics, bounds) => TraitAliasItem(TraitAlias {
19061903
generics: generics.clean(cx),
@@ -1909,13 +1906,11 @@ fn clean_maybe_renamed_item(
19091906
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
19101907
generics: generics.clean(cx),
19111908
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
1912-
fields_stripped: false,
19131909
}),
19141910
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
19151911
struct_type: CtorKind::from_hir(variant_data),
19161912
generics: generics.clean(cx),
19171913
fields: variant_data.fields().iter().map(|x| x.clean(cx)).collect(),
1918-
fields_stripped: false,
19191914
}),
19201915
ItemKind::Impl(ref impl_) => return clean_impl(impl_, item.hir_id(), cx),
19211916
// 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
}
@@ -2030,14 +2031,24 @@ pub(crate) struct Struct {
20302031
pub(crate) struct_type: CtorKind,
20312032
pub(crate) generics: Generics,
20322033
pub(crate) fields: Vec<Item>,
2033-
pub(crate) fields_stripped: bool,
2034+
}
2035+
2036+
impl Struct {
2037+
pub(crate) fn has_stripped_entries(&self) -> bool {
2038+
self.fields.iter().any(|f| f.is_stripped())
2039+
}
20342040
}
20352041

20362042
#[derive(Clone, Debug)]
20372043
pub(crate) struct Union {
20382044
pub(crate) generics: Generics,
20392045
pub(crate) fields: Vec<Item>,
2040-
pub(crate) fields_stripped: bool,
2046+
}
2047+
2048+
impl Union {
2049+
pub(crate) fn has_stripped_entries(&self) -> bool {
2050+
self.fields.iter().any(|f| f.is_stripped())
2051+
}
20412052
}
20422053

20432054
/// This is a more limited form of the standard Struct, different in that
@@ -2047,14 +2058,28 @@ pub(crate) struct Union {
20472058
pub(crate) struct VariantStruct {
20482059
pub(crate) struct_type: CtorKind,
20492060
pub(crate) fields: Vec<Item>,
2050-
pub(crate) fields_stripped: bool,
2061+
}
2062+
2063+
impl VariantStruct {
2064+
pub(crate) fn has_stripped_entries(&self) -> bool {
2065+
self.fields.iter().any(|f| f.is_stripped())
2066+
}
20512067
}
20522068

20532069
#[derive(Clone, Debug)]
20542070
pub(crate) struct Enum {
20552071
pub(crate) variants: IndexVec<VariantIdx, Item>,
20562072
pub(crate) generics: Generics,
2057-
pub(crate) variants_stripped: bool,
2073+
}
2074+
2075+
impl Enum {
2076+
pub(crate) fn has_stripped_entries(&self) -> bool {
2077+
self.variants.iter().any(|f| f.is_stripped())
2078+
}
2079+
2080+
pub(crate) fn variants(&self) -> impl Iterator<Item = &Item> {
2081+
self.variants.iter().filter(|v| !v.is_stripped())
2082+
}
20582083
}
20592084

20602085
#[derive(Clone, Debug)]
@@ -2064,6 +2089,15 @@ pub(crate) enum Variant {
20642089
Struct(VariantStruct),
20652090
}
20662091

2092+
impl Variant {
2093+
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
2094+
match *self {
2095+
Self::Struct(ref struct_) => Some(struct_.has_stripped_entries()),
2096+
Self::CLike | Self::Tuple(_) => None,
2097+
}
2098+
}
2099+
}
2100+
20672101
/// Small wrapper around [`rustc_span::Span`] that adds helper methods
20682102
/// and enforces calling [`rustc_span::Span::source_callsite()`].
20692103
#[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
@@ -1139,6 +1139,7 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]
11391139
}
11401140

11411141
fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum) {
1142+
let count_variants = e.variants().count();
11421143
wrap_into_docblock(w, |w| {
11431144
wrap_item(w, "enum", |w| {
11441145
render_attributes_in_pre(w, it, "");
@@ -1150,16 +1151,16 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
11501151
e.generics.print(cx),
11511152
print_where_clause(&e.generics, cx, 0, true),
11521153
);
1153-
if e.variants.is_empty() && !e.variants_stripped {
1154+
let variants_stripped = e.has_stripped_entries();
1155+
if count_variants == 0 && !variants_stripped {
11541156
w.write_str(" {}");
11551157
} else {
11561158
w.write_str(" {\n");
1157-
let count_variants = e.variants.len();
11581159
let toggle = should_hide_fields(count_variants);
11591160
if toggle {
11601161
toggle_open(w, format_args!("{} variants", count_variants));
11611162
}
1162-
for v in &e.variants {
1163+
for v in e.variants() {
11631164
w.write_str(" ");
11641165
let name = v.name.unwrap();
11651166
match *v.kind {
@@ -1188,7 +1189,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
11881189
w.write_str(",\n");
11891190
}
11901191

1191-
if e.variants_stripped {
1192+
if variants_stripped {
11921193
w.write_str(" // some variants omitted\n");
11931194
}
11941195
if toggle {
@@ -1201,15 +1202,15 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
12011202

12021203
document(w, cx, it, None, HeadingOffset::H2);
12031204

1204-
if !e.variants.is_empty() {
1205+
if count_variants != 0 {
12051206
write!(
12061207
w,
12071208
"<h2 id=\"variants\" class=\"variants small-section-header\">\
12081209
Variants{}<a href=\"#variants\" class=\"anchor\"></a></h2>",
12091210
document_non_exhaustive_header(it)
12101211
);
12111212
document_non_exhaustive(w, it);
1212-
for variant in &e.variants {
1213+
for variant in e.variants() {
12131214
let id = cx.derive_id(format!("{}.{}", ItemType::Variant, variant.name.unwrap()));
12141215
write!(
12151216
w,
@@ -1653,7 +1654,7 @@ fn render_union(
16531654
}
16541655
}
16551656

1656-
if it.has_stripped_fields().unwrap() {
1657+
if it.has_stripped_entries().unwrap() {
16571658
write!(w, " /* private fields */\n{}", tab);
16581659
}
16591660
if toggle {
@@ -1709,11 +1710,11 @@ fn render_struct(
17091710
}
17101711

17111712
if has_visible_fields {
1712-
if it.has_stripped_fields().unwrap() {
1713+
if it.has_stripped_entries().unwrap() {
17131714
write!(w, "\n{} /* private fields */", tab);
17141715
}
17151716
write!(w, "\n{}", tab);
1716-
} else if it.has_stripped_fields().unwrap() {
1717+
} else if it.has_stripped_entries().unwrap() {
17171718
write!(w, " /* private fields */ ");
17181719
}
17191720
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a> DocFolder for Stripper<'a> {
4040
debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
4141
// use a dedicated hidden item for given item type if any
4242
match *i.kind {
43-
clean::StructFieldItem(..) | clean::ModuleItem(..) => {
43+
clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
4444
// We need to recurse into stripped modules to
4545
// strip things like impl methods but when doing so
4646
// we must not add any items to the `retained` set.

0 commit comments

Comments
 (0)