Skip to content

improve custom message format in assert_eq macro #94016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
3 changes: 2 additions & 1 deletion library/core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ fn assert_failed_inner(
Some(args) => panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}`: {}"#,
right: `{:?}`,
context: `{:?}`"#,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the intent here was to align left: and right: with each other along the : - probably we want to do the same with context: if we do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated as suggested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I continue to submit more commits according to yaahc's suggestions, or close this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and update the format as suggested.

Copy link
Contributor Author

@MakitaToki MakitaToki Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change panicking.rs in core package and that one in std remains the same as the latter makes more failed tests.
The current output format is like this:

thread 'main' panicked at 'assertion failed: `(upper_bounds == target)`
   Error: `1 + 1 definitely should be 3`,
   upper_bounds: `2`,
   target: `3`', /home/kougami/temp/rust/src/test/ui/macros/assert-eq-macro-msg.rs:8:5

Copy link
Contributor Author

@MakitaToki MakitaToki Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my mistake. I definitely misunderstood your original comment. Do you want to stringify parameter names? @yaahc
Is it like this?

        Some(args) => panic!(
            r#"assertion failed: `(left {} right)`
  left: {},
 right: {}: {}"#,
            stringify!(op), stringify!(left), stringify!((right), stringify!(args)
        ),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not exactly, stringify! stringifies exactly whatever you pass into it, so calling stringify!(op) will just give you "op". Instead you'd need to call stringify! from inside of the assert_eq! macro_rules definition where you still have access to the original tokens, then you'd have to add arguments to assert_failed and assert_failed_inner to pass the strings all the way to the inner function that formats them, so it would look something like this:

// in `library/core/src/macros/mod.rs`
macro_rules! assert_eq {
    ($left:expr, $right:expr $(,)?) => ({
        match (&$left, &$right) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = $crate::panicking::AssertKind::Eq;
                    let left_name = stringify!($left);
                    let right_name = stringify!($right);
                    // 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.
                    $crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::None);
                }
            }
        }
    });
   // the rest of the assert_eq definition ...
}

// in `library/core/src/panicking` assert_failed_inner
fn assert_failed_inner(
    kind: AssertKind,
    left: &dyn fmt::Debug,
    right: &dyn fmt::Debug,
    left_name: &'static str,
    right_name: &'static str,
    args: Option<fmt::Arguments<'_>>,
) -> ! {
    let op = match kind {
        AssertKind::Eq => "==",
        AssertKind::Ne => "!=",
        AssertKind::Match => "matches",
    };

    match args {
        Some(args) => panic!(
            r#"assertion failed: `({left_name} {} {right_name})`
  {left_name}: `{:?}`,
 {right_name}: `{:?}`: {}"#,
            op, left, right, args
        ),
       // ....
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried my best to find some code snippets that needs to be updated. Is there anything else I'm missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this looks perfect! We just need to get the exact output sorted and update the test UI files so everything passes. Right now looking at the failed test in CI the output looks like this:

 thread 'main' panicked at 'assertion failed: `(1 + 1 == 5)`
  1 + 1: `2`,
 5: `5`', /checkout/src/test/ui/test-attrs/test-panic-abort.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

So we probably want to clean this up a bit, and we may want to rethink how many times we repeat the stringified version of the expression. Here's my suggestion for what we should aim for:

thread 'main' panicked at 'assertion failed: `(1 + 1 == 5)`
  left: `2`,
 right: `5`'
    at: /checkout/src/test/ui/test-attrs/test-panic-abort.rs:34:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated my commit

op, left, right, args
),
None => panic!(
Expand Down