Skip to content

Commit 54bb987

Browse files
committed
move const_panic/assert macros into core::panic module (since they are just internal helpers)
1 parent 5605568 commit 54bb987

19 files changed

+300
-310
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/ffi/c_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl CStr {
413413
#[rustc_allow_const_fn_unstable(const_eval_select)]
414414
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
415415
const_eval_select!(
416-
(bytes: &[u8]) -> &CStr:
416+
@capture { bytes: &[u8] } -> &CStr:
417417
if const {
418418
// Saturating so that an empty slice panics in the assert with a good
419419
// message, not here due to underflow.
@@ -735,7 +735,7 @@ impl AsRef<CStr> for CStr {
735735
#[rustc_allow_const_fn_unstable(const_eval_select)]
736736
const unsafe fn strlen(ptr: *const c_char) -> usize {
737737
const_eval_select!(
738-
(s: *const c_char = ptr) -> usize:
738+
@capture { s: *const c_char = ptr } -> usize:
739739
if const {
740740
let mut len = 0;
741741

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -2791,19 +2791,25 @@ where
27912791
/// A macro to make it easier to invoke const_eval_select. Use as follows:
27922792
/// ```rust,ignore (just a macro example)
27932793
/// const_eval_select!(
2794-
/// #[inline]
2795-
/// (arg1: i32 = some_expr, arg2: T = other_expr) -> U:
2796-
/// if const {
2794+
/// @capture { arg1: i32 = some_expr, arg2: T = other_expr } -> U:
2795+
/// #[attributes_for_both_functions]
2796+
/// if const #[attributes_for_const_arm] {
27972797
/// // Compile-time code goes here.
2798-
/// } else {
2798+
/// } else #[attributes_for_runtime_arm] {
27992799
/// // Run-time code goes here.
28002800
/// }
28012801
/// )
28022802
/// ```
2803+
/// The `@capture` block declares which surrounding variables / expressions can be
2804+
/// used inside the `if const`.
2805+
/// Note that the two arms of this `if` really each become their own function, which is why the
2806+
/// macro supports setting attributes (like `#[inline]`) for those functions.
2807+
///
2808+
/// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
28032809
pub(crate) macro const_eval_select {
28042810
(
2811+
@capture { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
28052812
$(#[$attr:meta])*
2806-
($($arg:ident : $ty:ty = $val:expr),* $(,)?) $( -> $ret:ty )?:
28072813
if const
28082814
$(#[$compiletime_attr:meta])* $compiletime:block
28092815
else
@@ -2829,16 +2835,16 @@ pub(crate) macro const_eval_select {
28292835
// We support leaving away the `val` expressions for *all* arguments
28302836
// (but not for *some* arguments, that's too tricky).
28312837
(
2838+
@capture { $($arg:ident : $ty:ty),* $(,)? } $( -> $ret:ty )? :
28322839
$(#[$attr:meta])*
2833-
($($arg:ident : $ty:ty),* $(,)?) -> $ret:ty:
28342840
if const
28352841
$(#[$compiletime_attr:meta])* $compiletime:block
28362842
else
28372843
$(#[$runtime_attr:meta])* $runtime:block
28382844
) => {
28392845
$crate::intrinsics::const_eval_select!(
2846+
@capture { $($arg : $ty = $arg),* } $(-> $ret)? :
28402847
$(#[$attr])*
2841-
($($arg : $ty = $arg),*) -> $ret:
28422848
if const
28432849
$(#[$compiletime_attr])* $compiletime
28442850
else
@@ -3794,7 +3800,7 @@ pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize
37943800
}
37953801

37963802
const_eval_select!(
3797-
(ptr: *const (), align: usize):
3803+
@capture { ptr: *const (), align: usize}:
37983804
if const {
37993805
// Do nothing.
38003806
} 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+
@capture { $($arg: $ty),* } -> !:
215+
#[inline]
216+
#[track_caller]
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/panicking.rs

+31-33
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)]
3030

3131
use crate::fmt;
32+
use crate::intrinsics::const_eval_select;
3233
use crate::panic::{Location, PanicInfo};
3334

3435
#[cfg(feature = "panic_immediate_abort")]
@@ -89,40 +90,37 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
8990
#[cfg_attr(not(bootstrap), rustc_const_stable_indirect)] // must follow stable const rules since it is exposed to stable
9091
#[rustc_allow_const_fn_unstable(const_eval_select)]
9192
pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! {
92-
#[inline] // this should always be inlined into `panic_nounwind_fmt`
93-
#[track_caller]
94-
fn runtime(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! {
95-
if cfg!(feature = "panic_immediate_abort") {
96-
super::intrinsics::abort()
93+
const_eval_select!(
94+
@capture { fmt: fmt::Arguments<'_>, force_no_backtrace: bool } -> !:
95+
#[inline] // this should always be inlined into `panic_nounwind_fmt`
96+
#[track_caller]
97+
if const {
98+
// We don't unwind anyway at compile-time so we can call the regular `panic_fmt`.
99+
panic_fmt(fmt)
100+
} else {
101+
if cfg!(feature = "panic_immediate_abort") {
102+
super::intrinsics::abort()
103+
}
104+
105+
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
106+
// that gets resolved to the `#[panic_handler]` function.
107+
extern "Rust" {
108+
#[lang = "panic_impl"]
109+
fn panic_impl(pi: &PanicInfo<'_>) -> !;
110+
}
111+
112+
// PanicInfo with the `can_unwind` flag set to false forces an abort.
113+
let pi = PanicInfo::new(
114+
&fmt,
115+
Location::caller(),
116+
/* can_unwind */ false,
117+
force_no_backtrace,
118+
);
119+
120+
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
121+
unsafe { panic_impl(&pi) }
97122
}
98-
99-
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
100-
// that gets resolved to the `#[panic_handler]` function.
101-
extern "Rust" {
102-
#[lang = "panic_impl"]
103-
fn panic_impl(pi: &PanicInfo<'_>) -> !;
104-
}
105-
106-
// PanicInfo with the `can_unwind` flag set to false forces an abort.
107-
let pi = PanicInfo::new(
108-
&fmt,
109-
Location::caller(),
110-
/* can_unwind */ false,
111-
force_no_backtrace,
112-
);
113-
114-
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
115-
unsafe { panic_impl(&pi) }
116-
}
117-
118-
#[inline]
119-
#[track_caller]
120-
const fn comptime(fmt: fmt::Arguments<'_>, _force_no_backtrace: bool) -> ! {
121-
// We don't unwind anyway at compile-time so we can call the regular `panic_fmt`.
122-
panic_fmt(fmt);
123-
}
124-
125-
super::intrinsics::const_eval_select((fmt, force_no_backtrace), comptime, runtime);
123+
)
126124
}
127125

128126
// Next we define a bunch of higher-level wrappers that all bottom out in the two core functions

0 commit comments

Comments
 (0)