Skip to content

Commit 4105abb

Browse files
committed
Improve assert_eq message
1 parent d462551 commit 4105abb

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

src/libcore/macros/mod.rs

+48-14
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,20 @@ macro_rules! assert_eq {
4444
match (&$left, &$right) {
4545
(left_val, right_val) => {
4646
if !(*left_val == *right_val) {
47+
let left = stringify!($left);
48+
let right = stringify!($right);
49+
let width = if left.len() > right.len() {
50+
left.len()
51+
} else {
52+
right.len()
53+
};
54+
4755
// The reborrows below are intentional. Without them, the stack slot for the
4856
// borrow is initialized even before the values are compared, leading to a
4957
// noticeable slow down.
50-
panic!(r#"assertion failed: `(left == right)`
51-
left: `{:?}`,
52-
right: `{:?}`"#, &*left_val, &*right_val)
58+
panic!(r#"assertion failed: `({left}) == ({right})`
59+
{left:>width$}: `{:?}`,
60+
{right:>width$}: `{:?}`"#, &*left_val, &*right_val, left=left, right=right, width=width)
5361
}
5462
}
5563
}
@@ -61,13 +69,22 @@ macro_rules! assert_eq {
6169
match (&($left), &($right)) {
6270
(left_val, right_val) => {
6371
if !(*left_val == *right_val) {
72+
let left = stringify!($left);
73+
let right = stringify!($right);
74+
let width = if left.len() > right.len() {
75+
left.len()
76+
} else {
77+
right.len()
78+
};
79+
6480
// The reborrows below are intentional. Without them, the stack slot for the
6581
// borrow is initialized even before the values are compared, leading to a
6682
// noticeable slow down.
67-
panic!(r#"assertion failed: `(left == right)`
68-
left: `{:?}`,
69-
right: `{:?}`: {}"#, &*left_val, &*right_val,
70-
$crate::format_args!($($arg)+))
83+
panic!(r#"assertion failed: `({left}) == ({right})`
84+
{left:>width$}: `{:?}`,
85+
{right:>width$}: `{:?}`: {}"#, &*left_val, &*right_val,
86+
$crate::format_args!($($arg)+),
87+
left=left, right=right, width=width)
7188
}
7289
}
7390
}
@@ -101,12 +118,20 @@ macro_rules! assert_ne {
101118
match (&$left, &$right) {
102119
(left_val, right_val) => {
103120
if *left_val == *right_val {
121+
let left = stringify!($left);
122+
let right = stringify!($right);
123+
let width = if left.len() > right.len() {
124+
left.len()
125+
} else {
126+
right.len()
127+
};
128+
104129
// The reborrows below are intentional. Without them, the stack slot for the
105130
// borrow is initialized even before the values are compared, leading to a
106131
// noticeable slow down.
107-
panic!(r#"assertion failed: `(left != right)`
108-
left: `{:?}`,
109-
right: `{:?}`"#, &*left_val, &*right_val)
132+
panic!(r#"assertion failed: `({left}) != ({right})`
133+
{left:>width$}: `{:?}`,
134+
{right:>width$}: `{:?}`"#, &*left_val, &*right_val, left=left, right=right, width=width)
110135
}
111136
}
112137
}
@@ -118,13 +143,22 @@ macro_rules! assert_ne {
118143
match (&($left), &($right)) {
119144
(left_val, right_val) => {
120145
if *left_val == *right_val {
146+
let left = stringify!($left);
147+
let right = stringify!($right);
148+
let width = if left.len() > right.len() {
149+
left.len()
150+
} else {
151+
right.len()
152+
};
153+
121154
// The reborrows below are intentional. Without them, the stack slot for the
122155
// borrow is initialized even before the values are compared, leading to a
123156
// noticeable slow down.
124-
panic!(r#"assertion failed: `(left != right)`
125-
left: `{:?}`,
126-
right: `{:?}`: {}"#, &*left_val, &*right_val,
127-
$crate::format_args!($($arg)+))
157+
panic!(r#"assertion failed: `({left}) != ({right})`
158+
{left:>width$}: `{:?}`,
159+
{right:>width$}: `{:?}`: {}"#, &*left_val, &*right_val,
160+
$crate::format_args!($($arg)+),
161+
left=left, right=right, width=width)
128162
}
129163
}
130164
}

src/test/ui/macros/assert-eq-macro-panic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-fail
2-
// error-pattern:assertion failed: `(left == right)`
3-
// error-pattern: left: `14`
4-
// error-pattern:right: `15`
2+
// error-pattern:assertion failed: `(14) == (15)`
3+
// error-pattern:14: `14`
4+
// error-pattern:15: `15`
55
// ignore-emscripten no processes
66

77
fn main() {

src/test/ui/macros/assert-ne-macro-panic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-fail
2-
// error-pattern:assertion failed: `(left != right)`
3-
// error-pattern: left: `14`
4-
// error-pattern:right: `14`
2+
// error-pattern:assertion failed: `(14) != (14)`
3+
// error-pattern:14: `14`
4+
// error-pattern:14: `14`
55
// ignore-emscripten no processes
66

77
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
thread 'main' panicked at 'assertion failed: `(left == right)`
2-
left: `2`,
3-
right: `4`', $DIR/test-panic-abort-nocapture.rs:32:5
1+
thread 'main' panicked at 'assertion failed: `(1 + 1) == (4)`
2+
1 + 1: `2`,
3+
4: `4`', $DIR/test-panic-abort-nocapture.rs:32:5
44
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5-
thread 'main' panicked at 'assertion failed: `(left == right)`
6-
left: `2`,
7-
right: `4`', $DIR/test-panic-abort-nocapture.rs:26:5
5+
thread 'main' panicked at 'assertion failed: `(1 + 1) == (4)`
6+
1 + 1: `2`,
7+
4: `4`', $DIR/test-panic-abort-nocapture.rs:26:5
88
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
99
testing321

src/test/ui/test-panic-abort.run.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ hello, world
1616
testing123
1717
---- it_fails stderr ----
1818
testing321
19-
thread 'main' panicked at 'assertion failed: `(left == right)`
20-
left: `2`,
21-
right: `5`', $DIR/test-panic-abort.rs:33:5
19+
thread 'main' panicked at 'assertion failed: `(1 + 1) == (5)`
20+
1 + 1: `2`,
21+
5: `5`', $DIR/test-panic-abort.rs:33:5
2222
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2323

2424

0 commit comments

Comments
 (0)