Skip to content

Commit

Permalink
rustdoc: Compute enum discriminant on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
aDotInTheVoid committed Sep 4, 2022
1 parent b76a012 commit efbd8f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
18 changes: 4 additions & 14 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,10 +1787,7 @@ pub(crate) fn clean_visibility(vis: ty::Visibility) -> Visibility {
pub(crate) fn clean_variant_def<'tcx>(variant: &ty::VariantDef, cx: &mut DocContext<'tcx>) -> Item {
let kind = match variant.ctor_kind {
CtorKind::Const => Variant::CLike(match variant.discr {
ty::VariantDiscr::Explicit(def_id) => Some(Discriminant {
expr: None,
value: print_evaluated_const(cx.tcx, def_id, false).unwrap(),
}),
ty::VariantDiscr::Explicit(def_id) => Some(Discriminant { expr: None, value: def_id }),
ty::VariantDiscr::Relative(_) => None,
}),
CtorKind::Fn => Variant::Tuple(
Expand Down Expand Up @@ -1820,16 +1817,9 @@ fn clean_variant_data<'tcx>(
hir::VariantData::Tuple(..) => {
Variant::Tuple(variant.fields().iter().map(|x| clean_field(x, cx)).collect())
}
hir::VariantData::Unit(..) => Variant::CLike(disr_expr.map(|disr| {
Discriminant {
expr: Some(print_const_expr(cx.tcx, disr.body)),
value: print_evaluated_const(
cx.tcx,
cx.tcx.hir().local_def_id(disr.hir_id).to_def_id(),
false,
)
.unwrap(),
}
hir::VariantData::Unit(..) => Variant::CLike(disr_expr.map(|disr| Discriminant {
expr: Some(disr.body),
value: cx.tcx.hir().local_def_id(disr.hir_id).to_def_id(),
})),
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2116,8 +2116,20 @@ impl Variant {
pub(crate) struct Discriminant {
// In the case of cross crate re-exports, we don't have the nessesary information
// to reconstruct the expression of the discriminant, only the value.
pub(crate) expr: Option<String>,
pub(crate) value: String,
pub(super) expr: Option<BodyId>,
pub(super) value: DefId,
}

impl Discriminant {
/// Will be `None` in the case of cross-crate reexports, and may be
/// simplified
pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> Option<String> {
self.expr.map(|body| print_const_expr(tcx, body))
}
/// Will always be a machine readable number, without underscores or suffixes.
pub(crate) fn value(&self, tcx: TyCtxt<'_>) -> String {
print_evaluated_const(tcx, self.value, false).unwrap()
}
}

/// Small wrapper around [`rustc_span::Span`] that adds helper methods
Expand Down
18 changes: 10 additions & 8 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl FromWithTcx<clean::Variant> for Variant {
fn from_tcx(variant: clean::Variant, tcx: TyCtxt<'_>) -> Self {
use clean::Variant::*;
match variant {
CLike(disr) => Variant::Plain(disr.map(convert_discriminant)),
CLike(disr) => Variant::Plain(disr.map(|disr| disr.into_tcx(tcx))),
Tuple(fields) => Variant::Tuple(
fields
.into_iter()
Expand All @@ -678,13 +678,15 @@ impl FromWithTcx<clean::Variant> for Variant {
}
}

fn convert_discriminant(disr: clean::Discriminant) -> Discriminant {
Discriminant {
// expr is only none if going throught the inlineing path, which gets
// `rustc_middle` types, not `rustc_hir`, but because JSON never inlines
// the expr is always some.
expr: disr.expr.unwrap(),
value: disr.value,
impl FromWithTcx<clean::Discriminant> for Discriminant {
fn from_tcx(disr: clean::Discriminant, tcx: TyCtxt<'_>) -> Self {
Discriminant {
// expr is only none if going throught the inlineing path, which gets
// `rustc_middle` types, not `rustc_hir`, but because JSON never inlines
// the expr is always some.
expr: disr.expr(tcx).unwrap(),
value: disr.value(tcx),
}
}
}

Expand Down

0 comments on commit efbd8f6

Please sign in to comment.