Skip to content

[rustdoc] Show enum discriminant if it is a C-like variant #116142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 9, 2023
Prev Previous commit
Next Next commit
Improve code
  • Loading branch information
GuillaumeGomez committed Oct 9, 2023
commit 4b6fc8b70fd355614bad31d4268993dc7ef17431
36 changes: 12 additions & 24 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,42 +328,30 @@ fn print_const_with_custom_print_scalar<'tcx>(
// For all other types, fallback to the original `pretty_print_const`.
match (ct, ct.ty().kind()) {
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
if with_underscores {
if with_type {
format!(
"{}{}",
format_integer_with_underscore_sep(&int.to_string()),
ui.name_str()
)
} else {
format_integer_with_underscore_sep(&int.to_string())
}
} else if with_type {
format!("{}{}", int.to_string(), ui.name_str())
let mut output = if with_underscores {
format_integer_with_underscore_sep(&int.to_string())
} else {
int.to_string()
};
if with_type {
output += ui.name_str();
}
output
}
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Int(i)) => {
let ty = ct.ty();
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
let data = int.assert_bits(size);
let sign_extended_data = size.sign_extend(data) as i128;
if with_underscores {
if with_type {
format!(
"{}{}",
format_integer_with_underscore_sep(&sign_extended_data.to_string()),
i.name_str()
)
} else {
format_integer_with_underscore_sep(&sign_extended_data.to_string())
}
} else if with_type {
format!("{}{}", sign_extended_data.to_string(), i.name_str())
let mut output = if with_underscores {
format_integer_with_underscore_sep(&sign_extended_data.to_string())
} else {
sign_extended_data.to_string()
};
if with_type {
output += i.name_str();
}
output
}
_ => ct.to_string(),
}
Expand Down
24 changes: 12 additions & 12 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def::CtorKind;
use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::middle::stability;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_target::abi::VariantIdx;
use std::cell::{RefCell, RefMut};
use std::cmp::Ordering;
use std::fmt;
Expand Down Expand Up @@ -1442,9 +1444,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::

/// It'll return true if all variants are C-like variants and if at least one of them has a value
/// set.
fn should_show_c_like_variants_value(
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
) -> bool {
fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -> bool {
let mut has_variants_with_value = false;
for variant in variants {
if let clean::VariantItem(ref var) = *variant.kind &&
Expand All @@ -1463,14 +1463,14 @@ fn display_c_like_variant(
cx: &mut Context<'_>,
item: &clean::Item,
variant: &clean::Variant,
index: rustc_target::abi::VariantIdx,
should_show_c_like_variants_value: bool,
index: VariantIdx,
should_show_enum_discriminant: bool,
enum_def_id: DefId,
) {
let name = item.name.unwrap();
if let Some(ref value) = variant.discriminant {
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
} else if should_show_c_like_variants_value {
} else if should_show_enum_discriminant {
let adt_def = cx.tcx().adt_def(enum_def_id);
let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
if discr.ty.is_signed() {
Expand All @@ -1487,13 +1487,13 @@ fn render_enum_fields(
mut w: &mut Buffer,
cx: &mut Context<'_>,
g: Option<&clean::Generics>,
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
variants: &IndexVec<VariantIdx, clean::Item>,
count_variants: usize,
has_stripped_entries: bool,
is_non_exhaustive: bool,
enum_def_id: DefId,
) {
let should_show_c_like_variants_value = should_show_c_like_variants_value(variants);
let should_show_enum_discriminant = should_show_enum_discriminant(variants);
if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) {
// If there wasn't a `where` clause, we add a whitespace.
w.write_str(" ");
Expand Down Expand Up @@ -1522,7 +1522,7 @@ fn render_enum_fields(
v,
var,
index,
should_show_c_like_variants_value,
should_show_enum_discriminant,
enum_def_id,
),
clean::VariantKind::Tuple(ref s) => {
Expand Down Expand Up @@ -1551,7 +1551,7 @@ fn item_variants(
w: &mut Buffer,
cx: &mut Context<'_>,
it: &clean::Item,
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
variants: &IndexVec<VariantIdx, clean::Item>,
) {
let tcx = cx.tcx();
write!(
Expand All @@ -1564,7 +1564,7 @@ fn item_variants(
document_non_exhaustive_header(it),
document_non_exhaustive(it)
);
let should_show_c_like_variants_value = should_show_c_like_variants_value(variants);
let should_show_enum_discriminant = should_show_enum_discriminant(variants);
for (index, variant) in variants.iter_enumerated() {
if variant.is_stripped() {
continue;
Expand Down Expand Up @@ -1593,7 +1593,7 @@ fn item_variants(
variant,
var,
index,
should_show_c_like_variants_value,
should_show_enum_discriminant,
it.def_id().unwrap(),
);
} else {
Expand Down