Skip to content

Commit c54098f

Browse files
committed
Constify assert_{eq,ne,matches}
1 parent bf39266 commit c54098f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

library/core/src/macros/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! panic {
3131
/// ```
3232
#[macro_export]
3333
#[stable(feature = "rust1", since = "1.0.0")]
34-
#[allow_internal_unstable(core_panic)]
34+
#[allow_internal_unstable(core_panic, const_format_args)]
3535
macro_rules! assert_eq {
3636
($left:expr, $right:expr $(,)?) => ({
3737
match (&$left, &$right) {
@@ -54,7 +54,7 @@ macro_rules! assert_eq {
5454
// The reborrows below are intentional. Without them, the stack slot for the
5555
// borrow is initialized even before the values are compared, leading to a
5656
// noticeable slow down.
57-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
57+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::const_format_args!($($arg)+)));
5858
}
5959
}
6060
}
@@ -80,7 +80,7 @@ macro_rules! assert_eq {
8080
/// ```
8181
#[macro_export]
8282
#[stable(feature = "assert_ne", since = "1.13.0")]
83-
#[allow_internal_unstable(core_panic)]
83+
#[allow_internal_unstable(core_panic, const_format_args)]
8484
macro_rules! assert_ne {
8585
($left:expr, $right:expr $(,)?) => ({
8686
match (&$left, &$right) {
@@ -103,7 +103,7 @@ macro_rules! assert_ne {
103103
// The reborrows below are intentional. Without them, the stack slot for the
104104
// borrow is initialized even before the values are compared, leading to a
105105
// noticeable slow down.
106-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
106+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::const_format_args!($($arg)+)));
107107
}
108108
}
109109
}
@@ -137,7 +137,7 @@ macro_rules! assert_ne {
137137
/// assert_matches!(c, Ok(x) | Err(x) if x.len() < 100);
138138
/// ```
139139
#[unstable(feature = "assert_matches", issue = "82775")]
140-
#[allow_internal_unstable(core_panic)]
140+
#[allow_internal_unstable(core_panic, const_format_args)]
141141
#[rustc_macro_transparency = "semitransparent"]
142142
pub macro assert_matches {
143143
($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => ({
@@ -159,7 +159,7 @@ pub macro assert_matches {
159159
$crate::panicking::assert_matches_failed(
160160
left_val,
161161
$crate::stringify!($($pattern)|+ $(if $guard)?),
162-
$crate::option::Option::Some($crate::format_args!($($arg)+))
162+
$crate::option::Option::Some($crate::const_format_args!($($arg)+))
163163
);
164164
}
165165
}

library/core/src/panicking.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ pub enum AssertKind {
127127
/// Internal function for `assert_eq!` and `assert_ne!` macros
128128
#[cold]
129129
#[track_caller]
130+
#[rustc_const_unstable(feature = "const_panic_extra", issue = "none")]
130131
#[doc(hidden)]
131-
pub fn assert_failed<T, U>(
132+
pub const fn assert_failed<T, U>(
132133
kind: AssertKind,
133134
left: &T,
134135
right: &U,
@@ -144,8 +145,9 @@ where
144145
/// Internal function for `assert_match!`
145146
#[cold]
146147
#[track_caller]
148+
#[rustc_const_unstable(feature = "const_panic_extra", issue = "none")]
147149
#[doc(hidden)]
148-
pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
150+
pub const fn assert_matches_failed<T: fmt::Debug + ?Sized>(
149151
left: &T,
150152
right: &str,
151153
args: Option<fmt::Arguments<'_>>,
@@ -162,7 +164,7 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
162164

163165
/// Non-generic version of the above functions, to avoid code bloat.
164166
#[track_caller]
165-
fn assert_failed_inner(
167+
const fn assert_failed_inner(
166168
kind: AssertKind,
167169
left: &dyn fmt::Debug,
168170
right: &dyn fmt::Debug,

0 commit comments

Comments
 (0)