Skip to content

Commit 614e19f

Browse files
committed
Apply suggestions
- Move `assert_failed` to core::panicking` - Make `assert_failed` use an enum instead of a string
1 parent 2ee073b commit 614e19f

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

core/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,6 @@ mod macros;
173173

174174
#[macro_use]
175175
mod internal_macros;
176-
#[doc(hidden)]
177-
#[unstable(
178-
feature = "macros_internals",
179-
reason = "macros implementation detail",
180-
issue = "none"
181-
)]
182-
pub use macros::internals as macros_internals;
183176

184177
#[path = "num/shells/int_macros.rs"]
185178
#[macro_use]

core/src/macros/internals.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

core/src/macros/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
2-
#[doc(hidden)]
3-
pub mod internals;
4-
51
#[cfg(bootstrap)]
62
#[doc(include = "panic.md")]
73
#[macro_export]
@@ -57,16 +53,17 @@ macro_rules! panic {
5753
/// ```
5854
#[macro_export]
5955
#[stable(feature = "rust1", since = "1.0.0")]
60-
#[allow_internal_unstable(macros_internals)]
56+
#[allow_internal_unstable(core_panic)]
6157
macro_rules! assert_eq {
6258
($left:expr, $right:expr $(,)?) => ({
6359
match (&$left, &$right) {
6460
(left_val, right_val) => {
6561
if !(*left_val == *right_val) {
62+
let kind = $crate::panicking::AssertKind::Eq;
6663
// The reborrows below are intentional. Without them, the stack slot for the
6764
// borrow is initialized even before the values are compared, leading to a
6865
// noticeable slow down.
69-
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::None);
66+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
7067
}
7168
}
7269
}
@@ -75,10 +72,11 @@ macro_rules! assert_eq {
7572
match (&$left, &$right) {
7673
(left_val, right_val) => {
7774
if !(*left_val == *right_val) {
75+
let kind = $crate::panicking::AssertKind::Eq;
7876
// The reborrows below are intentional. Without them, the stack slot for the
7977
// borrow is initialized even before the values are compared, leading to a
8078
// noticeable slow down.
81-
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
79+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
8280
}
8381
}
8482
}
@@ -104,16 +102,17 @@ macro_rules! assert_eq {
104102
/// ```
105103
#[macro_export]
106104
#[stable(feature = "assert_ne", since = "1.13.0")]
107-
#[allow_internal_unstable(macros_internals)]
105+
#[allow_internal_unstable(core_panic)]
108106
macro_rules! assert_ne {
109107
($left:expr, $right:expr $(,)?) => ({
110108
match (&$left, &$right) {
111109
(left_val, right_val) => {
112110
if *left_val == *right_val {
111+
let kind = $crate::panicking::AssertKind::Ne;
113112
// The reborrows below are intentional. Without them, the stack slot for the
114113
// borrow is initialized even before the values are compared, leading to a
115114
// noticeable slow down.
116-
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::None);
115+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
117116
}
118117
}
119118
}
@@ -122,10 +121,11 @@ macro_rules! assert_ne {
122121
match (&($left), &($right)) {
123122
(left_val, right_val) => {
124123
if *left_val == *right_val {
124+
let kind = $crate::panicking::AssertKind::Ne;
125125
// The reborrows below are intentional. Without them, the stack slot for the
126126
// borrow is initialized even before the values are compared, leading to a
127127
// noticeable slow down.
128-
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
128+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
129129
}
130130
}
131131
}

core/src/panicking.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,52 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9191
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
9292
unsafe { panic_impl(&pi) }
9393
}
94+
95+
#[derive(Debug)]
96+
pub enum AssertKind {
97+
Eq,
98+
Ne,
99+
}
100+
101+
/// Internal function for `assert_eq!` and `assert_ne!` macros
102+
#[cold]
103+
#[track_caller]
104+
pub fn assert_failed<T, U>(
105+
kind: AssertKind,
106+
left: &T,
107+
right: &U,
108+
args: Option<fmt::Arguments<'_>>,
109+
) -> !
110+
where
111+
T: fmt::Debug + ?Sized,
112+
U: fmt::Debug + ?Sized,
113+
{
114+
#[track_caller]
115+
fn inner(
116+
kind: AssertKind,
117+
left: &dyn fmt::Debug,
118+
right: &dyn fmt::Debug,
119+
args: Option<fmt::Arguments<'_>>,
120+
) -> ! {
121+
let op = match kind {
122+
AssertKind::Eq => "==",
123+
AssertKind::Ne => "!=",
124+
};
125+
126+
match args {
127+
Some(args) => panic!(
128+
r#"assertion failed: `(left {} right)`
129+
left: `{:?}`,
130+
right: `{:?}: {}`"#,
131+
op, left, right, args
132+
),
133+
None => panic!(
134+
r#"assertion failed: `(left {} right)`
135+
left: `{:?}`,
136+
right: `{:?}`"#,
137+
op, left, right,
138+
),
139+
}
140+
}
141+
inner(kind, &left, &right, args)
142+
}

0 commit comments

Comments
 (0)