Skip to content

Commit 27e674d

Browse files
authored
Rollup merge of #94150 - Enselic:synthetic-generic-parameters-in-json, r=CraftSpider
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 paramter 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 syntehtic generic parameters. `@rustbot` modify labels: +A-rustdoc-json
2 parents 2c6a29a + a424f42 commit 27e674d

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ignore-tidy-linelength
2+
3+
#![feature(no_core)]
4+
#![no_core]
5+
6+
// @set wham_id = generics.json "$.index[*][?(@.name=='Wham')].id"
7+
pub trait Wham {}
8+
9+
// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.where_predicates" []
10+
// @count - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[*]" 1
11+
// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].name" '"T"'
12+
// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" false
13+
// @has - "$.index[*][?(@.name=='one_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id
14+
// @is - "$.index[*][?(@.name=='one_generic_param_fn')].inner.decl.inputs" '[["w", {"inner": "T", "kind": "generic"}]]'
15+
pub fn one_generic_param_fn<T: Wham>(w: T) {}
16+
17+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.where_predicates" []
18+
// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[*]" 1
19+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].name" '"impl Wham"'
20+
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.synthetic" true
21+
// @has - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.generics.params[0].kind.type.bounds[0].trait_bound.trait.inner.id" $wham_id
22+
// @count - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[*]" 1
23+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][0]" '"w"'
24+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].kind" '"impl_trait"'
25+
// @is - "$.index[*][?(@.name=='one_synthetic_generic_param_fn')].inner.decl.inputs[0][1].inner[0].trait_bound.trait.inner.id" $wham_id
26+
pub fn one_synthetic_generic_param_fn(w: impl Wham) {}

0 commit comments

Comments
 (0)