Skip to content

Commit 9e9cc66

Browse files
authored
Rollup merge of #94091 - GuillaumeGomez:rustdoc-const-computed-value, r=oli-obk
Fix rustdoc const computed value Fixes #85088. It looks like this now (instead of hexadecimal): ![Screenshot from 2022-02-17 17-55-39](https://user-images.githubusercontent.com/3050060/154532115-0f9861a0-406f-4c9c-957f-32bedd8aca7d.png) r? ````@oli-obk````
2 parents 1771b98 + 296adba commit 9e9cc66

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
456456
// Going through `u64` to check size and truncation.
457457
Ok(Double::from_bits(self.to_u64()?.into()))
458458
}
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+
}
459464
}
460465

461466
#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]

compiler/rustc_middle/src/ty/consts/int.rs

+7
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,10 @@ impl fmt::UpperHex for ScalarInt {
384384
write!(f, "{:01$X}", { self.data }, self.size as usize * 2)
385385
}
386386
}
387+
388+
impl fmt::Display for ScalarInt {
389+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
390+
self.check_data();
391+
write!(f, "{}", { self.data })
392+
}
393+
}

src/librustdoc/clean/utils.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,11 @@ 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!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
305+
format!(
306+
"{}{}",
307+
format_integer_with_underscore_sep(&int.rustdoc_display()),
308+
ui.name_str()
309+
)
306310
}
307311
(ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => {
308312
let ty = tcx.lift(ct.ty()).unwrap();
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_name = "foo"]
2+
3+
// @has 'foo/constant.HOUR_IN_SECONDS.html'
4+
// @has - '//*[@class="docblock item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = 60 * 60; // 3_600u64'
5+
pub const HOUR_IN_SECONDS: u64 = 60 * 60;
6+
7+
// @has 'foo/constant.NEGATIVE.html'
8+
// @has - '//*[@class="docblock item-decl"]//code' 'pub const NEGATIVE: i64 = -60 * 60; // -3_600i64'
9+
pub const NEGATIVE: i64 = -60 * 60;

0 commit comments

Comments
 (0)