Skip to content

Commit 2ceee72

Browse files
authored
Rollup merge of #81227 - CraftSpider:struct-type-clean, r=jyn514
Remove doctree::StructType Also removes it from the Union type, as unions can only ever be 'Plain'. Adds a new StructType to JSON, 'union', as the easiest way to encode the type of a union there. This leaves only one item in doctree, `Module`. r? `@jyn514`
2 parents 1cc13b4 + 3349b40 commit 2ceee72

File tree

7 files changed

+24
-53
lines changed

7 files changed

+24
-53
lines changed

src/librustdoc/clean/inline.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::iter::once;
55
use rustc_ast as ast;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir as hir;
8-
use rustc_hir::def::{CtorKind, DefKind, Res};
8+
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
1010
use rustc_hir::Mutability;
1111
use rustc_metadata::creader::LoadedMacro;
@@ -17,7 +17,6 @@ use rustc_span::Span;
1717

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

2221
use super::Clean;
2322

@@ -246,11 +245,7 @@ fn build_struct(cx: &DocContext<'_>, did: DefId) -> clean::Struct {
246245
let variant = cx.tcx.adt_def(did).non_enum_variant();
247246

248247
clean::Struct {
249-
struct_type: match variant.ctor_kind {
250-
CtorKind::Fictive => doctree::Plain,
251-
CtorKind::Fn => doctree::Tuple,
252-
CtorKind::Const => doctree::Unit,
253-
},
248+
struct_type: variant.ctor_kind,
254249
generics: (cx.tcx.generics_of(did), predicates).clean(cx),
255250
fields: variant.fields.clean(cx),
256251
fields_stripped: false,
@@ -262,7 +257,6 @@ fn build_union(cx: &DocContext<'_>, did: DefId) -> clean::Union {
262257
let variant = cx.tcx.adt_def(did).non_enum_variant();
263258

264259
clean::Union {
265-
struct_type: doctree::Plain,
266260
generics: (cx.tcx.generics_of(did), predicates).clean(cx),
267261
fields: variant.fields.clean(cx),
268262
fields_stripped: false,

src/librustdoc/clean/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ impl Clean<Visibility> for ty::Visibility {
18271827
impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
18281828
fn clean(&self, cx: &DocContext<'_>) -> VariantStruct {
18291829
VariantStruct {
1830-
struct_type: doctree::struct_type_from_def(self),
1830+
struct_type: CtorKind::from_hir(self),
18311831
fields: self.fields().iter().map(|x| x.clean(cx)).collect(),
18321832
fields_stripped: false,
18331833
}
@@ -1842,7 +1842,7 @@ impl Clean<Item> for ty::VariantDef {
18421842
self.fields.iter().map(|f| cx.tcx.type_of(f.did).clean(cx)).collect(),
18431843
),
18441844
CtorKind::Fictive => Variant::Struct(VariantStruct {
1845-
struct_type: doctree::Plain,
1845+
struct_type: CtorKind::Fictive,
18461846
fields_stripped: false,
18471847
fields: self
18481848
.fields
@@ -1996,13 +1996,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
19961996
bounds: bounds.clean(cx),
19971997
}),
19981998
ItemKind::Union(ref variant_data, ref generics) => UnionItem(Union {
1999-
struct_type: doctree::struct_type_from_def(&variant_data),
20001999
generics: generics.clean(cx),
20012000
fields: variant_data.fields().clean(cx),
20022001
fields_stripped: false,
20032002
}),
20042003
ItemKind::Struct(ref variant_data, ref generics) => StructItem(Struct {
2005-
struct_type: doctree::struct_type_from_def(&variant_data),
2004+
struct_type: CtorKind::from_hir(variant_data),
20062005
generics: generics.clean(cx),
20072006
fields: variant_data.fields().clean(cx),
20082007
fields_stripped: false,

src/librustdoc/clean/types.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
1616
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1717
use rustc_feature::UnstableFeatures;
1818
use rustc_hir as hir;
19-
use rustc_hir::def::Res;
19+
use rustc_hir::def::{CtorKind, Res};
2020
use rustc_hir::def_id::{CrateNum, DefId};
2121
use rustc_hir::lang_items::LangItem;
2222
use rustc_hir::Mutability;
@@ -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;
@@ -1685,15 +1684,14 @@ impl Visibility {
16851684

16861685
#[derive(Clone, Debug)]
16871686
crate struct Struct {
1688-
crate struct_type: doctree::StructType,
1687+
crate struct_type: CtorKind,
16891688
crate generics: Generics,
16901689
crate fields: Vec<Item>,
16911690
crate fields_stripped: bool,
16921691
}
16931692

16941693
#[derive(Clone, Debug)]
16951694
crate struct Union {
1696-
crate struct_type: doctree::StructType,
16971695
crate generics: Generics,
16981696
crate fields: Vec<Item>,
16991697
crate fields_stripped: bool,
@@ -1704,7 +1702,7 @@ crate struct Union {
17041702
/// only as a variant in an enum.
17051703
#[derive(Clone, Debug)]
17061704
crate struct VariantStruct {
1707-
crate struct_type: doctree::StructType,
1705+
crate struct_type: CtorKind,
17081706
crate fields: Vec<Item>,
17091707
crate fields_stripped: bool,
17101708
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use rustc_attr::{Deprecation, StabilityLevel};
5252
use rustc_data_structures::flock;
5353
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5454
use rustc_hir as hir;
55+
use rustc_hir::def::CtorKind;
5556
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
5657
use rustc_hir::Mutability;
5758
use rustc_middle::middle::stability;
@@ -67,7 +68,6 @@ use serde::{Serialize, Serializer};
6768
use crate::clean::{self, AttributesExt, GetDefId, RenderedLink, SelfTy, TypeKind};
6869
use crate::config::{RenderInfo, RenderOptions};
6970
use crate::docfs::{DocFS, PathError};
70-
use crate::doctree;
7171
use crate::error::Error;
7272
use crate::formats::cache::{cache, Cache};
7373
use crate::formats::item_type::ItemType;
@@ -3103,7 +3103,7 @@ fn item_struct(
31033103
_ => None,
31043104
})
31053105
.peekable();
3106-
if let doctree::Plain = s.struct_type {
3106+
if let CtorKind::Fictive = s.struct_type {
31073107
if fields.peek().is_some() {
31083108
write!(
31093109
w,
@@ -3353,7 +3353,7 @@ fn render_struct(
33533353
w: &mut Buffer,
33543354
it: &clean::Item,
33553355
g: Option<&clean::Generics>,
3356-
ty: doctree::StructType,
3356+
ty: CtorKind,
33573357
fields: &[clean::Item],
33583358
tab: &str,
33593359
structhead: bool,
@@ -3370,7 +3370,7 @@ fn render_struct(
33703370
write!(w, "{}", g.print())
33713371
}
33723372
match ty {
3373-
doctree::Plain => {
3373+
CtorKind::Fictive => {
33743374
if let Some(g) = g {
33753375
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: true })
33763376
}
@@ -3402,7 +3402,7 @@ fn render_struct(
34023402
}
34033403
write!(w, "}}");
34043404
}
3405-
doctree::Tuple => {
3405+
CtorKind::Fn => {
34063406
write!(w, "(");
34073407
for (i, field) in fields.iter().enumerate() {
34083408
if i > 0 {
@@ -3427,7 +3427,7 @@ fn render_struct(
34273427
}
34283428
write!(w, ";");
34293429
}
3430-
doctree::Unit => {
3430+
CtorKind::Const => {
34313431
// Needed for PhantomData.
34323432
if let Some(g) = g {
34333433
write!(w, "{}", WhereClause { gens: g, indent: 0, end_newline: false })
@@ -4462,7 +4462,7 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
44624462
let fields = get_struct_fields_name(&s.fields);
44634463

44644464
if !fields.is_empty() {
4465-
if let doctree::Plain = s.struct_type {
4465+
if let CtorKind::Fictive = s.struct_type {
44664466
sidebar.push_str(&format!(
44674467
"<a class=\"sidebar-title\" href=\"#fields\">Fields</a>\
44684468
<div class=\"sidebar-links\">{}</div>",

src/librustdoc/json/conversions.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use std::convert::From;
66

77
use rustc_ast::ast;
8+
use rustc_hir::def::CtorKind;
89
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
910
use rustc_span::Pos;
1011

1112
use crate::clean;
12-
use crate::doctree;
1313
use crate::formats::item_type::ItemType;
1414
use crate::json::types::*;
1515
use crate::json::JsonRenderer;
@@ -210,9 +210,9 @@ impl From<clean::Struct> for Struct {
210210

211211
impl From<clean::Union> for Struct {
212212
fn from(struct_: clean::Union) -> Self {
213-
let clean::Union { struct_type, generics, fields, fields_stripped } = struct_;
213+
let clean::Union { generics, fields, fields_stripped } = struct_;
214214
Struct {
215-
struct_type: struct_type.into(),
215+
struct_type: StructType::Union,
216216
generics: generics.into(),
217217
fields_stripped,
218218
fields: ids(fields),
@@ -221,13 +221,12 @@ impl From<clean::Union> for Struct {
221221
}
222222
}
223223

224-
impl From<doctree::StructType> for StructType {
225-
fn from(struct_type: doctree::StructType) -> Self {
226-
use doctree::StructType::*;
224+
impl From<CtorKind> for StructType {
225+
fn from(struct_type: CtorKind) -> Self {
227226
match struct_type {
228-
Plain => StructType::Plain,
229-
Tuple => StructType::Tuple,
230-
Unit => StructType::Unit,
227+
CtorKind::Fictive => StructType::Plain,
228+
CtorKind::Fn => StructType::Tuple,
229+
CtorKind::Const => StructType::Unit,
231230
}
232231
}
233232
}

src/librustdoc/json/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub enum StructType {
270270
Plain,
271271
Tuple,
272272
Unit,
273+
Union,
273274
}
274275

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

0 commit comments

Comments
 (0)