Skip to content

Commit aa763fc

Browse files
committed
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: Clone>(_: 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.
1 parent 6d76841 commit aa763fc

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/librustdoc/json/conversions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,10 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
340340
Lifetime { outlives } => GenericParamDefKind::Lifetime {
341341
outlives: outlives.into_iter().map(|lt| lt.0.to_string()).collect(),
342342
},
343-
Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
343+
Type { did: _, bounds, default, synthetic } => GenericParamDefKind::Type {
344344
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
345345
default: default.map(|x| (*x).into_tcx(tcx)),
346+
synthetic,
346347
},
347348
Const { did: _, ty, default } => {
348349
GenericParamDefKind::Const { ty: (*ty).into_tcx(tcx), default: default.map(|x| *x) }

src/rustdoc-json-types/lib.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::PathBuf;
99
use serde::{Deserialize, Serialize};
1010

1111
/// rustdoc format-version.
12-
pub const FORMAT_VERSION: u32 = 12;
12+
pub const FORMAT_VERSION: u32 = 13;
1313

1414
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
1515
/// about the language items in the local crate, as well as info about external items to allow
@@ -346,9 +346,41 @@ pub struct GenericParamDef {
346346
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
347347
#[serde(rename_all = "snake_case")]
348348
pub enum GenericParamDefKind {
349-
Lifetime { outlives: Vec<String> },
350-
Type { bounds: Vec<GenericBound>, default: Option<Type> },
351-
Const { ty: Type, default: Option<String> },
349+
Lifetime {
350+
outlives: Vec<String>,
351+
},
352+
Type {
353+
bounds: Vec<GenericBound>,
354+
default: Option<Type>,
355+
/// This is normally `false`, which means that this generic parameter is
356+
/// declared in the Rust source text.
357+
///
358+
/// If it is `true`, this generic parameter has been introduced by the
359+
/// compiler behind the scenes.
360+
///
361+
/// # Example
362+
///
363+
/// Consider
364+
///
365+
/// ```ignore (pseudo-rust)
366+
/// pub fn f(_: impl Trait) {}
367+
/// ```
368+
///
369+
/// The compiler will transform this behind the scenes to
370+
///
371+
/// ```ignore (pseudo-rust)
372+
/// pub fn f<impl Trait: Trait>(_: impl Trait) {}
373+
/// ```
374+
///
375+
/// In this example, the generic parameter named `impl Trait` (and which
376+
/// is bound by `Trait`) is synthetic, because it was not originally in
377+
/// the Rust source text.
378+
synthetic: bool,
379+
},
380+
Const {
381+
ty: Type,
382+
default: Option<String>,
383+
},
352384
}
353385

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

src/test/rustdoc-json/fns/generics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @has generics.json "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false
2+
pub fn one_generic_param_fn<T>(_: T) {}
3+
4+
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true
5+
pub fn one_synthetic_generic_param_fn(_: impl Clone) {}

0 commit comments

Comments
 (0)