pretty-pretty extremal constants!

While many programmers may intuitively appreciate the significance of
"magic numbers" like −2147483648, Rust is about empowering everyone to
build reliable and efficient software! It's a bit more legible to
print the constant names (even noisy fully-qualified-paths thereof).

The bit-manipulation methods mirror those in
`librustc_mir::hair::pattern::_match::all_constructors`; thanks to the
immortal Varkor for guidance.

Resolves #56393.
This commit is contained in:
Zack M. Davis 2019-07-06 19:41:12 -07:00
parent c326c2e0f1
commit d1cdb02e4d
12 changed files with 62 additions and 45 deletions

View file

@ -6,12 +6,13 @@ use crate::middle::cstore::{ExternCrate, ExternCrateSource};
use crate::middle::region;
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
use crate::ty::subst::{Kind, Subst, UnpackedKind};
use crate::ty::layout::Size;
use crate::mir::interpret::{ConstValue, sign_extend, Scalar};
use crate::ty::layout::{Integer, IntegerExt, Size};
use crate::mir::interpret::{ConstValue, sign_extend, Scalar, truncate};
use syntax::ast;
use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::Float;
use rustc_target::spec::abi::Abi;
use syntax::attr::{SignedInt, UnsignedInt};
use syntax::symbol::{kw, InternedString};
use std::cell::Cell;
@ -899,15 +900,31 @@ pub trait PrettyPrinter<'tcx>:
return Ok(self);
},
ty::Uint(ui) => {
p!(write("{}{}", data, ui));
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(ui)).size();
let max = truncate(u128::max_value(), bit_size);
if data == max {
p!(write("std::{}::MAX", ui))
} else {
p!(write("{}{}", data, ui))
};
return Ok(self);
},
ty::Int(i) =>{
let bit_size = Integer::from_attr(&self.tcx(), SignedInt(i))
.size().bits() as u128;
let min = 1u128 << (bit_size - 1);
let max = min - 1;
let ty = self.tcx().lift_to_global(&ct.ty).unwrap();
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
.unwrap()
.size;
p!(write("{}{}", sign_extend(data, size) as i128, i));
match data {
d if d == min => p!(write("std::{}::MIN", i)),
d if d == max => p!(write("std::{}::MAX", i)),
_ => p!(write("{}{}", sign_extend(data, size) as i128, i))
}
return Ok(self);
},
ty::Char => {