Skip to content

Commit b531630

Browse files
committed
Auto merge of #111071 - nyurik:simpler-issue-94005, r=m-ou-se
Cleaner assert_eq! & assert_ne! panic messages This PR finishes refactoring of the assert messages per #94005. The panic message format change #112849 used to be part of this PR, but has been factored out and just merged. It might be better to keep both changes in the same release once FCP vote completes. 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); ``` #### Old messages ```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! ``` #### New messages ```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 ``` History of fixing #94005 * #94016 was a lengthy PR that was abandoned * #111030 was similar, but it stringified left and right arguments, and thus caused compile time performance issues, thus closed * #112849 factored out the two-line formatting of all panic messages Fixes #94005 r? `@m-ou-se`
2 parents 0bdb00d + 950e3d9 commit b531630

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)