From cf5246915b838b49778bc25cc68e8b1863a05362 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Sat, 27 Feb 2021 19:44:17 -0500 Subject: [PATCH 1/5] Remove Item::kind, use tagged enum. Rename variants to match --- src/librustdoc/json/conversions.rs | 52 ++++++++++++++---------------- src/librustdoc/json/mod.rs | 9 +++--- src/rustdoc-json-types/lib.rs | 48 +++++++++++++-------------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 47e4f3f0aa320..063d8aab1ec3e 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -23,7 +23,6 @@ use std::collections::HashSet; impl JsonRenderer<'_> { pub(super) fn convert_item(&self, item: clean::Item) -> Option { - let item_type = ItemType::from(&item); let deprecation = item.deprecation(self.tcx); let clean::Item { source, name, attrs, kind, visibility, def_id } = item; let inner = match *kind { @@ -50,7 +49,6 @@ impl JsonRenderer<'_> { .map(rustc_ast_pretty::pprust::attribute_to_string) .collect(), deprecation: deprecation.map(from_deprecation), - kind: item_type.into(), inner, }) } @@ -154,30 +152,30 @@ crate fn from_def_id(did: DefId) -> Id { fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option) -> ItemEnum { use clean::ItemKind::*; match item { - ModuleItem(m) => ItemEnum::ModuleItem(m.into()), - ImportItem(i) => ItemEnum::ImportItem(i.into()), - StructItem(s) => ItemEnum::StructItem(s.into()), - UnionItem(u) => ItemEnum::UnionItem(u.into()), - StructFieldItem(f) => ItemEnum::StructFieldItem(f.into()), - EnumItem(e) => ItemEnum::EnumItem(e.into()), - VariantItem(v) => ItemEnum::VariantItem(v.into()), - FunctionItem(f) => ItemEnum::FunctionItem(f.into()), - ForeignFunctionItem(f) => ItemEnum::FunctionItem(f.into()), - TraitItem(t) => ItemEnum::TraitItem(t.into()), - TraitAliasItem(t) => ItemEnum::TraitAliasItem(t.into()), - MethodItem(m, _) => ItemEnum::MethodItem(from_function_method(m, true)), - TyMethodItem(m) => ItemEnum::MethodItem(from_function_method(m, false)), - ImplItem(i) => ItemEnum::ImplItem(i.into()), - StaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)), - ForeignStaticItem(s) => ItemEnum::StaticItem(from_clean_static(s, tcx)), - ForeignTypeItem => ItemEnum::ForeignTypeItem, - TypedefItem(t, _) => ItemEnum::TypedefItem(t.into()), - OpaqueTyItem(t) => ItemEnum::OpaqueTyItem(t.into()), - ConstantItem(c) => ItemEnum::ConstantItem(c.into()), - MacroItem(m) => ItemEnum::MacroItem(m.source), - ProcMacroItem(m) => ItemEnum::ProcMacroItem(m.into()), - AssocConstItem(t, s) => ItemEnum::AssocConstItem { type_: t.into(), default: s }, - AssocTypeItem(g, t) => ItemEnum::AssocTypeItem { + ModuleItem(m) => ItemEnum::Module(m.into()), + ImportItem(i) => ItemEnum::Import(i.into()), + StructItem(s) => ItemEnum::Struct(s.into()), + UnionItem(u) => ItemEnum::Union(u.into()), + StructFieldItem(f) => ItemEnum::StructField(f.into()), + EnumItem(e) => ItemEnum::Enum(e.into()), + VariantItem(v) => ItemEnum::Variant(v.into()), + FunctionItem(f) => ItemEnum::Function(f.into()), + ForeignFunctionItem(f) => ItemEnum::Function(f.into()), + TraitItem(t) => ItemEnum::Trait(t.into()), + TraitAliasItem(t) => ItemEnum::TraitAlias(t.into()), + MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true)), + TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false)), + ImplItem(i) => ItemEnum::Impl(i.into()), + StaticItem(s) => ItemEnum::Static(from_clean_static(s, tcx)), + ForeignStaticItem(s) => ItemEnum::Static(from_clean_static(s, tcx)), + ForeignTypeItem => ItemEnum::ForeignType, + TypedefItem(t, _) => ItemEnum::Typedef(t.into()), + OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into()), + ConstantItem(c) => ItemEnum::Constant(c.into()), + MacroItem(m) => ItemEnum::Macro(m.source), + ProcMacroItem(m) => ItemEnum::ProcMacro(m.into()), + AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into(), default: s }, + AssocTypeItem(g, t) => ItemEnum::AssocType { bounds: g.into_iter().map(Into::into).collect(), default: t.map(Into::into), }, @@ -185,7 +183,7 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option { panic!("{:?} is not supported for JSON output", item) } - ExternCrateItem { ref src } => ItemEnum::ExternCrateItem { + ExternCrateItem { ref src } => ItemEnum::ExternCrate { name: name.as_ref().unwrap().to_string(), rename: src.map(|x| x.to_string()), }, diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 863610baed484..8b0c049c05bc7 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -108,8 +108,7 @@ impl JsonRenderer<'tcx> { .last() .map(Clone::clone), visibility: types::Visibility::Public, - kind: types::ItemKind::Trait, - inner: types::ItemEnum::TraitItem(trait_item.clone().into()), + inner: types::ItemEnum::Trait(trait_item.clone().into()), source: None, docs: Default::default(), links: Default::default(), @@ -158,11 +157,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let id = item.def_id; if let Some(mut new_item) = self.convert_item(item) { - if let types::ItemEnum::TraitItem(ref mut t) = new_item.inner { + if let types::ItemEnum::Trait(ref mut t) = new_item.inner { t.implementors = self.get_trait_implementors(id) - } else if let types::ItemEnum::StructItem(ref mut s) = new_item.inner { + } else if let types::ItemEnum::Struct(ref mut s) = new_item.inner { s.impls = self.get_impls(id) - } else if let types::ItemEnum::EnumItem(ref mut e) = new_item.inner { + } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { e.impls = self.get_impls(id) } let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone()); diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 6188b87d2c617..01f929c5d08ad 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -76,7 +76,7 @@ pub struct Item { /// Stringified versions of the attributes on this item (e.g. `"#[inline]"`) pub attrs: Vec, pub deprecation: Option, - pub kind: ItemKind, + #[serde(flatten)] pub inner: ItemEnum, } @@ -185,48 +185,48 @@ pub enum ItemKind { } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] -#[serde(untagged)] +#[serde(tag = "kind", content = "inner", rename_all = "snake_case")] pub enum ItemEnum { - ModuleItem(Module), - ExternCrateItem { + Module(Module), + ExternCrate { name: String, rename: Option, }, - ImportItem(Import), + Import(Import), - UnionItem(Union), - StructItem(Struct), - StructFieldItem(Type), - EnumItem(Enum), - VariantItem(Variant), + Union(Union), + Struct(Struct), + StructField(Type), + Enum(Enum), + Variant(Variant), - FunctionItem(Function), + Function(Function), - TraitItem(Trait), - TraitAliasItem(TraitAlias), - MethodItem(Method), - ImplItem(Impl), + Trait(Trait), + TraitAlias(TraitAlias), + Method(Method), + Impl(Impl), - TypedefItem(Typedef), - OpaqueTyItem(OpaqueTy), - ConstantItem(Constant), + Typedef(Typedef), + OpaqueTy(OpaqueTy), + Constant(Constant), - StaticItem(Static), + Static(Static), /// `type`s from an extern block - ForeignTypeItem, + ForeignType, /// Declarative macro_rules! macro - MacroItem(String), - ProcMacroItem(ProcMacro), + Macro(String), + ProcMacro(ProcMacro), - AssocConstItem { + AssocConst { #[serde(rename = "type")] type_: Type, /// e.g. `const X: usize = 5;` default: Option, }, - AssocTypeItem { + AssocType { bounds: Vec, /// e.g. `type X = usize;` default: Option, From ca48d1566ed54b7dd1718dfe877ef24711078538 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 4 Mar 2021 18:45:57 -0500 Subject: [PATCH 2/5] Add roundtrip testing and bump format version --- Cargo.lock | 3 +++ src/librustdoc/json/mod.rs | 2 +- src/rustdoc-json-types/Cargo.toml | 3 +++ src/rustdoc-json-types/lib.rs | 44 +++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9e773c572cc41..955f96d690308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.14.0" @@ -4568,6 +4570,7 @@ name = "rustdoc-json-types" version = "0.1.0" dependencies = [ "serde", + "serde_json", ] [[package]] diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 8b0c049c05bc7..2302dfdc80a84 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -242,7 +242,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { ) }) .collect(), - format_version: 4, + format_version: 5, }; let mut p = self.out_path.clone(); p.push(output.index.get(&output.root).unwrap().name.clone().unwrap()); diff --git a/src/rustdoc-json-types/Cargo.toml b/src/rustdoc-json-types/Cargo.toml index 7bba16a68b96c..9c02d5e645bfe 100644 --- a/src/rustdoc-json-types/Cargo.toml +++ b/src/rustdoc-json-types/Cargo.toml @@ -9,3 +9,6 @@ path = "lib.rs" [dependencies] serde = { version = "1.0", features = ["derive"] } + +[dev-dependencies] +serde_json = "1.0" diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 01f929c5d08ad..e898a01dfaf85 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -508,3 +508,47 @@ pub struct Static { pub mutable: bool, pub expr: String, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_struct_info_roundtrip() { + let s = ItemEnum::Struct(Struct { + struct_type: StructType::Plain, + generics: Generics { + params: vec![], + where_predicates: vec![] + }, + fields_stripped: false, + fields: vec![], + impls: vec![], + }); + + let struct_json = serde_json::to_string(&s).unwrap(); + + let de_s = serde_json::from_str(&struct_json).unwrap(); + + assert_eq!(s, de_s); + } + + #[test] + fn test_union_info_roundtrip() { + let u = ItemEnum::Union(Union { + generics: Generics { + params: vec![], + where_predicates: vec![] + }, + fields_stripped: false, + fields: vec![], + impls: vec![], + }); + + let union_json = serde_json::to_string(&u).unwrap(); + + let de_u = serde_json::from_str(&union_json).unwrap(); + + assert_eq!(u, de_u); + } +} From 83cece468e8e5b785fce0dad25807841b91f3321 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 4 Mar 2021 19:08:06 -0500 Subject: [PATCH 3/5] Move tests to own file --- src/rustdoc-json-types/lib.rs | 43 +-------------------------------- src/rustdoc-json-types/tests.rs | 40 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 src/rustdoc-json-types/tests.rs diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index e898a01dfaf85..4e7794fe61064 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -510,45 +510,4 @@ pub struct Static { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_struct_info_roundtrip() { - let s = ItemEnum::Struct(Struct { - struct_type: StructType::Plain, - generics: Generics { - params: vec![], - where_predicates: vec![] - }, - fields_stripped: false, - fields: vec![], - impls: vec![], - }); - - let struct_json = serde_json::to_string(&s).unwrap(); - - let de_s = serde_json::from_str(&struct_json).unwrap(); - - assert_eq!(s, de_s); - } - - #[test] - fn test_union_info_roundtrip() { - let u = ItemEnum::Union(Union { - generics: Generics { - params: vec![], - where_predicates: vec![] - }, - fields_stripped: false, - fields: vec![], - impls: vec![], - }); - - let union_json = serde_json::to_string(&u).unwrap(); - - let de_u = serde_json::from_str(&union_json).unwrap(); - - assert_eq!(u, de_u); - } -} +mod tests; diff --git a/src/rustdoc-json-types/tests.rs b/src/rustdoc-json-types/tests.rs new file mode 100644 index 0000000000000..4a5994f4c087c --- /dev/null +++ b/src/rustdoc-json-types/tests.rs @@ -0,0 +1,40 @@ +use super::*; + +#[test] +fn test_struct_info_roundtrip() { + let s = ItemEnum::Struct(Struct { + struct_type: StructType::Plain, + generics: Generics { + params: vec![], + where_predicates: vec![] + }, + fields_stripped: false, + fields: vec![], + impls: vec![], + }); + + let struct_json = serde_json::to_string(&s).unwrap(); + + let de_s = serde_json::from_str(&struct_json).unwrap(); + + assert_eq!(s, de_s); +} + +#[test] +fn test_union_info_roundtrip() { + let u = ItemEnum::Union(Union { + generics: Generics { + params: vec![], + where_predicates: vec![] + }, + fields_stripped: false, + fields: vec![], + impls: vec![], + }); + + let union_json = serde_json::to_string(&u).unwrap(); + + let de_u = serde_json::from_str(&union_json).unwrap(); + + assert_eq!(u, de_u); +} \ No newline at end of file From 70c9b370e822424de5c35b97bf49dffb5b8b9167 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 4 Mar 2021 19:44:09 -0500 Subject: [PATCH 4/5] x.py fmt --- src/rustdoc-json-types/tests.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/rustdoc-json-types/tests.rs b/src/rustdoc-json-types/tests.rs index 4a5994f4c087c..e7f6447ed8753 100644 --- a/src/rustdoc-json-types/tests.rs +++ b/src/rustdoc-json-types/tests.rs @@ -4,10 +4,7 @@ use super::*; fn test_struct_info_roundtrip() { let s = ItemEnum::Struct(Struct { struct_type: StructType::Plain, - generics: Generics { - params: vec![], - where_predicates: vec![] - }, + generics: Generics { params: vec![], where_predicates: vec![] }, fields_stripped: false, fields: vec![], impls: vec![], @@ -23,10 +20,7 @@ fn test_struct_info_roundtrip() { #[test] fn test_union_info_roundtrip() { let u = ItemEnum::Union(Union { - generics: Generics { - params: vec![], - where_predicates: vec![] - }, + generics: Generics { params: vec![], where_predicates: vec![] }, fields_stripped: false, fields: vec![], impls: vec![], @@ -37,4 +31,4 @@ fn test_union_info_roundtrip() { let de_u = serde_json::from_str(&union_json).unwrap(); assert_eq!(u, de_u); -} \ No newline at end of file +} From 18841ec342def87006b0535bbaa658d5fa43ba28 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Sat, 6 Mar 2021 15:37:07 -0500 Subject: [PATCH 5/5] Revert fmt version, add rustdoc-json-types to bootstrap tests --- Cargo.lock | 2 -- src/bootstrap/builder.rs | 1 + src/bootstrap/test.rs | 71 ++++++++++++++++++++++++++++++++++++++ src/librustdoc/json/mod.rs | 2 +- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 955f96d690308..328a220430601 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "addr2line" version = "0.14.0" diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index 9317c89625d06..22a1eb6370235 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -397,6 +397,7 @@ impl<'a> Builder<'a> { test::Crate, test::CrateLibrustc, test::CrateRustdoc, + test::CrateRustdocJsonTypes, test::Linkcheck, test::TierCheck, test::Cargotest, diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index c0cd24dd81f01..03ae684e23fed 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1922,6 +1922,77 @@ impl Step for CrateRustdoc { } } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct CrateRustdocJsonTypes { + host: TargetSelection, + test_kind: TestKind, +} + +impl Step for CrateRustdocJsonTypes { + type Output = (); + const DEFAULT: bool = true; + const ONLY_HOSTS: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/rustdoc-json-types") + } + + fn make_run(run: RunConfig<'_>) { + let builder = run.builder; + + let test_kind = builder.kind.into(); + + builder.ensure(CrateRustdocJsonTypes { host: run.target, test_kind }); + } + + fn run(self, builder: &Builder<'_>) { + let test_kind = self.test_kind; + let target = self.host; + + // Use the previous stage compiler to reuse the artifacts that are + // created when running compiletest for src/test/rustdoc. If this used + // `compiler`, then it would cause rustdoc to be built *again*, which + // isn't really necessary. + let compiler = builder.compiler_for(builder.top_stage, target, target); + builder.ensure(compile::Rustc { compiler, target }); + + let mut cargo = tool::prepare_tool_cargo( + builder, + compiler, + Mode::ToolRustc, + target, + test_kind.subcommand(), + "src/rustdoc-json-types", + SourceType::InTree, + &[], + ); + if test_kind.subcommand() == "test" && !builder.fail_fast { + cargo.arg("--no-fail-fast"); + } + + cargo.arg("-p").arg("rustdoc-json-types"); + + cargo.arg("--"); + cargo.args(&builder.config.cmd.test_args()); + + if self.host.contains("musl") { + cargo.arg("'-Ctarget-feature=-crt-static'"); + } + + if !builder.config.verbose_tests { + cargo.arg("--quiet"); + } + + builder.info(&format!( + "{} rustdoc-json-types stage{} ({} -> {})", + test_kind, compiler.stage, &compiler.host, target + )); + let _time = util::timeit(&builder); + + try_run(builder, &mut cargo.into()); + } +} + /// Some test suites are run inside emulators or on remote devices, and most /// of our test binaries are linked dynamically which means we need to ship /// the standard library and such to the emulator ahead of time. This step diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 2302dfdc80a84..8b0c049c05bc7 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -242,7 +242,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { ) }) .collect(), - format_version: 5, + format_version: 4, }; let mut p = self.out_path.clone(); p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());