about summary refs log tree commit diff
path: root/library/core/src/slice/index.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-11-03 16:20:51 +0000
committerbors <bors@rust-lang.org>2024-11-03 16:20:51 +0000
commite3a918ece026cec748fc64af5b4983095b46097e (patch)
tree342db9dd6cf4d8cf8a1d4a79c6df4d8dea178e46 /library/core/src/slice/index.rs
parent7028d9318fadc20e5e3058d52e44d785d31a6aaa (diff)
parentbc757f9034c75a0e812c637f97432b222a150781 (diff)
downloadrust-e3a918ece026cec748fc64af5b4983095b46097e.tar.gz
rust-e3a918ece026cec748fc64af5b4983095b46097e.zip
Auto merge of #132542 - RalfJung:const_panic, r=tgross35
add const_panic macro to make it easier to fall back to non-formatting panic in const

Suggested by `@tgross35`

r? `@tgross35`
Diffstat (limited to 'library/core/src/slice/index.rs')
-rw-r--r--library/core/src/slice/index.rs68
1 files changed, 19 insertions, 49 deletions
diff --git a/library/core/src/slice/index.rs b/library/core/src/slice/index.rs
index 231ab7396ad..ebb4bdb1449 100644
--- a/library/core/src/slice/index.rs
+++ b/library/core/src/slice/index.rs
@@ -1,6 +1,6 @@
 //! Indexing implementations for `[T]`.
 
-use crate::intrinsics::const_eval_select;
+use crate::macros::const_panic;
 use crate::ub_checks::assert_unsafe_precondition;
 use crate::{ops, range};
 
@@ -31,67 +31,37 @@ where
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
 #[cfg_attr(feature = "panic_immediate_abort", inline)]
 #[track_caller]
-#[rustc_allow_const_fn_unstable(const_eval_select)]
 const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
-    // FIXME(const-hack): once integer formatting in panics is possible, we
-    // should use the same implementation at compiletime and runtime.
-    const_eval_select((index, len), slice_start_index_len_fail_ct, slice_start_index_len_fail_rt)
-}
-
-#[inline]
-#[track_caller]
-fn slice_start_index_len_fail_rt(index: usize, len: usize) -> ! {
-    panic!("range start index {index} out of range for slice of length {len}");
-}
-
-#[inline]
-#[track_caller]
-const fn slice_start_index_len_fail_ct(_: usize, _: usize) -> ! {
-    panic!("slice start index is out of range for slice");
+    const_panic!(
+        "slice start index is out of range for slice",
+        "range start index {index} out of range for slice of length {len}",
+        index: usize,
+        len: usize,
+    )
 }
 
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
 #[cfg_attr(feature = "panic_immediate_abort", inline)]
 #[track_caller]
-#[rustc_allow_const_fn_unstable(const_eval_select)]
 const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
-    // FIXME(const-hack): once integer formatting in panics is possible, we
-    // should use the same implementation at compiletime and runtime.
-    const_eval_select((index, len), slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
-}
-
-#[inline]
-#[track_caller]
-fn slice_end_index_len_fail_rt(index: usize, len: usize) -> ! {
-    panic!("range end index {index} out of range for slice of length {len}");
-}
-
-#[inline]
-#[track_caller]
-const fn slice_end_index_len_fail_ct(_: usize, _: usize) -> ! {
-    panic!("slice end index is out of range for slice");
+    const_panic!(
+        "slice end index is out of range for slice",
+        "range end index {index} out of range for slice of length {len}",
+        index: usize,
+        len: usize,
+    )
 }
 
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
 #[cfg_attr(feature = "panic_immediate_abort", inline)]
 #[track_caller]
-#[rustc_allow_const_fn_unstable(const_eval_select)]
 const fn slice_index_order_fail(index: usize, end: usize) -> ! {
-    // FIXME(const-hack): once integer formatting in panics is possible, we
-    // should use the same implementation at compiletime and runtime.
-    const_eval_select((index, end), slice_index_order_fail_ct, slice_index_order_fail_rt)
-}
-
-#[inline]
-#[track_caller]
-fn slice_index_order_fail_rt(index: usize, end: usize) -> ! {
-    panic!("slice index starts at {index} but ends at {end}");
-}
-
-#[inline]
-#[track_caller]
-const fn slice_index_order_fail_ct(_: usize, _: usize) -> ! {
-    panic!("slice index start is larger than end");
+    const_panic!(
+        "slice index start is larger than end",
+        "slice index starts at {index} but ends at {end}",
+        index: usize,
+        end: usize,
+    )
 }
 
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]