about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-02-05 16:21:54 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-03-06 10:50:23 +0000
commita2c1211b6de0c1a93c3ff1f97c49acc237f620a6 (patch)
treeb874786b63e39044bed6facda1438123c3d10733
parente8f7a382be12a812b7d73f58967f740a0cf9d9af (diff)
downloadrust-a2c1211b6de0c1a93c3ff1f97c49acc237f620a6.tar.gz
rust-a2c1211b6de0c1a93c3ff1f97c49acc237f620a6.zip
Hide the end of ranges in pretty printing if it's also the maximum of the type
-rw-r--r--compiler/rustc_middle/src/ty/pattern.rs24
-rw-r--r--tests/codegen/pattern_type_symbols.rs4
-rw-r--r--tests/mir-opt/pattern_types.main.PreCodegen.after.mir4
-rw-r--r--tests/mir-opt/pattern_types.rs4
-rw-r--r--tests/ui/lint/clashing-extern-fn.stderr2
-rw-r--r--tests/ui/type/pattern_types/nested.stderr28
-rw-r--r--tests/ui/type/pattern_types/range_patterns.stderr4
-rw-r--r--tests/ui/type/pattern_types/range_patterns_unusable.stderr2
-rw-r--r--tests/ui/type/pattern_types/range_patterns_unusable_math.rs2
-rw-r--r--tests/ui/type/pattern_types/range_patterns_unusable_math.stderr4
10 files changed, 50 insertions, 28 deletions
diff --git a/compiler/rustc_middle/src/ty/pattern.rs b/compiler/rustc_middle/src/ty/pattern.rs
index 89dadb36ad6..4cad1ab2099 100644
--- a/compiler/rustc_middle/src/ty/pattern.rs
+++ b/compiler/rustc_middle/src/ty/pattern.rs
@@ -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}")
             }
         }
     }
diff --git a/tests/codegen/pattern_type_symbols.rs b/tests/codegen/pattern_type_symbols.rs
index b504a3508f9..e86a9ef27de 100644
--- a/tests/codegen/pattern_type_symbols.rs
+++ b/tests/codegen/pattern_type_symbols.rs
@@ -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>();
 }
diff --git a/tests/mir-opt/pattern_types.main.PreCodegen.after.mir b/tests/mir-opt/pattern_types.main.PreCodegen.after.mir
index 8214ff61d75..cc01f51973c 100644
--- a/tests/mir-opt/pattern_types.main.PreCodegen.after.mir
+++ b/tests/mir-opt/pattern_types.main.PreCodegen.after.mir
@@ -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..};
         }
     }
 
diff --git a/tests/mir-opt/pattern_types.rs b/tests/mir-opt/pattern_types.rs
index 6d539199345..d5847b95f73 100644
--- a/tests/mir-opt/pattern_types.rs
+++ b/tests/mir-opt/pattern_types.rs
@@ -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) };
 }
diff --git a/tests/ui/lint/clashing-extern-fn.stderr b/tests/ui/lint/clashing-extern-fn.stderr
index 4e9f6ea3f78..0c27547a6ed 100644
--- a/tests/ui/lint/clashing-extern-fn.stderr
+++ b/tests/ui/lint/clashing-extern-fn.stderr
@@ -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..)>;
diff --git a/tests/ui/type/pattern_types/nested.stderr b/tests/ui/type/pattern_types/nested.stderr
index 04aacabc136..f79d12bc3f3 100644
--- a/tests/ui/type/pattern_types/nested.stderr
+++ b/tests/ui/type/pattern_types/nested.stderr
@@ -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
diff --git a/tests/ui/type/pattern_types/range_patterns.stderr b/tests/ui/type/pattern_types/range_patterns.stderr
index 43ae7e870f0..512181c22d5 100644
--- a/tests/ui/type/pattern_types/range_patterns.stderr
+++ b/tests/ui/type/pattern_types/range_patterns.stderr
@@ -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),
diff --git a/tests/ui/type/pattern_types/range_patterns_unusable.stderr b/tests/ui/type/pattern_types/range_patterns_unusable.stderr
index a9558852eb2..7daa41d7081 100644
--- a/tests/ui/type/pattern_types/range_patterns_unusable.stderr
+++ b/tests/ui/type/pattern_types/range_patterns_unusable.stderr
@@ -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
diff --git a/tests/ui/type/pattern_types/range_patterns_unusable_math.rs b/tests/ui/type/pattern_types/range_patterns_unusable_math.rs
index 54ad6096c57..6125063699b 100644
--- a/tests/ui/type/pattern_types/range_patterns_unusable_math.rs
+++ b/tests/ui/type/pattern_types/range_patterns_unusable_math.rs
@@ -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..`
 }
diff --git a/tests/ui/type/pattern_types/range_patterns_unusable_math.stderr b/tests/ui/type/pattern_types/range_patterns_unusable_math.stderr
index 68ddd63cd53..a64f1db3176 100644
--- a/tests/ui/type/pattern_types/range_patterns_unusable_math.stderr
+++ b/tests/ui/type/pattern_types/range_patterns_unusable_math.stderr
@@ -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