Skip to content

Commit 6b71f0c

Browse files
committed
Hide the end of ranges in pretty printing if it's also the maximum of the type
1 parent 31dd548 commit 6b71f0c

10 files changed

+53
-23
lines changed

compiler/rustc_middle/src/ty/pattern.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,37 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
2828
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2929
match *self {
3030
PatternKind::Range { start, end, include_end } => {
31-
write!(f, "{start}{include_end}{end}")
31+
write!(f, "{start}")?;
32+
33+
match include_end {
34+
RangeEnd::Included => {
35+
if let Some(c) = end.try_to_value() {
36+
let end = c.valtree.unwrap_leaf();
37+
let size = end.size();
38+
let max = match c.ty.kind() {
39+
ty::Int(_) => Some(ty::ScalarInt::truncate_from_int(
40+
size.signed_int_max(),
41+
size,
42+
)),
43+
ty::Uint(_) => Some(ty::ScalarInt::truncate_from_uint(
44+
size.unsigned_int_max(),
45+
size,
46+
)),
47+
ty::Char => {
48+
Some(ty::ScalarInt::truncate_from_uint(char::MAX, size))
49+
}
50+
_ => None,
51+
};
52+
if let Some((max, _)) = max
53+
&& end == max
54+
{
55+
return write!(f, "..");
56+
}
57+
}
58+
}
59+
RangeEnd::Excluded => {}
60+
}
61+
write!(f, "{include_end}{end}")
3262
}
3363
}
3464
}

tests/mir-opt/pattern_types.main.PreCodegen.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
fn main() -> () {
44
let mut _0: ();
55
scope 1 {
6-
debug x => const 2_u32 is 1..=u32::MAX;
6+
debug x => const 2_u32 is 1..;
77
scope 2 {
8-
debug y => const {transmute(0x00000000): (u32) is 1..=u32::MAX};
8+
debug y => const {transmute(0x00000000): (u32) is 1..};
99
}
1010
}
1111

tests/mir-opt/pattern_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::pat::pattern_type;
55

66
// EMIT_MIR pattern_types.main.PreCodegen.after.mir
77
fn main() {
8-
// CHECK: debug x => const 2_u32 is 1..=u32::MAX
8+
// CHECK: debug x => const 2_u32 is 1..
99
let x: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(2) };
10-
// CHECK: debug y => const {transmute(0x00000000): (u32) is 1..=u32::MAX}
10+
// CHECK: debug y => const {transmute(0x00000000): (u32) is 1..}
1111
let y: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
1212
}

tests/ui/lint/clashing-extern-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
1717
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
1818
= note: enum has no representation hint
1919

20-
warning: `extern` block uses type `Option<(usize) is 0..=usize::MAX>`, which is not FFI-safe
20+
warning: `extern` block uses type `Option<(usize) is 0..>`, which is not FFI-safe
2121
--> $DIR/clashing-extern-fn.rs:502:54
2222
|
2323
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;
@@ -276,7 +276,7 @@ LL | fn pt_non_null_ptr() -> pattern_type!(usize is 1..);
276276
LL | fn pt_non_null_ptr() -> *const ();
277277
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
278278
|
279-
= note: expected `unsafe extern "C" fn() -> (usize) is 1..=usize::MAX`
279+
= note: expected `unsafe extern "C" fn() -> (usize) is 1..`
280280
found `unsafe extern "C" fn() -> *const ()`
281281

282282
warning: 24 warnings emitted

tests/ui/type/pattern_types/nested.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `(u32) is 1..=u32::MAX` is not a valid base type for range patterns
1+
error: `(u32) is 1..` is not a valid base type for range patterns
22
--> $DIR/nested.rs:10:34
33
|
44
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
@@ -10,7 +10,7 @@ note: range patterns only support integers
1010
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
1111
| ^^^
1212

13-
error: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
13+
error: `(i32) is 1..` is not a valid base type for range patterns
1414
--> $DIR/nested.rs:15:35
1515
|
1616
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
@@ -22,7 +22,7 @@ note: range patterns only support integers
2222
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
2323
| ^^^^^
2424

25-
error: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
25+
error: `(i32) is 1..` is not a valid base type for range patterns
2626
--> $DIR/nested.rs:19:35
2727
|
2828
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
@@ -62,27 +62,27 @@ error[E0308]: mismatched types
6262
--> $DIR/nested.rs:10:63
6363
|
6464
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
65-
| ^ expected `(u32) is 1..=u32::MAX`, found integer
65+
| ^ expected `(u32) is 1..`, found integer
6666
|
67-
= note: expected pattern type `(u32) is 1..=u32::MAX`
67+
= note: expected pattern type `(u32) is 1..`
6868
found type `{integer}`
6969

7070
error[E0308]: mismatched types
7171
--> $DIR/nested.rs:15:67
7272
|
7373
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
74-
| ^^ expected `(i32) is 1..=i32::MAX`, found integer
74+
| ^^ expected `(i32) is 1..`, found integer
7575
|
76-
= note: expected pattern type `(i32) is 1..=i32::MAX`
76+
= note: expected pattern type `(i32) is 1..`
7777
found type `{integer}`
7878

7979
error[E0308]: mismatched types
8080
--> $DIR/nested.rs:19:66
8181
|
8282
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
83-
| ^ expected `(i32) is 1..=i32::MAX`, found integer
83+
| ^ expected `(i32) is 1..`, found integer
8484
|
85-
= note: expected pattern type `(i32) is 1..=i32::MAX`
85+
= note: expected pattern type `(i32) is 1..`
8686
found type `{integer}`
8787

8888
error[E0308]: mismatched types

tests/ui/type/pattern_types/range_patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ error: layout_of(NonZero<u32>) = Layout {
4343
LL | type X = std::num::NonZeroU32;
4444
| ^^^^^^
4545

46-
error: layout_of((u32) is 1..=u32::MAX) = Layout {
46+
error: layout_of((u32) is 1..) = Layout {
4747
size: Size(4 bytes),
4848
align: AbiAndPrefAlign {
4949
abi: Align(4 bytes),
@@ -81,7 +81,7 @@ error: layout_of((u32) is 1..=u32::MAX) = Layout {
8181
LL | type Y = pattern_type!(u32 is 1..);
8282
| ^^^^^^
8383

84-
error: layout_of(Option<(u32) is 1..=u32::MAX>) = Layout {
84+
error: layout_of(Option<(u32) is 1..>) = Layout {
8585
size: Size(4 bytes),
8686
align: AbiAndPrefAlign {
8787
abi: Align(4 bytes),

tests/ui/type/pattern_types/range_patterns_trait_impls2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
44
LL | impl Eq for Y {}
55
| ^^^^^^^^^^^^-
66
| |
7-
| `(u32) is 1..=u32::MAX` is not defined in the current crate
7+
| `(u32) is 1..` is not defined in the current crate
88
|
99
= note: impl doesn't have any local type before any uncovered type parameters
1010
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules

tests/ui/type/pattern_types/range_patterns_unusable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
44
LL | let _: Option<u32> = unsafe { std::mem::transmute(z) };
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `Option<(u32) is 1..=u32::MAX>` (32 bits)
7+
= note: source type: `Option<(u32) is 1..>` (32 bits)
88
= note: target type: `Option<u32>` (64 bits)
99

1010
error: aborting due to 1 previous error

tests/ui/type/pattern_types/range_patterns_unusable_math.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ type Z = Option<pattern_type!(u32 is 1..)>;
1111

1212
fn main() {
1313
let x: Y = unsafe { std::mem::transmute(42_u32) };
14-
let x = x + 1_u32; //~ ERROR cannot add `u32` to `(u32) is 1..=u32::MAX`
14+
let x = x + 1_u32; //~ ERROR cannot add `u32` to `(u32) is 1..`
1515
}

tests/ui/type/pattern_types/range_patterns_unusable_math.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0369]: cannot add `u32` to `(u32) is 1..=u32::MAX`
1+
error[E0369]: cannot add `u32` to `(u32) is 1..`
22
--> $DIR/range_patterns_unusable_math.rs:14:15
33
|
44
LL | let x = x + 1_u32;
55
| - ^ ----- u32
66
| |
7-
| (u32) is 1..=u32::MAX
7+
| (u32) is 1..
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)