Improve code
This commit is contained in:
parent
b0badc17cd
commit
4b6fc8b70f
@ -328,42 +328,30 @@ fn print_const_with_custom_print_scalar<'tcx>(
|
|||||||
// For all other types, fallback to the original `pretty_print_const`.
|
// For all other types, fallback to the original `pretty_print_const`.
|
||||||
match (ct, ct.ty().kind()) {
|
match (ct, ct.ty().kind()) {
|
||||||
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
|
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
|
||||||
if with_underscores {
|
let mut output = if with_underscores {
|
||||||
if with_type {
|
format_integer_with_underscore_sep(&int.to_string())
|
||||||
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 {
|
} else {
|
||||||
int.to_string()
|
int.to_string()
|
||||||
|
};
|
||||||
|
if with_type {
|
||||||
|
output += ui.name_str();
|
||||||
}
|
}
|
||||||
|
output
|
||||||
}
|
}
|
||||||
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Int(i)) => {
|
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Int(i)) => {
|
||||||
let ty = ct.ty();
|
let ty = ct.ty();
|
||||||
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
|
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
|
||||||
let data = int.assert_bits(size);
|
let data = int.assert_bits(size);
|
||||||
let sign_extended_data = size.sign_extend(data) as i128;
|
let sign_extended_data = size.sign_extend(data) as i128;
|
||||||
if with_underscores {
|
let mut output = if with_underscores {
|
||||||
if with_type {
|
format_integer_with_underscore_sep(&sign_extended_data.to_string())
|
||||||
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 {
|
} else {
|
||||||
sign_extended_data.to_string()
|
sign_extended_data.to_string()
|
||||||
|
};
|
||||||
|
if with_type {
|
||||||
|
output += i.name_str();
|
||||||
}
|
}
|
||||||
|
output
|
||||||
}
|
}
|
||||||
_ => ct.to_string(),
|
_ => ct.to_string(),
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,12 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
|||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def::CtorKind;
|
use rustc_hir::def::CtorKind;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
|
use rustc_index::IndexVec;
|
||||||
use rustc_middle::middle::stability;
|
use rustc_middle::middle::stability;
|
||||||
use rustc_middle::ty::{self, TyCtxt};
|
use rustc_middle::ty::{self, TyCtxt};
|
||||||
use rustc_span::hygiene::MacroKind;
|
use rustc_span::hygiene::MacroKind;
|
||||||
use rustc_span::symbol::{kw, sym, Symbol};
|
use rustc_span::symbol::{kw, sym, Symbol};
|
||||||
|
use rustc_target::abi::VariantIdx;
|
||||||
use std::cell::{RefCell, RefMut};
|
use std::cell::{RefCell, RefMut};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@ -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
|
/// It'll return true if all variants are C-like variants and if at least one of them has a value
|
||||||
/// set.
|
/// set.
|
||||||
fn should_show_c_like_variants_value(
|
fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -> bool {
|
||||||
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
|
|
||||||
) -> bool {
|
|
||||||
let mut has_variants_with_value = false;
|
let mut has_variants_with_value = false;
|
||||||
for variant in variants {
|
for variant in variants {
|
||||||
if let clean::VariantItem(ref var) = *variant.kind &&
|
if let clean::VariantItem(ref var) = *variant.kind &&
|
||||||
@ -1463,14 +1463,14 @@ fn display_c_like_variant(
|
|||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
item: &clean::Item,
|
item: &clean::Item,
|
||||||
variant: &clean::Variant,
|
variant: &clean::Variant,
|
||||||
index: rustc_target::abi::VariantIdx,
|
index: VariantIdx,
|
||||||
should_show_c_like_variants_value: bool,
|
should_show_enum_discriminant: bool,
|
||||||
enum_def_id: DefId,
|
enum_def_id: DefId,
|
||||||
) {
|
) {
|
||||||
let name = item.name.unwrap();
|
let name = item.name.unwrap();
|
||||||
if let Some(ref value) = variant.discriminant {
|
if let Some(ref value) = variant.discriminant {
|
||||||
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
|
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 adt_def = cx.tcx().adt_def(enum_def_id);
|
||||||
let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
|
let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
|
||||||
if discr.ty.is_signed() {
|
if discr.ty.is_signed() {
|
||||||
@ -1487,13 +1487,13 @@ fn render_enum_fields(
|
|||||||
mut w: &mut Buffer,
|
mut w: &mut Buffer,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
g: Option<&clean::Generics>,
|
g: Option<&clean::Generics>,
|
||||||
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
|
variants: &IndexVec<VariantIdx, clean::Item>,
|
||||||
count_variants: usize,
|
count_variants: usize,
|
||||||
has_stripped_entries: bool,
|
has_stripped_entries: bool,
|
||||||
is_non_exhaustive: bool,
|
is_non_exhaustive: bool,
|
||||||
enum_def_id: DefId,
|
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 !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) {
|
||||||
// If there wasn't a `where` clause, we add a whitespace.
|
// If there wasn't a `where` clause, we add a whitespace.
|
||||||
w.write_str(" ");
|
w.write_str(" ");
|
||||||
@ -1522,7 +1522,7 @@ fn render_enum_fields(
|
|||||||
v,
|
v,
|
||||||
var,
|
var,
|
||||||
index,
|
index,
|
||||||
should_show_c_like_variants_value,
|
should_show_enum_discriminant,
|
||||||
enum_def_id,
|
enum_def_id,
|
||||||
),
|
),
|
||||||
clean::VariantKind::Tuple(ref s) => {
|
clean::VariantKind::Tuple(ref s) => {
|
||||||
@ -1551,7 +1551,7 @@ fn item_variants(
|
|||||||
w: &mut Buffer,
|
w: &mut Buffer,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
it: &clean::Item,
|
it: &clean::Item,
|
||||||
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
|
variants: &IndexVec<VariantIdx, clean::Item>,
|
||||||
) {
|
) {
|
||||||
let tcx = cx.tcx();
|
let tcx = cx.tcx();
|
||||||
write!(
|
write!(
|
||||||
@ -1564,7 +1564,7 @@ fn item_variants(
|
|||||||
document_non_exhaustive_header(it),
|
document_non_exhaustive_header(it),
|
||||||
document_non_exhaustive(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() {
|
for (index, variant) in variants.iter_enumerated() {
|
||||||
if variant.is_stripped() {
|
if variant.is_stripped() {
|
||||||
continue;
|
continue;
|
||||||
@ -1593,7 +1593,7 @@ fn item_variants(
|
|||||||
variant,
|
variant,
|
||||||
var,
|
var,
|
||||||
index,
|
index,
|
||||||
should_show_c_like_variants_value,
|
should_show_enum_discriminant,
|
||||||
it.def_id().unwrap(),
|
it.def_id().unwrap(),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user