Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e3bbdd

Browse files
committedFeb 13, 2025
Hide the end of ranges in pretty printing if it's also the maximum of the type
1 parent 4952865 commit 2e3bbdd

9 files changed

+48
-26
lines changed
 

‎compiler/rustc_middle/src/ty/pattern.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,29 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
2727
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2828
match *self {
2929
PatternKind::Range { start, end } => {
30-
write!(f, "{start}..={end}")
30+
write!(f, "{start}")?;
31+
32+
if let Some(c) = end.try_to_value() {
33+
let end = c.valtree.unwrap_leaf();
34+
let size = end.size();
35+
let max = match c.ty.kind() {
36+
ty::Int(_) => {
37+
Some(ty::ScalarInt::truncate_from_int(size.signed_int_max(), size))
38+
}
39+
ty::Uint(_) => {
40+
Some(ty::ScalarInt::truncate_from_uint(size.unsigned_int_max(), size))
41+
}
42+
ty::Char => Some(ty::ScalarInt::truncate_from_uint(char::MAX, size)),
43+
_ => None,
44+
};
45+
if let Some((max, _)) = max
46+
&& end == max
47+
{
48+
return write!(f, "..");
49+
}
50+
}
51+
52+
write!(f, "..={end}")
3153
}
3254
}
3355
}

‎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

+1-1
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..)>;

‎tests/ui/type/pattern_types/nested.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ error[E0308]: mismatched types
22
--> $DIR/nested.rs:10:63
33
|
44
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
5-
| ^ expected `(u32) is 1..=u32::MAX`, found integer
5+
| ^ expected `(u32) is 1..`, found integer
66
|
7-
= note: expected pattern type `(u32) is 1..=u32::MAX`
7+
= note: expected pattern type `(u32) is 1..`
88
found type `{integer}`
99

10-
error[E0277]: `(u32) is 1..=u32::MAX` is not a valid base type for range patterns
10+
error[E0277]: `(u32) is 1..` is not a valid base type for range patterns
1111
--> $DIR/nested.rs:10:34
1212
|
1313
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
1515
|
16-
= help: the trait `core::pat::RangePattern` is not implemented for `(u32) is 1..=u32::MAX`
16+
= help: the trait `core::pat::RangePattern` is not implemented for `(u32) is 1..`
1717
= help: the following other types implement trait `core::pat::RangePattern`:
1818
char
1919
i128
@@ -25,13 +25,13 @@ LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!(
2525
u128
2626
and 5 others
2727

28-
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
28+
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
2929
--> $DIR/nested.rs:15:35
3030
|
3131
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
3333
|
34-
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
34+
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
3535
= help: the following other types implement trait `core::pat::RangePattern`:
3636
char
3737
i128
@@ -47,18 +47,18 @@ error[E0308]: mismatched types
4747
--> $DIR/nested.rs:15:67
4848
|
4949
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
50-
| ^^ expected `(i32) is 1..=i32::MAX`, found integer
50+
| ^^ expected `(i32) is 1..`, found integer
5151
|
52-
= note: expected pattern type `(i32) is 1..=i32::MAX`
52+
= note: expected pattern type `(i32) is 1..`
5353
found type `{integer}`
5454

55-
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
55+
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
5656
--> $DIR/nested.rs:19:35
5757
|
5858
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
6060
|
61-
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
61+
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
6262
= help: the following other types implement trait `core::pat::RangePattern`:
6363
char
6464
i128
@@ -76,10 +76,10 @@ error[E0308]: mismatched types
7676
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
7777
| ^
7878
| |
79-
| expected `(i32) is 1..=i32::MAX`, found integer
79+
| expected `(i32) is 1..`, found integer
8080
| arguments to this function are incorrect
8181
|
82-
= note: expected pattern type `(i32) is 1..=i32::MAX`
82+
= note: expected pattern type `(i32) is 1..`
8383
found type `{integer}`
8484
help: the return type of this call is `{integer}` due to the type of the argument passed
8585
--> $DIR/nested.rs:19:66
@@ -89,13 +89,13 @@ LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!
8989
note: method defined here
9090
--> $SRC_DIR/core/src/pat.rs:LL:COL
9191

92-
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
92+
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
9393
--> $DIR/nested.rs:19:66
9494
|
9595
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
9696
| ^ only integer types and `char` are supported
9797
|
98-
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
98+
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
9999
= help: the following other types implement trait `core::pat::RangePattern`:
100100
char
101101
i128

‎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_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)
Failed to load comments.