Skip to content

Commit

Permalink
rustdoc- Show defaults on const generics
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Jun 3, 2021
1 parent da86509 commit 6dff51f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
}
}
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Const { .. } => {}
GenericParamDefKind::Const { ref mut default, .. } => {
// We never want something like `impl<const N: usize = 10>`
default.take();
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,15 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
},
)
}
ty::GenericParamDefKind::Const { .. } => (
ty::GenericParamDefKind::Const { has_default, .. } => (
self.name,
GenericParamDefKind::Const {
did: self.def_id,
ty: cx.tcx.type_of(self.def_id).clean(cx),
default: match has_default {
true => Some(cx.tcx.const_param_default(self.def_id).to_string()),
false => None,
},
},
),
};
Expand Down Expand Up @@ -487,12 +491,15 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
synthetic,
},
),
hir::GenericParamKind::Const { ref ty, default: _ } => (
hir::GenericParamKind::Const { ref ty, default } => (
self.name.ident().name,
GenericParamDefKind::Const {
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
ty: ty.clean(cx),
// FIXME(const_generics_defaults): add `default` field here for docs
default: default.map(|ct| {
let def_id = cx.tcx.hir().local_def_id(ct.hir_id);
ty::Const::from_anon_const(cx.tcx, def_id).to_string()
}),
},
),
};
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ crate enum GenericParamDefKind {
Const {
did: DefId,
ty: Type,
default: Option<String>,
},
}

Expand Down
16 changes: 13 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,22 @@ impl clean::GenericParamDef {

Ok(())
}
clean::GenericParamDefKind::Const { ref ty, .. } => {
clean::GenericParamDefKind::Const { ref ty, ref default, .. } => {
if f.alternate() {
write!(f, "const {}: {:#}", self.name, ty.print(cx))
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
} else {
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))?;
}

if let Some(default) = default {
if f.alternate() {
write!(f, " = {:#}", default)?;
} else {
write!(f, "&nbsp;=&nbsp;{}", default)?;
}
}

Ok(())
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
default: default.map(|x| x.into_tcx(tcx)),
},
Const { did: _, ty } => GenericParamDefKind::Const(ty.into_tcx(tcx)),
Const { did: _, ty, default } => {
GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default }
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub struct GenericParamDef {
pub enum GenericParamDefKind {
Lifetime,
Type { bounds: Vec<GenericBound>, default: Option<Type> },
Const(Type),
Const { ty: Type, default: Option<String> },
}

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

0 comments on commit 6dff51f

Please sign in to comment.