Skip to content

Commit 950e3d9

Browse files
committed
Cleaner assert_eq! & assert_ne! panic messages
Modify panic message for `assert_eq!`, `assert_ne!`, the currently unstable `assert_matches!`, as well as the corresponding `debug_assert_*` macros. ```rust assert_eq!(1 + 1, 3); assert_eq!(1 + 1, 3, "my custom message value={}!", 42); ``` ```plain thread 'main' panicked at $DIR/main.rs:6:5: assertion failed: `(left == right)` left: `2`, right: `3` ``` ```plain thread 'main' panicked at $DIR/main.rs:6:5: assertion failed: `(left == right)` left: `2`, right: `3`: my custom message value=42! ``` ```plain thread 'main' panicked at $DIR/main.rs:6:5: assertion `left == right` failed left: 2 right: 3 ``` ```plain thread 'main' panicked at $DIR/main.rs:6:5: assertion `left == right` failed: my custom message value=42! left: 2 right: 3 ``` This PR is a simpler subset of the #111030, but it does NOT stringify the original left and right source code assert expressions, thus should be faster to compile.
1 parent c57393e commit 950e3d9

8 files changed

+30
-32
lines changed

library/core/src/panicking.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,14 @@ fn assert_failed_inner(
267267

268268
match args {
269269
Some(args) => panic!(
270-
r#"assertion failed: `(left {} right)`
271-
left: `{:?}`,
272-
right: `{:?}`: {}"#,
273-
op, left, right, args
270+
r#"assertion `left {op} right` failed: {args}
271+
left: {left:?}
272+
right: {right:?}"#
274273
),
275274
None => panic!(
276-
r#"assertion failed: `(left {} right)`
277-
left: `{:?}`,
278-
right: `{:?}`"#,
279-
op, left, right,
275+
r#"assertion `left {op} right` failed
276+
left: {left:?}
277+
right: {right:?}"#
280278
),
281279
}
282280
}

tests/ui/macros/assert-eq-macro-msg.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: `2`
4-
// error-pattern:right: `3`: 1 + 1 definitely should be 3
2+
// error-pattern:assertion `left == right` failed: 1 + 1 definitely should be 3
3+
// error-pattern: left: 2
4+
// error-pattern: right: 3
55
// ignore-emscripten no processes
66

77
fn main() {

tests/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 `left == right` failed
3+
// error-pattern: left: 14
4+
// error-pattern: right: 15
55
// ignore-emscripten no processes
66

77
fn main() {

tests/ui/macros/assert-matches-macro-msg.rs

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

77
#![feature(assert_matches)]

tests/ui/macros/assert-ne-macro-msg.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: `2`
4-
// error-pattern:right: `2`: 1 + 1 definitely should not be 2
2+
// error-pattern:assertion `left != right` failed: 1 + 1 definitely should not be 2
3+
// error-pattern: left: 2
4+
// error-pattern: right: 2
55
// ignore-emscripten no processes
66

77
fn main() {

tests/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 `left != right` failed
3+
// error-pattern: left: 14
4+
// error-pattern: right: 14
55
// ignore-emscripten no processes
66

77
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:33:5:
2-
assertion failed: `(left == right)`
3-
left: `2`,
4-
right: `4`
2+
assertion `left == right` failed
3+
left: 2
4+
right: 4
55
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
66
thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:27:5:
7-
assertion failed: `(left == right)`
8-
left: `2`,
9-
right: `4`
7+
assertion `left == right` failed
8+
left: 2
9+
right: 4
1010
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1111
testing321

tests/ui/test-attrs/test-panic-abort.run.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ testing123
1818
---- it_fails stderr ----
1919
testing321
2020
thread 'main' panicked at $DIR/test-panic-abort.rs:38:5:
21-
assertion failed: `(left == right)`
22-
left: `2`,
23-
right: `5`
21+
assertion `left == right` failed
22+
left: 2
23+
right: 5
2424
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2525

2626

0 commit comments

Comments
 (0)