Skip to content

Commit

Permalink
Improve assert_eq message
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Jul 2, 2020
1 parent d462551 commit 4105abb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
62 changes: 48 additions & 14 deletions src/libcore/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ macro_rules! assert_eq {
match (&$left, &$right) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let left = stringify!($left);
let right = stringify!($right);
let width = if left.len() > right.len() {
left.len()
} else {
right.len()
};

// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
panic!(r#"assertion failed: `({left}) == ({right})`
{left:>width$}: `{:?}`,
{right:>width$}: `{:?}`"#, &*left_val, &*right_val, left=left, right=right, width=width)
}
}
}
Expand All @@ -61,13 +69,22 @@ macro_rules! assert_eq {
match (&($left), &($right)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let left = stringify!($left);
let right = stringify!($right);
let width = if left.len() > right.len() {
left.len()
} else {
right.len()
};

// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+))
panic!(r#"assertion failed: `({left}) == ({right})`
{left:>width$}: `{:?}`,
{right:>width$}: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+),
left=left, right=right, width=width)
}
}
}
Expand Down Expand Up @@ -101,12 +118,20 @@ macro_rules! assert_ne {
match (&$left, &$right) {
(left_val, right_val) => {
if *left_val == *right_val {
let left = stringify!($left);
let right = stringify!($right);
let width = if left.len() > right.len() {
left.len()
} else {
right.len()
};

// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
panic!(r#"assertion failed: `({left}) != ({right})`
{left:>width$}: `{:?}`,
{right:>width$}: `{:?}`"#, &*left_val, &*right_val, left=left, right=right, width=width)
}
}
}
Expand All @@ -118,13 +143,22 @@ macro_rules! assert_ne {
match (&($left), &($right)) {
(left_val, right_val) => {
if *left_val == *right_val {
let left = stringify!($left);
let right = stringify!($right);
let width = if left.len() > right.len() {
left.len()
} else {
right.len()
};

// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+))
panic!(r#"assertion failed: `({left}) != ({right})`
{left:>width$}: `{:?}`,
{right:>width$}: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+),
left=left, right=right, width=width)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/macros/assert-eq-macro-panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-fail
// error-pattern:assertion failed: `(left == right)`
// error-pattern: left: `14`
// error-pattern:right: `15`
// error-pattern:assertion failed: `(14) == (15)`
// error-pattern:14: `14`
// error-pattern:15: `15`
// ignore-emscripten no processes

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/macros/assert-ne-macro-panic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-fail
// error-pattern:assertion failed: `(left != right)`
// error-pattern: left: `14`
// error-pattern:right: `14`
// error-pattern:assertion failed: `(14) != (14)`
// error-pattern:14: `14`
// error-pattern:14: `14`
// ignore-emscripten no processes

fn main() {
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/test-panic-abort-nocapture.run.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `4`', $DIR/test-panic-abort-nocapture.rs:32:5
thread 'main' panicked at 'assertion failed: `(1 + 1) == (4)`
1 + 1: `2`,
4: `4`', $DIR/test-panic-abort-nocapture.rs:32:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `4`', $DIR/test-panic-abort-nocapture.rs:26:5
thread 'main' panicked at 'assertion failed: `(1 + 1) == (4)`
1 + 1: `2`,
4: `4`', $DIR/test-panic-abort-nocapture.rs:26:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
testing321
6 changes: 3 additions & 3 deletions src/test/ui/test-panic-abort.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ hello, world
testing123
---- it_fails stderr ----
testing321
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `5`', $DIR/test-panic-abort.rs:33:5
thread 'main' panicked at 'assertion failed: `(1 + 1) == (5)`
1 + 1: `2`,
5: `5`', $DIR/test-panic-abort.rs:33:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


Expand Down

0 comments on commit 4105abb

Please sign in to comment.