Skip to content

Commit 0681951

Browse files
authored
Rollup merge of #82613 - CraftSpider:fix-de, r=aDotInTheVoid
Remove Item::kind, use tagged enum. Rename variants to match Fixes #82299, by making the ItemEnum tagged. Doesn't remove ItemKind as it's still used in other places. r? `````@jyn514````` `````@rustbot````` label: +A-rustdoc-json +T-rustdoc
2 parents 298c31b + 18841ec commit 0681951

File tree

8 files changed

+166
-56
lines changed

8 files changed

+166
-56
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4568,6 +4568,7 @@ name = "rustdoc-json-types"
45684568
version = "0.1.0"
45694569
dependencies = [
45704570
"serde",
4571+
"serde_json",
45714572
]
45724573

45734574
[[package]]

src/bootstrap/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl<'a> Builder<'a> {
397397
test::Crate,
398398
test::CrateLibrustc,
399399
test::CrateRustdoc,
400+
test::CrateRustdocJsonTypes,
400401
test::Linkcheck,
401402
test::TierCheck,
402403
test::Cargotest,

src/bootstrap/test.rs

+71
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,77 @@ impl Step for CrateRustdoc {
19221922
}
19231923
}
19241924

1925+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1926+
pub struct CrateRustdocJsonTypes {
1927+
host: TargetSelection,
1928+
test_kind: TestKind,
1929+
}
1930+
1931+
impl Step for CrateRustdocJsonTypes {
1932+
type Output = ();
1933+
const DEFAULT: bool = true;
1934+
const ONLY_HOSTS: bool = true;
1935+
1936+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1937+
run.path("src/rustdoc-json-types")
1938+
}
1939+
1940+
fn make_run(run: RunConfig<'_>) {
1941+
let builder = run.builder;
1942+
1943+
let test_kind = builder.kind.into();
1944+
1945+
builder.ensure(CrateRustdocJsonTypes { host: run.target, test_kind });
1946+
}
1947+
1948+
fn run(self, builder: &Builder<'_>) {
1949+
let test_kind = self.test_kind;
1950+
let target = self.host;
1951+
1952+
// Use the previous stage compiler to reuse the artifacts that are
1953+
// created when running compiletest for src/test/rustdoc. If this used
1954+
// `compiler`, then it would cause rustdoc to be built *again*, which
1955+
// isn't really necessary.
1956+
let compiler = builder.compiler_for(builder.top_stage, target, target);
1957+
builder.ensure(compile::Rustc { compiler, target });
1958+
1959+
let mut cargo = tool::prepare_tool_cargo(
1960+
builder,
1961+
compiler,
1962+
Mode::ToolRustc,
1963+
target,
1964+
test_kind.subcommand(),
1965+
"src/rustdoc-json-types",
1966+
SourceType::InTree,
1967+
&[],
1968+
);
1969+
if test_kind.subcommand() == "test" && !builder.fail_fast {
1970+
cargo.arg("--no-fail-fast");
1971+
}
1972+
1973+
cargo.arg("-p").arg("rustdoc-json-types");
1974+
1975+
cargo.arg("--");
1976+
cargo.args(&builder.config.cmd.test_args());
1977+
1978+
if self.host.contains("musl") {
1979+
cargo.arg("'-Ctarget-feature=-crt-static'");
1980+
}
1981+
1982+
if !builder.config.verbose_tests {
1983+
cargo.arg("--quiet");
1984+
}
1985+
1986+
builder.info(&format!(
1987+
"{} rustdoc-json-types stage{} ({} -> {})",
1988+
test_kind, compiler.stage, &compiler.host, target
1989+
));
1990+
let _time = util::timeit(&builder);
1991+
1992+
try_run(builder, &mut cargo.into());
1993+
}
1994+
}
1995+
19251996
/// Some test suites are run inside emulators or on remote devices, and most
19261997
/// of our test binaries are linked dynamically which means we need to ship
19271998
/// the standard library and such to the emulator ahead of time. This step

src/librustdoc/json/conversions.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::collections::HashSet;
2323

2424
impl JsonRenderer<'_> {
2525
pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
26-
let item_type = ItemType::from(&item);
2726
let deprecation = item.deprecation(self.tcx);
2827
let clean::Item { source, name, attrs, kind, visibility, def_id } = item;
2928
let inner = match *kind {
@@ -50,7 +49,6 @@ impl JsonRenderer<'_> {
5049
.map(rustc_ast_pretty::pprust::attribute_to_string)
5150
.collect(),
5251
deprecation: deprecation.map(from_deprecation),
53-
kind: item_type.into(),
5452
inner,
5553
})
5654
}
@@ -154,38 +152,38 @@ crate fn from_def_id(did: DefId) -> Id {
154152
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Symbol>) -> ItemEnum {
155153
use clean::ItemKind::*;
156154
match item {
157-
ModuleItem(m) => ItemEnum::ModuleItem(m.into()),
158-
ImportItem(i) => ItemEnum::ImportItem(i.into()),
159-
StructItem(s) => ItemEnum::StructItem(s.into()),
160-
UnionItem(u) => ItemEnum::UnionItem(u.into()),
161-
StructFieldItem(f) => ItemEnum::StructFieldItem(f.into()),
162-
EnumItem(e) => ItemEnum::EnumItem(e.into()),
163-
VariantItem(v) => ItemEnum::VariantItem(v.into()),
164-
FunctionItem(f) => ItemEnum::FunctionItem(f.into()),
165-
ForeignFunctionItem(f) => ItemEnum::FunctionItem(f.into()),
166-
TraitItem(t) => ItemEnum::TraitItem(t.into()),
167-
TraitAliasItem(t) => ItemEnum::TraitAliasItem(t.into()),
168-
MethodItem(m, _) => ItemEnum::MethodItem(from_function_method(m, true)),
169-
TyMethodItem(m) => ItemEnum::MethodItem(from_function_method(m, false)),
170-
ImplItem(i) => ItemEnum::ImplItem(i.into()),
171-
StaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)),
172-
ForeignStaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)),
173-
ForeignTypeItem => ItemEnum::ForeignTypeItem,
174-
TypedefItem(t, _) => ItemEnum::TypedefItem(t.into()),
175-
OpaqueTyItem(t) => ItemEnum::OpaqueTyItem(t.into()),
176-
ConstantItem(c) => ItemEnum::ConstantItem(c.into()),
177-
MacroItem(m) => ItemEnum::MacroItem(m.source),
178-
ProcMacroItem(m) => ItemEnum::ProcMacroItem(m.into()),
179-
AssocConstItem(t, s) => ItemEnum::AssocConstItem { type_: t.into(), default: s },
180-
AssocTypeItem(g, t) => ItemEnum::AssocTypeItem {
155+
ModuleItem(m) => ItemEnum::Module(m.into()),
156+
ImportItem(i) => ItemEnum::Import(i.into()),
157+
StructItem(s) => ItemEnum::Struct(s.into()),
158+
UnionItem(u) => ItemEnum::Union(u.into()),
159+
StructFieldItem(f) => ItemEnum::StructField(f.into()),
160+
EnumItem(e) => ItemEnum::Enum(e.into()),
161+
VariantItem(v) => ItemEnum::Variant(v.into()),
162+
FunctionItem(f) => ItemEnum::Function(f.into()),
163+
ForeignFunctionItem(f) => ItemEnum::Function(f.into()),
164+
TraitItem(t) => ItemEnum::Trait(t.into()),
165+
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into()),
166+
MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true)),
167+
TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false)),
168+
ImplItem(i) => ItemEnum::Impl(i.into()),
169+
StaticItem(s) => ItemEnum::Static(from_clean_static(s, tcx)),
170+
ForeignStaticItem(s) => ItemEnum::Static(from_clean_static(s, tcx)),
171+
ForeignTypeItem => ItemEnum::ForeignType,
172+
TypedefItem(t, _) => ItemEnum::Typedef(t.into()),
173+
OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into()),
174+
ConstantItem(c) => ItemEnum::Constant(c.into()),
175+
MacroItem(m) => ItemEnum::Macro(m.source),
176+
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into()),
177+
AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into(), default: s },
178+
AssocTypeItem(g, t) => ItemEnum::AssocType {
181179
bounds: g.into_iter().map(Into::into).collect(),
182180
default: t.map(Into::into),
183181
},
184182
StrippedItem(inner) => from_clean_item_kind(*inner, tcx, name),
185183
PrimitiveItem(_) | KeywordItem(_) => {
186184
panic!("{:?} is not supported for JSON output", item)
187185
}
188-
ExternCrateItem { ref src } => ItemEnum::ExternCrateItem {
186+
ExternCrateItem { ref src } => ItemEnum::ExternCrate {
189187
name: name.as_ref().unwrap().to_string(),
190188
rename: src.map(|x| x.to_string()),
191189
},

src/librustdoc/json/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ impl JsonRenderer<'tcx> {
108108
.last()
109109
.map(Clone::clone),
110110
visibility: types::Visibility::Public,
111-
kind: types::ItemKind::Trait,
112-
inner: types::ItemEnum::TraitItem(trait_item.clone().into()),
111+
inner: types::ItemEnum::Trait(trait_item.clone().into()),
113112
source: None,
114113
docs: Default::default(),
115114
links: Default::default(),
@@ -158,11 +157,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
158157

159158
let id = item.def_id;
160159
if let Some(mut new_item) = self.convert_item(item) {
161-
if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner {
160+
if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
162161
t.implementors = self.get_trait_implementors(id)
163-
} else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner {
162+
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
164163
s.impls = self.get_impls(id)
165-
} else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner {
164+
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
166165
e.impls = self.get_impls(id)
167166
}
168167
let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());

src/rustdoc-json-types/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ path = "lib.rs"
99

1010
[dependencies]
1111
serde = { version = "1.0", features = ["derive"] }
12+
13+
[dev-dependencies]
14+
serde_json = "1.0"

src/rustdoc-json-types/lib.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub struct Item {
7676
/// Stringified versions of the attributes on this item (e.g. `"#[inline]"`)
7777
pub attrs: Vec<String>,
7878
pub deprecation: Option<Deprecation>,
79-
pub kind: ItemKind,
79+
#[serde(flatten)]
8080
pub inner: ItemEnum,
8181
}
8282

@@ -185,48 +185,48 @@ pub enum ItemKind {
185185
}
186186

187187
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
188-
#[serde(untagged)]
188+
#[serde(tag = "kind", content = "inner", rename_all = "snake_case")]
189189
pub enum ItemEnum {
190-
ModuleItem(Module),
191-
ExternCrateItem {
190+
Module(Module),
191+
ExternCrate {
192192
name: String,
193193
rename: Option<String>,
194194
},
195-
ImportItem(Import),
195+
Import(Import),
196196

197-
UnionItem(Union),
198-
StructItem(Struct),
199-
StructFieldItem(Type),
200-
EnumItem(Enum),
201-
VariantItem(Variant),
197+
Union(Union),
198+
Struct(Struct),
199+
StructField(Type),
200+
Enum(Enum),
201+
Variant(Variant),
202202

203-
FunctionItem(Function),
203+
Function(Function),
204204

205-
TraitItem(Trait),
206-
TraitAliasItem(TraitAlias),
207-
MethodItem(Method),
208-
ImplItem(Impl),
205+
Trait(Trait),
206+
TraitAlias(TraitAlias),
207+
Method(Method),
208+
Impl(Impl),
209209

210-
TypedefItem(Typedef),
211-
OpaqueTyItem(OpaqueTy),
212-
ConstantItem(Constant),
210+
Typedef(Typedef),
211+
OpaqueTy(OpaqueTy),
212+
Constant(Constant),
213213

214-
StaticItem(Static),
214+
Static(Static),
215215

216216
/// `type`s from an extern block
217-
ForeignTypeItem,
217+
ForeignType,
218218

219219
/// Declarative macro_rules! macro
220-
MacroItem(String),
221-
ProcMacroItem(ProcMacro),
220+
Macro(String),
221+
ProcMacro(ProcMacro),
222222

223-
AssocConstItem {
223+
AssocConst {
224224
#[serde(rename = "type")]
225225
type_: Type,
226226
/// e.g. `const X: usize = 5;`
227227
default: Option<String>,
228228
},
229-
AssocTypeItem {
229+
AssocType {
230230
bounds: Vec<GenericBound>,
231231
/// e.g. `type X = usize;`
232232
default: Option<Type>,
@@ -508,3 +508,6 @@ pub struct Static {
508508
pub mutable: bool,
509509
pub expr: String,
510510
}
511+
512+
#[cfg(test)]
513+
mod tests;

src/rustdoc-json-types/tests.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_struct_info_roundtrip() {
5+
let s = ItemEnum::Struct(Struct {
6+
struct_type: StructType::Plain,
7+
generics: Generics { params: vec![], where_predicates: vec![] },
8+
fields_stripped: false,
9+
fields: vec![],
10+
impls: vec![],
11+
});
12+
13+
let struct_json = serde_json::to_string(&s).unwrap();
14+
15+
let de_s = serde_json::from_str(&struct_json).unwrap();
16+
17+
assert_eq!(s, de_s);
18+
}
19+
20+
#[test]
21+
fn test_union_info_roundtrip() {
22+
let u = ItemEnum::Union(Union {
23+
generics: Generics { params: vec![], where_predicates: vec![] },
24+
fields_stripped: false,
25+
fields: vec![],
26+
impls: vec![],
27+
});
28+
29+
let union_json = serde_json::to_string(&u).unwrap();
30+
31+
let de_u = serde_json::from_str(&union_json).unwrap();
32+
33+
assert_eq!(u, de_u);
34+
}

0 commit comments

Comments
 (0)