Skip to content

Commit

Permalink
rustdoc: json: add support for variances
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Apr 2, 2024
1 parent 7406e00 commit e381cb4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,11 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
use clean::GenericParamDefKind::*;
match kind {
Lifetime(param) => GenericParamDefKind::Lifetime {
variance: param.variance.map(|var| var.into_tcx(tcx)),
outlives: param.outlives.into_iter().map(convert_lifetime).collect(),
},
Type(param) => GenericParamDefKind::Type {
variance: param.variance.map(|var| var.into_tcx(tcx)),
bounds: param.bounds.into_tcx(tcx),
default: param.default.map(|ty| ty.into_tcx(tcx)),
synthetic: param.synthetic,
Expand All @@ -468,6 +470,17 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
}
}

impl FromWithTcx<ty::Variance> for Variance {
fn from_tcx(variance: ty::Variance, _tcx: TyCtxt<'_>) -> Self {
match variance {
ty::Variance::Covariant => Self::Covariant,
ty::Variance::Invariant => Self::Invariant,
ty::Variance::Contravariant => Self::Contravariant,
ty::Variance::Bivariant => Self::Bivariant,
}
}
}

impl FromWithTcx<clean::WherePredicate> for WherePredicate {
fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
use clean::WherePredicate::*;
Expand Down
12 changes: 12 additions & 0 deletions src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ pub struct GenericParamDef {
#[serde(rename_all = "snake_case")]
pub enum GenericParamDefKind {
Lifetime {
variance: Option<Variance>,
outlives: Vec<String>,
},
Type {
variance: Option<Variance>,
bounds: Vec<GenericBound>,
default: Option<Type>,
/// This is normally `false`, which means that this generic parameter is
Expand Down Expand Up @@ -480,6 +482,16 @@ pub enum GenericParamDefKind {
},
}

// FIXME(fmease): docs
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Variance {
Covariant,
Invariant,
Contravariant,
Bivariant,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WherePredicate {
Expand Down
9 changes: 7 additions & 2 deletions src/tools/jsondoclint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,13 @@ impl<'a> Validator<'a> {

fn check_generic_param_def(&mut self, gpd: &'a GenericParamDef) {
match &gpd.kind {
rustdoc_json_types::GenericParamDefKind::Lifetime { outlives: _ } => {}
rustdoc_json_types::GenericParamDefKind::Type { bounds, default, synthetic: _ } => {
rustdoc_json_types::GenericParamDefKind::Lifetime { variance: _, outlives: _ } => {}
rustdoc_json_types::GenericParamDefKind::Type {
variance: _,
bounds,
default,
synthetic: _,
} => {
bounds.iter().for_each(|b| self.check_generic_bound(b));
if let Some(ty) = default {
self.check_type(ty);
Expand Down

0 comments on commit e381cb4

Please sign in to comment.