Skip to content

Commit 476af31

Browse files
committed
Auto merge of #62457 - zackmdavis:minimax_search_and_the_structure_of_cognition, r=varkor
pretty-pretty extremal constants! (A resurrection of the defunct #57073.) 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. r? @varkor
2 parents ad7c55e + d1cdb02 commit 476af31

12 files changed

+62
-45
lines changed

src/librustc/ty/print/pretty.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use crate::middle::cstore::{ExternCrate, ExternCrateSource};
66
use crate::middle::region;
77
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
88
use crate::ty::subst::{Kind, Subst, UnpackedKind};
9-
use crate::ty::layout::Size;
10-
use crate::mir::interpret::{ConstValue, sign_extend, Scalar};
9+
use crate::ty::layout::{Integer, IntegerExt, Size};
10+
use crate::mir::interpret::{ConstValue, sign_extend, Scalar, truncate};
1111
use syntax::ast;
1212
use rustc_apfloat::ieee::{Double, Single};
1313
use rustc_apfloat::Float;
1414
use rustc_target::spec::abi::Abi;
15+
use syntax::attr::{SignedInt, UnsignedInt};
1516
use syntax::symbol::{kw, InternedString};
1617

1718
use std::cell::Cell;
@@ -899,15 +900,31 @@ pub trait PrettyPrinter<'tcx>:
899900
return Ok(self);
900901
},
901902
ty::Uint(ui) => {
902-
p!(write("{}{}", data, ui));
903+
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(ui)).size();
904+
let max = truncate(u128::max_value(), bit_size);
905+
906+
if data == max {
907+
p!(write("std::{}::MAX", ui))
908+
} else {
909+
p!(write("{}{}", data, ui))
910+
};
903911
return Ok(self);
904912
},
905913
ty::Int(i) =>{
914+
let bit_size = Integer::from_attr(&self.tcx(), SignedInt(i))
915+
.size().bits() as u128;
916+
let min = 1u128 << (bit_size - 1);
917+
let max = min - 1;
918+
906919
let ty = self.tcx().lift_to_global(&ct.ty).unwrap();
907920
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
908921
.unwrap()
909922
.size;
910-
p!(write("{}{}", sign_extend(data, size) as i128, i));
923+
match data {
924+
d if d == min => p!(write("std::{}::MIN", i)),
925+
d if d == max => p!(write("std::{}::MAX", i)),
926+
_ => p!(write("{}{}", sign_extend(data, size) as i128, i))
927+
}
911928
return Ok(self);
912929
},
913930
ty::Char => {

src/test/ui/consts/const-match-check.eval1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
1+
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
22
--> $DIR/const-match-check.rs:25:15
33
|
44
LL | A = { let 0 = 0; 0 },
5-
| ^ pattern `-2147483648i32..=-1i32` not covered
5+
| ^ pattern `std::i32::MIN..=-1i32` not covered
66

77
error: aborting due to previous error
88

src/test/ui/consts/const-match-check.eval2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
1+
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
22
--> $DIR/const-match-check.rs:31:24
33
|
44
LL | let x: [i32; { let 0 = 0; 0 }] = [];
5-
| ^ pattern `-2147483648i32..=-1i32` not covered
5+
| ^ pattern `std::i32::MIN..=-1i32` not covered
66

77
error: aborting due to previous error
88

src/test/ui/consts/const-match-check.matchck.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
error[E0005]: refutable pattern in local binding: `-2147483648i32..=-1i32` not covered
1+
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
22
--> $DIR/const-match-check.rs:4:22
33
|
44
LL | const X: i32 = { let 0 = 0; 0 };
5-
| ^ pattern `-2147483648i32..=-1i32` not covered
5+
| ^ pattern `std::i32::MIN..=-1i32` not covered
66

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

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

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

2525
error: aborting due to 4 previous errors
2626

src/test/ui/exhaustive_integer_patterns.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ note: lint level defined here
1010
LL | #![deny(unreachable_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

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

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

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

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

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

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

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

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

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

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

src/test/ui/for/for-loop-refutable-pattern-error-message.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in `for` loop binding: `&-2147483648i32..=0i32` not covered
1+
error[E0005]: refutable pattern in `for` loop binding: `&std::i32::MIN..=0i32` not covered
22
--> $DIR/for-loop-refutable-pattern-error-message.rs:2:9
33
|
44
LL | for &1 in [1].iter() {}
5-
| ^^ pattern `&-2147483648i32..=0i32` not covered
5+
| ^^ pattern `&std::i32::MIN..=0i32` not covered
66

77
error: aborting due to previous error
88

src/test/ui/match/match-non-exhaustive.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `-2147483648i32..=0i32` and `2i32..=2147483647i32` not covered
1+
error[E0004]: non-exhaustive patterns: `std::i32::MIN..=0i32` and `2i32..=std::i32::MAX` not covered
22
--> $DIR/match-non-exhaustive.rs:2:11
33
|
44
LL | match 0 { 1 => () }
5-
| ^ patterns `-2147483648i32..=0i32` and `2i32..=2147483647i32` not covered
5+
| ^ patterns `std::i32::MIN..=0i32` and `2i32..=std::i32::MAX` not covered
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

src/test/ui/non-exhaustive/non-exhaustive-match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn main() {
1212
match Some(10) { //~ ERROR non-exhaustive patterns: `Some(_)` not covered
1313
None => {}
1414
}
15-
match (2, 3, 4) { //~ ERROR non-exhaustive patterns: `(_, _, -2147483648i32..=3i32)`
16-
// and `(_, _, 5i32..=2147483647i32)` not covered
15+
match (2, 3, 4) { //~ ERROR non-exhaustive patterns: `(_, _, std::i32::MIN..=3i32)`
16+
// and `(_, _, 5i32..=std::i32::MAX)` not covered
1717
(_, _, 4) => {}
1818
}
1919
match (T::A, T::A) { //~ ERROR non-exhaustive patterns: `(A, A)` not covered

src/test/ui/non-exhaustive/non-exhaustive-match.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ LL | match Some(10) {
2828
|
2929
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3030

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

src/test/ui/precise_pointer_size_matching.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0004]: non-exhaustive patterns: `$ISIZE_MIN..=-6isize` and `21isize..=$ISIZE_MAX` not covered
1+
error[E0004]: non-exhaustive patterns: `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered
22
--> $DIR/precise_pointer_size_matching.rs:24:11
33
|
44
LL | match 0isize {
5-
| ^^^^^^ patterns `$ISIZE_MIN..=-6isize` and `21isize..=$ISIZE_MAX` not covered
5+
| ^^^^^^ patterns `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

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

src/test/ui/refutable-pattern-errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }
33

44
fn main() {
55
let (1, (Some(1), 2..=3)) = (1, (None, 2));
6-
//~^ ERROR refutable pattern in local binding: `(-2147483648i32..=0i32, _)` not covered
6+
//~^ ERROR refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` not covered
77
}

src/test/ui/refutable-pattern-errors.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0005]: refutable pattern in function argument: `(_, _)` not covered
44
LL | fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }
55
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(_, _)` not covered
66

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

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)