Hide the end of ranges in pretty printing if it's also the maximum of the type

This commit is contained in:
Oli Scherer 2025-02-05 16:21:54 +00:00
parent e8f7a382be
commit a2c1211b6d
10 changed files with 50 additions and 28 deletions

View File

@ -27,7 +27,29 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
PatternKind::Range { start, end } => {
write!(f, "{start}..={end}")
write!(f, "{start}")?;
if let Some(c) = end.try_to_value() {
let end = c.valtree.unwrap_leaf();
let size = end.size();
let max = match c.ty.kind() {
ty::Int(_) => {
Some(ty::ScalarInt::truncate_from_int(size.signed_int_max(), size))
}
ty::Uint(_) => {
Some(ty::ScalarInt::truncate_from_uint(size.unsigned_int_max(), size))
}
ty::Char => Some(ty::ScalarInt::truncate_from_uint(char::MAX, size)),
_ => None,
};
if let Some((max, _)) = max
&& end == max
{
return write!(f, "..");
}
}
write!(f, "..={end}")
}
}
}

View File

@ -16,7 +16,7 @@ pub fn bar() {
// CHECK: call pattern_type_symbols::foo::<u32>
// CHECK: call void @_RINvC[[CRATE_IDENT:[a-zA-Z0-9]{12}]]_20pattern_type_symbols3foomEB2_
foo::<u32>();
// CHECK: call pattern_type_symbols::foo::<(u32, [(); 0], [(); 999999999], [(); true])>
// CHECK: call void @_RINvC[[CRATE_IDENT]]_20pattern_type_symbols3fooTmAum0_Aum3b9ac9ff_Aub1_EEB2_
// CHECK: call pattern_type_symbols::foo::<(u32, [(); 0], [(); 999999999])>
// CHECK: call void @_RINvC[[CRATE_IDENT]]_20pattern_type_symbols3fooTmAum0_Aum3b9ac9ff_EEB2_
foo::<NanoU32>();
}

View File

@ -3,9 +3,9 @@
fn main() -> () {
let mut _0: ();
scope 1 {
debug x => const 2_u32 is 1..=u32::MAX;
debug x => const 2_u32 is 1..;
scope 2 {
debug y => const {transmute(0x00000000): (u32) is 1..=u32::MAX};
debug y => const {transmute(0x00000000): (u32) is 1..};
}
}

View File

@ -5,8 +5,8 @@ use std::pat::pattern_type;
// EMIT_MIR pattern_types.main.PreCodegen.after.mir
fn main() {
// CHECK: debug x => const 2_u32 is 1..=u32::MAX
// CHECK: debug x => const 2_u32 is 1..
let x: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(2) };
// CHECK: debug y => const {transmute(0x00000000): (u32) is 1..=u32::MAX}
// CHECK: debug y => const {transmute(0x00000000): (u32) is 1..}
let y: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
}

View File

@ -17,7 +17,7 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
warning: `extern` block uses type `Option<(usize) is 0..=usize::MAX>`, which is not FFI-safe
warning: `extern` block uses type `Option<(usize) is 0..>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:502:54
|
LL | fn pt_non_zero_usize_opt_full_range() -> Option<pattern_type!(usize is 0..)>;

View File

@ -2,18 +2,18 @@ error[E0308]: mismatched types
--> $DIR/nested.rs:10:63
|
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
| ^ expected `(u32) is 1..=u32::MAX`, found integer
| ^ expected `(u32) is 1..`, found integer
|
= note: expected pattern type `(u32) is 1..=u32::MAX`
= note: expected pattern type `(u32) is 1..`
found type `{integer}`
error[E0277]: `(u32) is 1..=u32::MAX` is not a valid base type for range patterns
error[E0277]: `(u32) is 1..` is not a valid base type for range patterns
--> $DIR/nested.rs:10:34
|
LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
|
= help: the trait `core::pat::RangePattern` is not implemented for `(u32) is 1..=u32::MAX`
= help: the trait `core::pat::RangePattern` is not implemented for `(u32) is 1..`
= help: the following other types implement trait `core::pat::RangePattern`:
char
i128
@ -25,13 +25,13 @@ LL | const BAD_NESTING: pattern_type!(pattern_type!(u32 is 1..) is 0..) = todo!(
u128
and 5 others
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
--> $DIR/nested.rs:15:35
|
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
|
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
= help: the following other types implement trait `core::pat::RangePattern`:
char
i128
@ -47,18 +47,18 @@ error[E0308]: mismatched types
--> $DIR/nested.rs:15:67
|
LL | const BAD_NESTING2: pattern_type!(pattern_type!(i32 is 1..) is ..=-1) = todo!();
| ^^ expected `(i32) is 1..=i32::MAX`, found integer
| ^^ expected `(i32) is 1..`, found integer
|
= note: expected pattern type `(i32) is 1..=i32::MAX`
= note: expected pattern type `(i32) is 1..`
found type `{integer}`
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
--> $DIR/nested.rs:19:35
|
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ only integer types and `char` are supported
|
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
= help: the following other types implement trait `core::pat::RangePattern`:
char
i128
@ -76,10 +76,10 @@ error[E0308]: mismatched types
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
| ^
| |
| expected `(i32) is 1..=i32::MAX`, found integer
| expected `(i32) is 1..`, found integer
| arguments to this function are incorrect
|
= note: expected pattern type `(i32) is 1..=i32::MAX`
= note: expected pattern type `(i32) is 1..`
found type `{integer}`
help: the return type of this call is `{integer}` due to the type of the argument passed
--> $DIR/nested.rs:19:66
@ -89,13 +89,13 @@ LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!
note: method defined here
--> $SRC_DIR/core/src/pat.rs:LL:COL
error[E0277]: `(i32) is 1..=i32::MAX` is not a valid base type for range patterns
error[E0277]: `(i32) is 1..` is not a valid base type for range patterns
--> $DIR/nested.rs:19:66
|
LL | const BAD_NESTING3: pattern_type!(pattern_type!(i32 is 1..) is ..0) = todo!();
| ^ only integer types and `char` are supported
|
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..=i32::MAX`
= help: the trait `core::pat::RangePattern` is not implemented for `(i32) is 1..`
= help: the following other types implement trait `core::pat::RangePattern`:
char
i128

View File

@ -44,7 +44,7 @@ error: layout_of(NonZero<u32>) = Layout {
LL | type X = std::num::NonZeroU32;
| ^^^^^^
error: layout_of((u32) is 1..=u32::MAX) = Layout {
error: layout_of((u32) is 1..) = Layout {
size: Size(4 bytes),
align: AbiAndPrefAlign {
abi: Align(4 bytes),
@ -83,7 +83,7 @@ error: layout_of((u32) is 1..=u32::MAX) = Layout {
LL | type Y = pattern_type!(u32 is 1..);
| ^^^^^^
error: layout_of(Option<(u32) is 1..=u32::MAX>) = Layout {
error: layout_of(Option<(u32) is 1..>) = Layout {
size: Size(4 bytes),
align: AbiAndPrefAlign {
abi: Align(4 bytes),

View File

@ -4,7 +4,7 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
LL | let _: Option<u32> = unsafe { std::mem::transmute(z) };
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `Option<(u32) is 1..=u32::MAX>` (32 bits)
= note: source type: `Option<(u32) is 1..>` (32 bits)
= note: target type: `Option<u32>` (64 bits)
error: aborting due to 1 previous error

View File

@ -11,5 +11,5 @@ type Z = Option<pattern_type!(u32 is 1..)>;
fn main() {
let x: Y = unsafe { std::mem::transmute(42_u32) };
let x = x + 1_u32; //~ ERROR cannot add `u32` to `(u32) is 1..=u32::MAX`
let x = x + 1_u32; //~ ERROR cannot add `u32` to `(u32) is 1..`
}

View File

@ -1,10 +1,10 @@
error[E0369]: cannot add `u32` to `(u32) is 1..=u32::MAX`
error[E0369]: cannot add `u32` to `(u32) is 1..`
--> $DIR/range_patterns_unusable_math.rs:14:15
|
LL | let x = x + 1_u32;
| - ^ ----- u32
| |
| (u32) is 1..=u32::MAX
| (u32) is 1..
error: aborting due to 1 previous error