From aa763fcf421e627455aa1de16df1292c8e1bcb9d Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sat, 19 Feb 2022 09:50:35 +0100 Subject: [PATCH 1/2] rustdoc-json: Include GenericParamDefKind::Type::synthetic in JSON The rustdoc JSON for ``` pub fn f(_: impl Clone) {} ``` will effectively be ``` pub fn f(_: impl Clone) ``` where a synthetic generic parameter called `impl Clone` with generic trait bound `Clone` is added to the function declaration. The generated HTML filters out these generic parameters by doing `self.params.iter().filter(|p| !p.is_synthetic_type_param())`, because the synthetic generic parameter is not of interest to regular users. For the same reason, we should expose whether or not a generic parameter is synthetic or not also in the rustdoc JSON, so that rustdoc JSON clients can also have the option to hide synthetic generic parameters. --- src/librustdoc/json/conversions.rs | 3 +- src/rustdoc-json-types/lib.rs | 40 ++++++++++++++++++++++++--- src/test/rustdoc-json/fns/generics.rs | 5 ++++ 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/test/rustdoc-json/fns/generics.rs diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 4358dc8980f..7ffcfada5c0 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -340,9 +340,10 @@ fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self { Lifetime { outlives } => GenericParamDefKind::Lifetime { outlives: outlives.into_iter().map(|lt| lt.0.to_string()).collect(), }, - Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type { + Type { did: _, bounds, default, synthetic } => GenericParamDefKind::Type { bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(), default: default.map(|x| (*x).into_tcx(tcx)), + synthetic, }, Const { did: _, ty, default } => { GenericParamDefKind::Const { ty: (*ty).into_tcx(tcx), default: default.map(|x| *x) } diff --git a/src/rustdoc-json-types/lib.rs b/src/rustdoc-json-types/lib.rs index 40b0de44829..7df880a4ed8 100644 --- a/src/rustdoc-json-types/lib.rs +++ b/src/rustdoc-json-types/lib.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; /// rustdoc format-version. -pub const FORMAT_VERSION: u32 = 12; +pub const FORMAT_VERSION: u32 = 13; /// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information /// about the language items in the local crate, as well as info about external items to allow @@ -346,9 +346,41 @@ pub struct GenericParamDef { #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "snake_case")] pub enum GenericParamDefKind { - Lifetime { outlives: Vec }, - Type { bounds: Vec, default: Option }, - Const { ty: Type, default: Option }, + Lifetime { + outlives: Vec, + }, + Type { + bounds: Vec, + default: Option, + /// This is normally `false`, which means that this generic parameter is + /// declared in the Rust source text. + /// + /// If it is `true`, this generic parameter has been introduced by the + /// compiler behind the scenes. + /// + /// # Example + /// + /// Consider + /// + /// ```ignore (pseudo-rust) + /// pub fn f(_: impl Trait) {} + /// ``` + /// + /// The compiler will transform this behind the scenes to + /// + /// ```ignore (pseudo-rust) + /// pub fn f(_: impl Trait) {} + /// ``` + /// + /// In this example, the generic parameter named `impl Trait` (and which + /// is bound by `Trait`) is synthetic, because it was not originally in + /// the Rust source text. + synthetic: bool, + }, + Const { + ty: Type, + default: Option, + }, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] diff --git a/src/test/rustdoc-json/fns/generics.rs b/src/test/rustdoc-json/fns/generics.rs new file mode 100644 index 00000000000..f80c380337b --- /dev/null +++ b/src/test/rustdoc-json/fns/generics.rs @@ -0,0 +1,5 @@ +// @has generics.json "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false +pub fn one_generic_param_fn(_: T) {} + +// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true +pub fn one_synthetic_generic_param_fn(_: impl Clone) {} From a424f42e97f2d1696f51a4bb71f4d305dedeb847 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Fri, 25 Feb 2022 20:34:00 +0100 Subject: [PATCH 2/2] rustdoc-json: Make the `fns/generics.rs` test much more robust --- src/test/rustdoc-json/fns/generics.rs | 29 +++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/test/rustdoc-json/fns/generics.rs b/src/test/rustdoc-json/fns/generics.rs index f80c380337b..e777fabaa52 100644 --- a/src/test/rustdoc-json/fns/generics.rs +++ b/src/test/rustdoc-json/fns/generics.rs @@ -1,5 +1,26 @@ -// @has generics.json "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false -pub fn one_generic_param_fn(_: T) {} +// ignore-tidy-linelength -// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true -pub fn one_synthetic_generic_param_fn(_: impl Clone) {} +#![feature(no_core)] +#![no_core] + +// @set wham_id = generics.json "$.index[*][?(@.name=='Wham')].id" +pub trait Wham {} + +// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.where_predicates" [] +// @count - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[*]" 1 +// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].name" '"T"' +// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false +// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id +// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.decl.inputs" '[["w", {"inner": "T", "kind": "generic"}]]' +pub fn one_generic_param_fn(w: T) {} + +// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.where_predicates" [] +// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[*]" 1 +// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].name" '"impl Wham"' +// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true +// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id +// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[*]" 1 +// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][0]" '"w"' +// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].kind" '"impl_trait"' +// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.inner.id" $wham_id +pub fn one_synthetic_generic_param_fn(w: impl Wham) {}