Skip to content

Commit

Permalink
Rollup merge of rust-lang#94091 - GuillaumeGomez:rustdoc-const-comput…
Browse files Browse the repository at this point in the history
…ed-value, r=oli-obk

Fix rustdoc const computed value

Fixes rust-lang#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`
  • Loading branch information
matthiaskrgr authored Feb 19, 2022
2 parents 37e2d39 + 296adba commit 79c4c1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
// Going through `u64` to check size and truncation.
Ok(Double::from_bits(self.to_u64()?.into()))
}

// FIXME: Replace current `impl Display for Scalar` with `impl LowerHex`.
pub fn rustdoc_display(&self) -> String {
if let Scalar::Int(int) = self { int.to_string() } else { self.to_string() }
}
}

#[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/ty/consts/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,10 @@ impl fmt::UpperHex for ScalarInt {
write!(f, "{:01$X}", { self.data }, self.size as usize * 2)
}
}

impl fmt::Display for ScalarInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.check_data();
write!(f, "{}", { self.data })
}
}
6 changes: 5 additions & 1 deletion src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: ty::Const<'_>) -> S
// For all other types, fallback to the original `pretty_print_const`.
match (ct.val(), ct.ty().kind()) {
(ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Uint(ui)) => {
format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
format!(
"{}{}",
format_integer_with_underscore_sep(&int.rustdoc_display()),
ui.name_str()
)
}
(ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => {
let ty = tcx.lift(ct.ty()).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc/const-value-display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![crate_name = "foo"]

// @has 'foo/constant.HOUR_IN_SECONDS.html'
// @has - '//*[@class="docblock item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = 60 * 60; // 3_600u64'
pub const HOUR_IN_SECONDS: u64 = 60 * 60;

// @has 'foo/constant.NEGATIVE.html'
// @has - '//*[@class="docblock item-decl"]//code' 'pub const NEGATIVE: i64 = -60 * 60; // -3_600i64'
pub const NEGATIVE: i64 = -60 * 60;

0 comments on commit 79c4c1e

Please sign in to comment.