Skip to content

Commit

Permalink
pretty-pretty extremal constants!
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zackmdavis committed Aug 7, 2019
1 parent c326c2e commit d1cdb02
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 45 deletions.
25 changes: 21 additions & 4 deletions src/librustc/ty/print/pretty.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-match-check.eval1.stderr
@@ -1,8 +1,8 @@
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:25:15
|
LL | A = { let 0 = 0; 0 },
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-match-check.eval2.stderr
@@ -1,8 +1,8 @@
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:31:24
|
LL | let x: [i32; { let 0 = 0; 0 }] = [];
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error: aborting due to previous error

Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/consts/const-match-check.matchck.stderr
@@ -1,26 +1,26 @@
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:4:22
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:8:23
|
LL | static Y: i32 = { let 0 = 0; 0 };
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:13:26
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
--> $DIR/const-match-check.rs:19:26
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `-2147483648i32..=-1i32` not covered
| ^ pattern `std::i32::MIN..=-1i32` not covered

error: aborting due to 4 previous errors

Expand Down
28 changes: 14 additions & 14 deletions src/test/ui/exhaustive_integer_patterns.stderr
Expand Up @@ -10,11 +10,11 @@ note: lint level defined here
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error[E0004]: non-exhaustive patterns: `128u8..=255u8` not covered
error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered
--> $DIR/exhaustive_integer_patterns.rs:28:11
|
LL | match x {
| ^ pattern `128u8..=255u8` not covered
| ^ pattern `128u8..=std::u8::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand All @@ -32,19 +32,19 @@ error: unreachable pattern
LL | -2..=20 => {}
| ^^^^^^^

error[E0004]: non-exhaustive patterns: `-128i8..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered
error[E0004]: non-exhaustive patterns: `std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered
--> $DIR/exhaustive_integer_patterns.rs:41:11
|
LL | match x {
| ^ patterns `-128i8..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered
| ^ patterns `std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `-128i8` not covered
error[E0004]: non-exhaustive patterns: `std::i8::MIN` not covered
--> $DIR/exhaustive_integer_patterns.rs:82:11
|
LL | match 0i8 {
| ^^^ pattern `-128i8` not covered
| ^^^ pattern `std::i8::MIN` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand All @@ -56,19 +56,19 @@ LL | match 0i16 {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `128u8..=255u8` not covered
error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered
--> $DIR/exhaustive_integer_patterns.rs:108:11
|
LL | match 0u8 {
| ^^^ pattern `128u8..=255u8` not covered
| ^^^ pattern `128u8..=std::u8::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=255u8, Some(_))` not covered
error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered
--> $DIR/exhaustive_integer_patterns.rs:120:11
|
LL | match (0u8, Some(())) {
| ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=255u8, Some(_))` not covered
| ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand All @@ -80,19 +80,19 @@ LL | match (0u8, true) {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `340282366920938463463374607431768211455u128` not covered
error[E0004]: non-exhaustive patterns: `std::u128::MAX` not covered
--> $DIR/exhaustive_integer_patterns.rs:145:11
|
LL | match 0u128 {
| ^^^^^ pattern `340282366920938463463374607431768211455u128` not covered
| ^^^^^ pattern `std::u128::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `5u128..=340282366920938463463374607431768211455u128` not covered
error[E0004]: non-exhaustive patterns: `5u128..=std::u128::MAX` not covered
--> $DIR/exhaustive_integer_patterns.rs:149:11
|
LL | match 0u128 {
| ^^^^^ pattern `5u128..=340282366920938463463374607431768211455u128` not covered
| ^^^^^ pattern `5u128..=std::u128::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
@@ -1,8 +1,8 @@
error[E0005]: refutable pattern in `for` loop binding: `&-2147483648i32..=0i32` not covered
error[E0005]: refutable pattern in `for` loop binding: `&std::i32::MIN..=0i32` not covered
--> $DIR/for-loop-refutable-pattern-error-message.rs:2:9
|
LL | for &1 in [1].iter() {}
| ^^ pattern `&-2147483648i32..=0i32` not covered
| ^^ pattern `&std::i32::MIN..=0i32` not covered

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/match/match-non-exhaustive.stderr
@@ -1,8 +1,8 @@
error[E0004]: non-exhaustive patterns: `-2147483648i32..=0i32` and `2i32..=2147483647i32` not covered
error[E0004]: non-exhaustive patterns: `std::i32::MIN..=0i32` and `2i32..=std::i32::MAX` not covered
--> $DIR/match-non-exhaustive.rs:2:11
|
LL | match 0 { 1 => () }
| ^ patterns `-2147483648i32..=0i32` and `2i32..=2147483647i32` not covered
| ^ patterns `std::i32::MIN..=0i32` and `2i32..=std::i32::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/non-exhaustive/non-exhaustive-match.rs
Expand Up @@ -12,8 +12,8 @@ fn main() {
match Some(10) { //~ ERROR non-exhaustive patterns: `Some(_)` not covered
None => {}
}
match (2, 3, 4) { //~ ERROR non-exhaustive patterns: `(_, _, -2147483648i32..=3i32)`
// and `(_, _, 5i32..=2147483647i32)` not covered
match (2, 3, 4) { //~ ERROR non-exhaustive patterns: `(_, _, std::i32::MIN..=3i32)`
// and `(_, _, 5i32..=std::i32::MAX)` not covered
(_, _, 4) => {}
}
match (T::A, T::A) { //~ ERROR non-exhaustive patterns: `(A, A)` not covered
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/non-exhaustive/non-exhaustive-match.stderr
Expand Up @@ -28,11 +28,11 @@ LL | match Some(10) {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `(_, _, -2147483648i32..=3i32)` and `(_, _, 5i32..=2147483647i32)` not covered
error[E0004]: non-exhaustive patterns: `(_, _, std::i32::MIN..=3i32)` and `(_, _, 5i32..=std::i32::MAX)` not covered
--> $DIR/non-exhaustive-match.rs:15:11
|
LL | match (2, 3, 4) {
| ^^^^^^^^^ patterns `(_, _, -2147483648i32..=3i32)` and `(_, _, 5i32..=2147483647i32)` not covered
| ^^^^^^^^^ patterns `(_, _, std::i32::MIN..=3i32)` and `(_, _, 5i32..=std::i32::MAX)` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/precise_pointer_size_matching.stderr
@@ -1,16 +1,16 @@
error[E0004]: non-exhaustive patterns: `$ISIZE_MIN..=-6isize` and `21isize..=$ISIZE_MAX` not covered
error[E0004]: non-exhaustive patterns: `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered
--> $DIR/precise_pointer_size_matching.rs:24:11
|
LL | match 0isize {
| ^^^^^^ patterns `$ISIZE_MIN..=-6isize` and `21isize..=$ISIZE_MAX` not covered
| ^^^^^^ patterns `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: `0usize` and `21usize..=$USIZE_MAX` not covered
error[E0004]: non-exhaustive patterns: `0usize` and `21usize..=std::usize::MAX` not covered
--> $DIR/precise_pointer_size_matching.rs:29:11
|
LL | match 0usize {
| ^^^^^^ patterns `0usize` and `21usize..=$USIZE_MAX` not covered
| ^^^^^^ patterns `0usize` and `21usize..=std::usize::MAX` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/refutable-pattern-errors.rs
Expand Up @@ -3,5 +3,5 @@ fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }

fn main() {
let (1, (Some(1), 2..=3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding: `(-2147483648i32..=0i32, _)` not covered
//~^ ERROR refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` not covered
}
4 changes: 2 additions & 2 deletions src/test/ui/refutable-pattern-errors.stderr
Expand Up @@ -4,11 +4,11 @@ error[E0005]: refutable pattern in function argument: `(_, _)` not covered
LL | fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(_, _)` not covered

error[E0005]: refutable pattern in local binding: `(-2147483648i32..=0i32, _)` not covered
error[E0005]: refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` not covered
--> $DIR/refutable-pattern-errors.rs:5:9
|
LL | let (1, (Some(1), 2..=3)) = (1, (None, 2));
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(-2147483648i32..=0i32, _)` not covered
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(std::i32::MIN..=0i32, _)` not covered

error: aborting due to 2 previous errors

Expand Down

0 comments on commit d1cdb02

Please sign in to comment.