Show enum variant value if it is a C-like variant

This commit is contained in:
Guillaume Gomez 2023-09-25 14:51:32 +02:00
parent af68593179
commit a576dfd0c6
4 changed files with 47 additions and 18 deletions

View file

@ -2091,9 +2091,8 @@ impl Discriminant {
pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> Option<String> {
self.expr.map(|body| rendered_const(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()
pub(crate) fn value(&self, tcx: TyCtxt<'_>, with_underscores: bool) -> String {
print_evaluated_const(tcx, self.value, with_underscores, false).unwrap()
}
}
@ -2348,7 +2347,7 @@ impl ConstantKind {
match *self {
ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
print_evaluated_const(tcx, def_id, true)
print_evaluated_const(tcx, def_id, true, true)
}
}
}

View file

@ -275,7 +275,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
pub(crate) fn print_evaluated_const(
tcx: TyCtxt<'_>,
def_id: DefId,
underscores_and_type: bool,
with_underscores: bool,
with_type: bool,
) -> Option<String> {
tcx.const_eval_poly(def_id).ok().and_then(|val| {
let ty = tcx.type_of(def_id).instantiate_identity();
@ -284,7 +285,7 @@ pub(crate) fn print_evaluated_const(
(mir::ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
(mir::ConstValue::Scalar(_), _) => {
let const_ = mir::Const::from_value(val, ty);
Some(print_const_with_custom_print_scalar(tcx, const_, underscores_and_type))
Some(print_const_with_custom_print_scalar(tcx, const_, with_underscores, with_type))
}
_ => None,
}
@ -320,14 +321,25 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
fn print_const_with_custom_print_scalar<'tcx>(
tcx: TyCtxt<'tcx>,
ct: mir::Const<'tcx>,
underscores_and_type: bool,
with_underscores: bool,
with_type: bool,
) -> String {
// Use a slightly different format for integer types which always shows the actual value.
// 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 underscores_and_type {
format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
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())
} else {
int.to_string()
}
@ -337,12 +349,18 @@ fn print_const_with_custom_print_scalar<'tcx>(
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 underscores_and_type {
format!(
"{}{}",
format_integer_with_underscore_sep(&sign_extended_data.to_string()),
i.name_str()
)
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())
} else {
sign_extended_data.to_string()
}

View file

@ -1467,9 +1467,15 @@ fn render_enum_fields<'a>(
match *v.kind {
// FIXME(#101337): Show discriminant
clean::VariantItem(ref var) => match var.kind {
clean::VariantKind::CLike => w.write_str(name.as_str()),
clean::VariantKind::CLike => {
if let Some(ref value) = var.discriminant {
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
} else {
w.write_str(name.as_str());
}
}
clean::VariantKind::Tuple(ref s) => {
write!(w, "{name}({})", print_tuple_struct_fields(cx, s),);
write!(w, "{name}({})", print_tuple_struct_fields(cx, s));
}
clean::VariantKind::Struct(ref s) => {
render_struct(w, v, None, None, &s.fields, TAB, false, cx);
@ -1523,6 +1529,12 @@ fn item_variants<'a>(
" rightside",
);
write!(w, "<h3 class=\"code-header\">{name}", name = variant.name.unwrap());
if let clean::VariantItem(ref var) = *variant.kind &&
let clean::VariantKind::CLike = var.kind &&
let Some(ref value) = var.discriminant
{
write!(w, " = {}", value.value(cx.tcx(), true));
}
let clean::VariantItem(variant_data) = &*variant.kind else { unreachable!() };

View file

@ -745,7 +745,7 @@ impl FromWithTcx<clean::Discriminant> for Discriminant {
// `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),
value: disr.value(tcx, false),
}
}
}