Skip to content

Commit e381cb4

Browse files
committed
rustdoc: json: add support for variances
1 parent 7406e00 commit e381cb4

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/librustdoc/json/conversions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,11 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
454454
use clean::GenericParamDefKind::*;
455455
match kind {
456456
Lifetime(param) => GenericParamDefKind::Lifetime {
457+
variance: param.variance.map(|var| var.into_tcx(tcx)),
457458
outlives: param.outlives.into_iter().map(convert_lifetime).collect(),
458459
},
459460
Type(param) => GenericParamDefKind::Type {
461+
variance: param.variance.map(|var| var.into_tcx(tcx)),
460462
bounds: param.bounds.into_tcx(tcx),
461463
default: param.default.map(|ty| ty.into_tcx(tcx)),
462464
synthetic: param.synthetic,
@@ -468,6 +470,17 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
468470
}
469471
}
470472

473+
impl FromWithTcx<ty::Variance> for Variance {
474+
fn from_tcx(variance: ty::Variance, _tcx: TyCtxt<'_>) -> Self {
475+
match variance {
476+
ty::Variance::Covariant => Self::Covariant,
477+
ty::Variance::Invariant => Self::Invariant,
478+
ty::Variance::Contravariant => Self::Contravariant,
479+
ty::Variance::Bivariant => Self::Bivariant,
480+
}
481+
}
482+
}
483+
471484
impl FromWithTcx<clean::WherePredicate> for WherePredicate {
472485
fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
473486
use clean::WherePredicate::*;

src/rustdoc-json-types/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,11 @@ pub struct GenericParamDef {
443443
#[serde(rename_all = "snake_case")]
444444
pub enum GenericParamDefKind {
445445
Lifetime {
446+
variance: Option<Variance>,
446447
outlives: Vec<String>,
447448
},
448449
Type {
450+
variance: Option<Variance>,
449451
bounds: Vec<GenericBound>,
450452
default: Option<Type>,
451453
/// This is normally `false`, which means that this generic parameter is
@@ -480,6 +482,16 @@ pub enum GenericParamDefKind {
480482
},
481483
}
482484

485+
// FIXME(fmease): docs
486+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
487+
#[serde(rename_all = "snake_case")]
488+
pub enum Variance {
489+
Covariant,
490+
Invariant,
491+
Contravariant,
492+
Bivariant,
493+
}
494+
483495
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
484496
#[serde(rename_all = "snake_case")]
485497
pub enum WherePredicate {

src/tools/jsondoclint/src/validator.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,13 @@ impl<'a> Validator<'a> {
324324

325325
fn check_generic_param_def(&mut self, gpd: &'a GenericParamDef) {
326326
match &gpd.kind {
327-
rustdoc_json_types::GenericParamDefKind::Lifetime { outlives: _ } => {}
328-
rustdoc_json_types::GenericParamDefKind::Type { bounds, default, synthetic: _ } => {
327+
rustdoc_json_types::GenericParamDefKind::Lifetime { variance: _, outlives: _ } => {}
328+
rustdoc_json_types::GenericParamDefKind::Type {
329+
variance: _,
330+
bounds,
331+
default,
332+
synthetic: _,
333+
} => {
329334
bounds.iter().for_each(|b| self.check_generic_bound(b));
330335
if let Some(ty) = default {
331336
self.check_type(ty);

0 commit comments

Comments
 (0)