Skip to content

Commit 450c5ea

Browse files
committed
Move StructType to clean, remove it from Unions, make JSON output whether something is a union
1 parent a4cbb44 commit 450c5ea

File tree

7 files changed

+40
-47
lines changed

7 files changed

+40
-47
lines changed

src/librustdoc/clean/inline.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
1616
use rustc_span::Span;
1717

18-
use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
18+
use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind, StructType};
1919
use crate::core::DocContext;
20-
use crate::doctree;
2120

2221
use super::Clean;
2322

@@ -247,9 +246,9 @@ fn build_struct(cx: &DocContext<'_>, did: DefId) -> clean::Struct {
247246

248247
clean::Struct {
249248
struct_type: match variant.ctor_kind {
250-
CtorKind::Fictive => doctree::Plain,
251-
CtorKind::Fn => doctree::Tuple,
252-
CtorKind::Const => doctree::Unit,
249+
CtorKind::Fictive => StructType::Plain,
250+
CtorKind::Fn => StructType::Tuple,
251+
CtorKind::Const => StructType::Unit,
253252
},
254253
generics: (cx.tcx.generics_of(did), predicates).clean(cx),
255254
fields: variant.fields.clean(cx),
@@ -262,7 +261,6 @@ fn build_union(cx: &DocContext<'_>, did: DefId) -> clean::Union {
262261
let variant = cx.tcx.adt_def(did).non_enum_variant();
263262

264263
clean::Union {
265-
struct_type: doctree::Plain,
266264
generics: (cx.tcx.generics_of(did), predicates).clean(cx),
267265
fields: variant.fields.clean(cx),
268266
fields_stripped: false,

src/librustdoc/clean/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1824,10 +1824,19 @@ impl Clean<Visibility> for ty::Visibility {
18241824
}
18251825
}
18261826

1827+
crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
1828+
use StructType::*;
1829+
match *vdata {
1830+
hir::VariantData::Struct(..) => Plain,
1831+
hir::VariantData::Tuple(..) => Tuple,
1832+
hir::VariantData::Unit(..) => Unit,
1833+
}
1834+
}
1835+
18271836
impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
18281837
fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
18291838
VariantStruct {
1830-
struct_type: doctree::struct_type_from_def(self),
1839+
struct_type: struct_type_from_def(self),
18311840
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
18321841
fields_stripped: false,
18331842
}
@@ -1842,7 +1851,7 @@ impl Clean<Item> for ty::VariantDef {
18421851
self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect(),
18431852
),
18441853
CtorKind::Fictive => Variant::Struct(VariantStruct {
1845-
struct_type: doctree::Plain,
1854+
struct_type: StructType::Plain,
18461855
fields_stripped: false,
18471856
fields: self
18481857
.fields
@@ -1996,13 +2005,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
19962005
bounds: bounds.clean(cx),
19972006
}),
19982007
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
1999-
struct_type: doctree::struct_type_from_def(&variant_data),
20002008
generics: generics.clean(cx),
20012009
fields: variant_data.fields().clean(cx),
20022010
fields_stripped: false,
20032011
}),
20042012
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
2005-
struct_type: doctree::struct_type_from_def(&variant_data),
2013+
struct_type: struct_type_from_def(&variant_data),
20062014
generics: generics.clean(cx),
20072015
fields: variant_data.fields().clean(cx),
20082016
fields_stripped: false,

src/librustdoc/clean/types.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::clean::inline;
3737
use crate::clean::types::Type::{QPath, ResolvedPath};
3838
use crate::clean::Clean;
3939
use crate::core::DocContext;
40-
use crate::doctree;
4140
use crate::formats::cache::cache;
4241
use crate::formats::item_type::ItemType;
4342
use crate::html::render::cache::ExternalLocation;
@@ -1683,17 +1682,26 @@ impl Visibility {
16831682
}
16841683
}
16851684

1685+
#[derive(Debug, Clone, Copy)]
1686+
crate enum StructType {
1687+
/// A braced struct
1688+
Plain,
1689+
/// A tuple struct
1690+
Tuple,
1691+
/// A unit struct
1692+
Unit,
1693+
}
1694+
16861695
#[derive(Clone, Debug)]
16871696
crate struct Struct {
1688-
crate struct_type: doctree::StructType,
1697+
crate struct_type: StructType,
16891698
crate generics: Generics,
16901699
crate fields: Vec<Item>,
16911700
crate fields_stripped: bool,
16921701
}
16931702

16941703
#[derive(Clone, Debug)]
16951704
crate struct Union {
1696-
crate struct_type: doctree::StructType,
16971705
crate generics: Generics,
16981706
crate fields: Vec<Item>,
16991707
crate fields_stripped: bool,
@@ -1704,7 +1712,7 @@ crate struct Union {
17041712
/// only as a variant in an enum.
17051713
#[derive(Clone, Debug)]
17061714
crate struct VariantStruct {
1707-
crate struct_type: doctree::StructType,
1715+
crate struct_type: StructType,
17081716
crate fields: Vec<Item>,
17091717
crate fields_stripped: bool,
17101718
}

src/librustdoc/doctree.rs

-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! This module is used to store stuff from Rust's AST in a more convenient
22
//! manner (and with prettier names) before cleaning.
3-
crate use self::StructType::*;
4-
53
use rustc_span::{self, Span, Symbol};
64

75
use rustc_hir as hir;
@@ -34,21 +32,3 @@ impl Module<'hir> {
3432
}
3533
}
3634
}
37-
38-
#[derive(Debug, Clone, Copy)]
39-
crate enum StructType {
40-
/// A braced struct
41-
Plain,
42-
/// A tuple struct
43-
Tuple,
44-
/// A unit struct
45-
Unit,
46-
}
47-
48-
crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
49-
match *vdata {
50-
hir::VariantData::Struct(..) => Plain,
51-
hir::VariantData::Tuple(..) => Tuple,
52-
hir::VariantData::Unit(..) => Unit,
53-
}
54-
}

src/librustdoc/html/render/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ use serde::{Serialize, Serializer};
6868
use crate::clean::{self, AttributesExt, GetDefId, RenderedLink, SelfTy, TypeKind};
6969
use crate::config::{RenderInfo, RenderOptions};
7070
use crate::docfs::{DocFS, PathError};
71-
use crate::doctree;
7271
use crate::error::Error;
7372
use crate::formats::cache::{cache, Cache};
7473
use crate::formats::item_type::ItemType;
@@ -3101,7 +3100,7 @@ fn item_struct(
31013100
_ => None,
31023101
})
31033102
.peekable();
3104-
if let doctree::Plain = s.struct_type {
3103+
if let clean::StructType::Plain = s.struct_type {
31053104
if fields.peek().is_some() {
31063105
write!(
31073106
w,
@@ -3351,7 +3350,7 @@ fn render_struct(
33513350
w: &mut Buffer,
33523351
it: &clean::Item,
33533352
g: Option<&clean::Generics>,
3354-
ty: doctree::StructType,
3353+
ty: clean::StructType,
33553354
fields: &[clean::Item],
33563355
tab: &str,
33573356
structhead: bool,
@@ -3368,7 +3367,7 @@ fn render_struct(
33683367
write!(w, "{}", g.print())
33693368
}
33703369
match ty {
3371-
doctree::Plain => {
3370+
clean::StructType::Plain => {
33723371
if let Some(g) = g {
33733372
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })
33743373
}
@@ -3400,7 +3399,7 @@ fn render_struct(
34003399
}
34013400
write!(w, "}}");
34023401
}
3403-
doctree::Tuple => {
3402+
clean::StructType::Tuple => {
34043403
write!(w, "(");
34053404
for (i, field) in fields.iter().enumerate() {
34063405
if i > 0 {
@@ -3425,7 +3424,7 @@ fn render_struct(
34253424
}
34263425
write!(w, ";");
34273426
}
3428-
doctree::Unit => {
3427+
clean::StructType::Unit => {
34293428
// Needed for PhantomData.
34303429
if let Some(g) = g {
34313430
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })
@@ -4460,7 +4459,7 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
44604459
let fields = get_struct_fields_name(&s.fields);
44614460

44624461
if !fields.is_empty() {
4463-
if let doctree::Plain = s.struct_type {
4462+
if let clean::StructType::Plain = s.struct_type {
44644463
sidebar.push_str(&format!(
44654464
"<a class=\"sidebar-title\" href=\"#fields\">Fields</a>\
44664465
<div class=\"sidebar-links\">{}</div>",

src/librustdoc/json/conversions.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
99
use rustc_span::Pos;
1010

1111
use crate::clean;
12-
use crate::doctree;
1312
use crate::formats::item_type::ItemType;
1413
use crate::json::types::*;
1514
use crate::json::JsonRenderer;
@@ -210,9 +209,9 @@ impl From<clean::Struct> for Struct {
210209

211210
impl From<clean::Union> for Struct {
212211
fn from(struct_: clean::Union) -> Self {
213-
let clean::Union { struct_type, generics, fields, fields_stripped } = struct_;
212+
let clean::Union { generics, fields, fields_stripped } = struct_;
214213
Struct {
215-
struct_type: struct_type.into(),
214+
struct_type: StructType::Union,
216215
generics: generics.into(),
217216
fields_stripped,
218217
fields: ids(fields),
@@ -221,9 +220,9 @@ impl From<clean::Union> for Struct {
221220
}
222221
}
223222

224-
impl From<doctree::StructType> for StructType {
225-
fn from(struct_type: doctree::StructType) -> Self {
226-
use doctree::StructType::*;
223+
impl From<clean::StructType> for StructType {
224+
fn from(struct_type: clean::StructType) -> Self {
225+
use clean::StructType::*;
227226
match struct_type {
228227
Plain => StructType::Plain,
229228
Tuple => StructType::Tuple,

src/librustdoc/json/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ pub enum StructType {
269269
Plain,
270270
Tuple,
271271
Unit,
272+
Union,
272273
}
273274

274275
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]

0 commit comments

Comments
 (0)