From 8e40d936473c41342e935ab323d755210af285e0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 22 Jun 2022 16:26:04 +0200 Subject: [PATCH 1/2] Filter out keyword items in rustdoc JSON output --- src/librustdoc/json/conversions.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 4fde63c99d4b9..c627dcc30d667 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -43,7 +43,7 @@ impl JsonRenderer<'_> { let span = item.span(self.tcx); let clean::Item { name, attrs: _, kind: _, visibility, item_id, cfg: _ } = item; let inner = match *item.kind { - clean::StrippedItem(_) => return None, + clean::StrippedItem(_) | clean::KeywordItem(_) => return None, _ => from_clean_item(item, self.tcx), }; Some(Item { @@ -254,11 +254,8 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum { }, // FIXME: do not map to Typedef but to a custom variant AssocTypeItem(t, _) => ItemEnum::Typedef(t.into_tcx(tcx)), - // `convert_item` early returns `None` for striped items - StrippedItem(_) => unreachable!(), - KeywordItem(_) => { - panic!("{:?} is not supported for JSON output", item) - } + // `convert_item` early returns `None` for striped items and keywords. + StrippedItem(_) | KeywordItem(_) => unreachable!(), ExternCrateItem { ref src } => ItemEnum::ExternCrate { name: name.as_ref().unwrap().to_string(), rename: src.map(|x| x.to_string()), @@ -764,7 +761,7 @@ impl FromWithTcx for ItemKind { fn ids(items: impl IntoIterator, tcx: TyCtxt<'_>) -> Vec { items .into_iter() - .filter(|x| !x.is_stripped()) + .filter(|x| !x.is_stripped() && !x.is_keyword()) .map(|i| from_item_id_with_name(i.item_id, tcx, i.name)) .collect() } From 75ad2f7a76170943aaf543c74097927e57a89f32 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 22 Jun 2022 16:26:22 +0200 Subject: [PATCH 2/2] Add test for keywords in rustdoc JSON output --- src/test/rustdoc-json/keyword.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/rustdoc-json/keyword.rs diff --git a/src/test/rustdoc-json/keyword.rs b/src/test/rustdoc-json/keyword.rs new file mode 100644 index 0000000000000..78a843aca7b95 --- /dev/null +++ b/src/test/rustdoc-json/keyword.rs @@ -0,0 +1,21 @@ +// Regression test for . + +// Keywords should not be generated in rustdoc JSON output and this test +// ensures it. + +#![feature(rustdoc_internals)] +#![no_std] + +// @has keyword.json +// @!has - "$.index[*][?(@.name=='match')]" +// @has - "$.index[*][?(@.name=='foo')]" + +#[doc(keyword = "match")] +/// this is a test! +pub mod foo {} + +// @!has - "$.index[*][?(@.name=='hello')]" +// @!has - "$.index[*][?(@.name=='bar')]" +#[doc(keyword = "hello")] +/// hello +mod bar {}