Skip to content

Commit 6e70d4d

Browse files
committed
move const_panic/assert macros into core::panic module (since they are just internal helpers)
1 parent 9456d5c commit 6e70d4d

File tree

11 files changed

+70
-70
lines changed

11 files changed

+70
-70
lines changed

Diff for: library/core/src/char/methods.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! impl char {}
22
33
use super::*;
4-
use crate::macros::const_panic;
4+
use crate::panic::const_panic;
55
use crate::slice;
66
use crate::str::from_utf8_unchecked_mut;
77
use crate::unicode::printable::is_printable;

Diff for: library/core/src/intrinsics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,7 @@ where
28032803
pub(crate) macro const_eval_select {
28042804
(
28052805
$(#[$attr:meta])*
2806-
($($arg:ident : $ty:ty = $val:expr),* $(,)?) $( -> $ret:ty )?:
2806+
($($arg:ident : $ty:ty = $val:expr),* $(,)?) $( -> $ret:ty )? :
28072807
if const
28082808
$(#[$compiletime_attr:meta])* $compiletime:block
28092809
else
@@ -2830,15 +2830,15 @@ pub(crate) macro const_eval_select {
28302830
// (but not for *some* arguments, that's too tricky).
28312831
(
28322832
$(#[$attr:meta])*
2833-
($($arg:ident : $ty:ty),* $(,)?) -> $ret:ty:
2833+
($($arg:ident : $ty:ty),* $(,)?) $( -> $ret:ty )? :
28342834
if const
28352835
$(#[$compiletime_attr:meta])* $compiletime:block
28362836
else
28372837
$(#[$runtime_attr:meta])* $runtime:block
28382838
) => {
28392839
$crate::intrinsics::const_eval_select!(
28402840
$(#[$attr])*
2841-
($($arg : $ty = $arg),*) -> $ret:
2841+
($($arg : $ty = $arg),*) $(-> $ret)? :
28422842
if const
28432843
$(#[$compiletime_attr])* $compiletime
28442844
else

Diff for: library/core/src/macros/mod.rs

-58
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,6 @@ macro_rules! panic {
1212
};
1313
}
1414

15-
/// Helper macro for panicking in a `const fn`.
16-
/// Invoke as:
17-
/// ```rust,ignore (just an example)
18-
/// core::macros::const_panic!("boring message", "flavored message {a} {b:?}", a: u32 = foo.len(), b: Something = bar);
19-
/// ```
20-
/// where the first message will be printed in const-eval,
21-
/// and the second message will be printed at runtime.
22-
// All uses of this macro are FIXME(const-hack).
23-
#[unstable(feature = "panic_internals", issue = "none")]
24-
#[doc(hidden)]
25-
pub macro const_panic {
26-
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
27-
// Wrap call to `const_eval_select` in a function so that we can
28-
// add the `rustc_allow_const_fn_unstable`. This is okay to do
29-
// because both variants will panic, just with different messages.
30-
#[rustc_allow_const_fn_unstable(const_eval_select)]
31-
#[inline(always)]
32-
#[track_caller]
33-
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
34-
const fn do_panic($($arg: $ty),*) -> ! {
35-
$crate::intrinsics::const_eval_select!(
36-
#[inline]
37-
#[track_caller]
38-
($($arg: $ty),*) -> !:
39-
if const {
40-
$crate::panic!($const_msg)
41-
} else {
42-
$crate::panic!($runtime_msg)
43-
}
44-
)
45-
}
46-
47-
do_panic($($val),*)
48-
}},
49-
// We support leaving away the `val` expressions for *all* arguments
50-
// (but not for *some* arguments, that's too tricky).
51-
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {
52-
$crate::macros::const_panic!(
53-
$const_msg,
54-
$runtime_msg,
55-
$($arg: $ty = $arg),*
56-
)
57-
},
58-
}
59-
6015
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
6116
///
6217
/// Assertions are always checked in both debug and release builds, and cannot
@@ -241,19 +196,6 @@ pub macro assert_matches {
241196
},
242197
}
243198

244-
/// A version of `assert` that prints a non-formatting message in const contexts.
245-
///
246-
/// See [`const_panic!`].
247-
#[unstable(feature = "panic_internals", issue = "none")]
248-
#[doc(hidden)]
249-
pub macro const_assert {
250-
($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
251-
if !$crate::intrinsics::likely($condition) {
252-
$crate::macros::const_panic!($const_msg, $runtime_msg, $($arg)*)
253-
}
254-
}}
255-
}
256-
257199
/// A macro for defining `#[cfg]` match-like statements.
258200
///
259201
/// It is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of

Diff for: library/core/src/num/f128.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use crate::convert::FloatToInt;
1515
#[cfg(not(test))]
1616
use crate::intrinsics;
17-
use crate::macros::const_assert;
1817
use crate::mem;
1918
use crate::num::FpCategory;
19+
use crate::panic::const_assert;
2020

2121
/// Basic mathematical constants.
2222
#[unstable(feature = "f128", issue = "116909")]

Diff for: library/core/src/num/f16.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use crate::convert::FloatToInt;
1515
#[cfg(not(test))]
1616
use crate::intrinsics;
17-
use crate::macros::const_assert;
1817
use crate::mem;
1918
use crate::num::FpCategory;
19+
use crate::panic::const_assert;
2020

2121
/// Basic mathematical constants.
2222
#[unstable(feature = "f16", issue = "116909")]

Diff for: library/core/src/num/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use crate::convert::FloatToInt;
1515
#[cfg(not(test))]
1616
use crate::intrinsics;
17-
use crate::macros::const_assert;
1817
use crate::mem;
1918
use crate::num::FpCategory;
19+
use crate::panic::const_assert;
2020

2121
/// The radix or base of the internal representation of `f32`.
2222
/// Use [`f32::RADIX`] instead.

Diff for: library/core/src/num/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use crate::convert::FloatToInt;
1515
#[cfg(not(test))]
1616
use crate::intrinsics;
17-
use crate::macros::const_assert;
1817
use crate::mem;
1918
use crate::num::FpCategory;
19+
use crate::panic::const_assert;
2020

2121
/// The radix or base of the internal representation of `f64`.
2222
/// Use [`f64::RADIX`] instead.

Diff for: library/core/src/num/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
#![stable(feature = "rust1", since = "1.0.0")]
44

5-
use crate::macros::const_panic;
5+
use crate::panic::const_panic;
66
use crate::str::FromStr;
77
use crate::ub_checks::assert_unsafe_precondition;
88
use crate::{ascii, intrinsics, mem};

Diff for: library/core/src/panic.rs

+58
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,61 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
189189
None
190190
}
191191
}
192+
193+
/// Helper macro for panicking in a `const fn`.
194+
/// Invoke as:
195+
/// ```rust,ignore (just an example)
196+
/// core::macros::const_panic!("boring message", "flavored message {a} {b:?}", a: u32 = foo.len(), b: Something = bar);
197+
/// ```
198+
/// where the first message will be printed in const-eval,
199+
/// and the second message will be printed at runtime.
200+
// All uses of this macro are FIXME(const-hack).
201+
#[unstable(feature = "panic_internals", issue = "none")]
202+
#[doc(hidden)]
203+
pub macro const_panic {
204+
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
205+
// Wrap call to `const_eval_select` in a function so that we can
206+
// add the `rustc_allow_const_fn_unstable`. This is okay to do
207+
// because both variants will panic, just with different messages.
208+
#[rustc_allow_const_fn_unstable(const_eval_select)]
209+
#[inline(always)]
210+
#[track_caller]
211+
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
212+
const fn do_panic($($arg: $ty),*) -> ! {
213+
$crate::intrinsics::const_eval_select!(
214+
#[inline]
215+
#[track_caller]
216+
($($arg: $ty),*) -> !:
217+
if const {
218+
$crate::panic!($const_msg)
219+
} else {
220+
$crate::panic!($runtime_msg)
221+
}
222+
)
223+
}
224+
225+
do_panic($($val),*)
226+
}},
227+
// We support leaving away the `val` expressions for *all* arguments
228+
// (but not for *some* arguments, that's too tricky).
229+
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {
230+
$crate::panic::const_panic!(
231+
$const_msg,
232+
$runtime_msg,
233+
$($arg: $ty = $arg),*
234+
)
235+
},
236+
}
237+
238+
/// A version of `assert` that prints a non-formatting message in const contexts.
239+
///
240+
/// See [`const_panic!`].
241+
#[unstable(feature = "panic_internals", issue = "none")]
242+
#[doc(hidden)]
243+
pub macro const_assert {
244+
($condition: expr, $const_msg:literal, $runtime_msg:literal, $($arg:tt)*) => {{
245+
if !$crate::intrinsics::likely($condition) {
246+
$crate::panic::const_panic!($const_msg, $runtime_msg, $($arg)*)
247+
}
248+
}}
249+
}

Diff for: library/core/src/slice/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Indexing implementations for `[T]`.
22
3-
use crate::macros::const_panic;
3+
use crate::panic::const_panic;
44
use crate::ub_checks::assert_unsafe_precondition;
55
use crate::{ops, range};
66

Diff for: tests/ui/consts/const-ptr-is-null.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ error[E0080]: evaluation of constant value failed
33
|
44
= note: the evaluated program panicked at 'null-ness of this pointer cannot be determined in const context', $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
55
|
6-
note: inside `std::ptr::const_ptr::<impl *const T>::is_null::const_impl`
6+
note: inside `std::ptr::const_ptr::<impl *const T>::is_null::compiletime`
77
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
88
note: inside `std::ptr::const_ptr::<impl *const i32>::is_null`
99
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -12,7 +12,7 @@ note: inside `MAYBE_NULL`
1212
|
1313
LL | assert!(!ptr.wrapping_sub(512).is_null());
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `const_eval_select` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error: aborting due to 1 previous error
1818

0 commit comments

Comments
 (0)