Skip to content

Commit cdfe6b5

Browse files
MakitaTokinyurik
authored andcommitted
stringify original tokens in related macros
resolve conflicts and stringify original tokens accept local changes
1 parent af31311 commit cdfe6b5

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

library/core/src/macros/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ macro_rules! assert_eq {
3939
(left_val, right_val) => {
4040
if !(*left_val == *right_val) {
4141
let kind = $crate::panicking::AssertKind::Eq;
42+
let left_name = stringify!($left);
43+
let right_name = stringify!($right);
4244
// The reborrows below are intentional. Without them, the stack slot for the
4345
// borrow is initialized even before the values are compared, leading to a
4446
// noticeable slow down.
45-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
47+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::None);
4648
}
4749
}
4850
}
@@ -52,10 +54,12 @@ macro_rules! assert_eq {
5254
(left_val, right_val) => {
5355
if !(*left_val == *right_val) {
5456
let kind = $crate::panicking::AssertKind::Eq;
57+
let left_name = stringify!($left);
58+
let right_name = stringify!($right);
5559
// The reborrows below are intentional. Without them, the stack slot for the
5660
// borrow is initialized even before the values are compared, leading to a
5761
// noticeable slow down.
58-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
62+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::Some($crate::format_args!($($arg)+)));
5963
}
6064
}
6165
}
@@ -89,10 +93,12 @@ macro_rules! assert_ne {
8993
(left_val, right_val) => {
9094
if *left_val == *right_val {
9195
let kind = $crate::panicking::AssertKind::Ne;
96+
let left_name = stringify!($left);
97+
let right_name = stringify!($right);
9298
// The reborrows below are intentional. Without them, the stack slot for the
9399
// borrow is initialized even before the values are compared, leading to a
94100
// noticeable slow down.
95-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
101+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::None);
96102
}
97103
}
98104
}
@@ -102,10 +108,12 @@ macro_rules! assert_ne {
102108
(left_val, right_val) => {
103109
if *left_val == *right_val {
104110
let kind = $crate::panicking::AssertKind::Ne;
111+
let left_name = stringify!($left);
112+
let right_name = stringify!($right);
105113
// The reborrows below are intentional. Without them, the stack slot for the
106114
// borrow is initialized even before the values are compared, leading to a
107115
// noticeable slow down.
108-
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
116+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::Some($crate::format_args!($($arg)+)));
109117
}
110118
}
111119
}

library/core/src/panicking.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,17 @@ pub enum AssertKind {
217217
#[doc(hidden)]
218218
pub fn assert_failed<T, U>(
219219
kind: AssertKind,
220-
left: &T,
221-
right: &U,
220+
left_val: &T,
221+
right_val: &U,
222+
left_name: &'static str,
223+
right_name: &'static str,
222224
args: Option<fmt::Arguments<'_>>,
223225
) -> !
224226
where
225227
T: fmt::Debug + ?Sized,
226228
U: fmt::Debug + ?Sized,
227229
{
228-
assert_failed_inner(kind, &left, &right, args)
230+
assert_failed_inner(kind, &left_val, &right_val, &left_name, &right_name, args)
229231
}
230232

231233
/// Internal function for `assert_match!`
@@ -245,7 +247,14 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
245247
f.write_str(self.0)
246248
}
247249
}
248-
assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
250+
assert_failed_inner(
251+
AssertKind::Match,
252+
&left,
253+
&Pattern(right),
254+
stringify!(&left),
255+
stringify!(&right),
256+
args,
257+
);
249258
}
250259

251260
/// Non-generic version of the above functions, to avoid code bloat.
@@ -254,8 +263,10 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
254263
#[track_caller]
255264
fn assert_failed_inner(
256265
kind: AssertKind,
257-
upper_bounds: &dyn fmt::Debug,
258-
target: &dyn fmt::Debug,
266+
left_val: &dyn fmt::Debug,
267+
right_val: &dyn fmt::Debug,
268+
left_name: &'static str,
269+
right_name: &'static str,
259270
args: Option<fmt::Arguments<'_>>,
260271
) -> ! {
261272
let op = match kind {
@@ -266,17 +277,16 @@ fn assert_failed_inner(
266277

267278
match args {
268279
Some(args) => panic!(
269-
r#"assertion failed: `(upper_bounds {} target)`
270-
Error: `{:?}`,
271-
upper_bounds: `{:?}`,
272-
target: `{:?}`"#,
273-
op, args, upper_bounds, target,
280+
r#"assertion failed: `({left_name} {} {right_name})`
281+
{left_name}: `{:?}`,
282+
{right_name}: `{:?}`: {}"#,
283+
op, left_val, right_val, args
274284
),
275285
None => panic!(
276-
r#"assertion failed: `(upper_bounds {} target)`
277-
upper_bounds: `{:?}`,
278-
target: `{:?}`"#,
279-
op, upper_bounds, target,
286+
r#"assertion failed: `({left_name} {} {right_name})`
287+
{left_name}: `{:?}`,
288+
{right_name}: `{:?}`"#,
289+
op, left_val, right_val,
280290
),
281291
}
282292
}

0 commit comments

Comments
 (0)