Skip to content

Commit f639ba6

Browse files
authored
Rollup merge of #94189 - GuillaumeGomez:scalar-lower-hex, r=RalfJung
Implement LowerHex on Scalar to clean up their display in rustdoc Follow-up of #94091. r? ````@RalfJung````
2 parents 74cb6b7 + c358ffe commit f639ba6

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
697697
this.ecx.read_discriminant(op),
698698
this.path,
699699
err_ub!(InvalidTag(val)) =>
700-
{ "{}", val } expected { "a valid enum tag" },
700+
{ "{:x}", val } expected { "a valid enum tag" },
701701
err_ub!(InvalidUninitBytes(None)) =>
702702
{ "uninitialized bytes" } expected { "a valid enum tag" },
703703
err_unsup!(ReadPointerAsBytes) =>

compiler/rustc_middle/src/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
370370
InvalidChar(c) => {
371371
write!(f, "interpreting an invalid 32-bit value as a char: 0x{:08x}", c)
372372
}
373-
InvalidTag(val) => write!(f, "enum value has invalid tag: {}", val),
373+
InvalidTag(val) => write!(f, "enum value has invalid tag: {:x}", val),
374374
InvalidFunctionPointer(p) => {
375375
write!(f, "using {:?} as function pointer but it does not point to a function", p)
376376
}

compiler/rustc_middle/src/mir/interpret/value.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,16 @@ impl<Tag: Provenance> fmt::Display for Scalar<Tag> {
153153
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154154
match self {
155155
Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
156-
Scalar::Int(int) => write!(f, "{:?}", int),
156+
Scalar::Int(int) => write!(f, "{}", int),
157+
}
158+
}
159+
}
160+
161+
impl<Tag: Provenance> fmt::LowerHex for Scalar<Tag> {
162+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163+
match self {
164+
Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
165+
Scalar::Int(int) => write!(f, "0x{:x}", int),
157166
}
158167
}
159168
}
@@ -456,11 +465,6 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
456465
// Going through `u64` to check size and truncation.
457466
Ok(Double::from_bits(self.to_u64()?.into()))
458467
}
459-
460-
// FIXME: Replace current `impl Display for Scalar` with `impl LowerHex`.
461-
pub fn rustdoc_display(&self) -> String {
462-
if let Scalar::Int(int) = self { int.to_string() } else { self.to_string() }
463-
}
464468
}
465469

466470
#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
@@ -494,7 +498,7 @@ impl<Tag: Provenance> fmt::Display for ScalarMaybeUninit<Tag> {
494498
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
495499
match self {
496500
ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"),
497-
ScalarMaybeUninit::Scalar(s) => write!(f, "{}", s),
501+
ScalarMaybeUninit::Scalar(s) => write!(f, "{:x}", s),
498502
}
499503
}
500504
}

src/librustdoc/clean/utils.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,7 @@ fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: ty::Const<'_>) -> S
302302
// For all other types, fallback to the original `pretty_print_const`.
303303
match (ct.val(), ct.ty().kind()) {
304304
(ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Uint(ui)) => {
305-
format!(
306-
"{}{}",
307-
format_integer_with_underscore_sep(&int.rustdoc_display()),
308-
ui.name_str()
309-
)
305+
format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
310306
}
311307
(ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => {
312308
let ty = tcx.lift(ct.ty()).unwrap();

0 commit comments

Comments
 (0)