Skip to content
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

assert_eq! message format (take 2) #111030

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ macro_rules! assert_eq {
(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, $crate::option::Option::None);
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::None);
}
}
}
Expand All @@ -52,10 +54,12 @@ macro_rules! assert_eq {
(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, $crate::option::Option::Some($crate::format_args!($($arg)+)));
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::Some($crate::format_args!($($arg)+)));
}
}
}
Expand Down Expand Up @@ -89,10 +93,12 @@ macro_rules! assert_ne {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = $crate::panicking::AssertKind::Ne;
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, $crate::option::Option::None);
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::None);
}
}
}
Expand All @@ -102,10 +108,12 @@ macro_rules! assert_ne {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = $crate::panicking::AssertKind::Ne;
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, $crate::option::Option::Some($crate::format_args!($($arg)+)));
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, left_name, right_name, $crate::option::Option::Some($crate::format_args!($($arg)+)));
}
}
}
Expand Down
40 changes: 26 additions & 14 deletions library/core/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,17 @@ pub enum AssertKind {
#[doc(hidden)]
pub fn assert_failed<T, U>(
kind: AssertKind,
left: &T,
right: &U,
left_val: &T,
right_val: &U,
left_name: &'static str,
right_name: &'static str,
args: Option<fmt::Arguments<'_>>,
) -> !
where
T: fmt::Debug + ?Sized,
U: fmt::Debug + ?Sized,
{
assert_failed_inner(kind, &left, &right, args)
assert_failed_inner(kind, &left_val, &right_val, &left_name, &right_name, args)
}

/// Internal function for `assert_match!`
Expand All @@ -245,7 +247,14 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
f.write_str(self.0)
}
}
assert_failed_inner(AssertKind::Match, &left, &Pattern(right), args);
assert_failed_inner(
AssertKind::Match,
&left,
&Pattern(right),
stringify!(&left),
stringify!(&right),
args,
);
}

/// Non-generic version of the above functions, to avoid code bloat.
Expand All @@ -254,8 +263,10 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>(
#[track_caller]
fn assert_failed_inner(
kind: AssertKind,
left: &dyn fmt::Debug,
right: &dyn fmt::Debug,
left_val: &dyn fmt::Debug,
right_val: &dyn fmt::Debug,
left_name: &'static str,
right_name: &'static str,
args: Option<fmt::Arguments<'_>>,
) -> ! {
let op = match kind {
Expand All @@ -266,16 +277,17 @@ fn assert_failed_inner(

match args {
Some(args) => panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}`: {}"#,
op, left, right, args
r#"assertion failed: `({left_name} {op} {right_name})`
error: {args},
left: `{left_val:?}`,
right: `{right_val:?}`
at: "#
),
None => panic!(
r#"assertion failed: `(left {} right)`
left: `{:?}`,
right: `{:?}`"#,
op, left, right,
r#"assertion failed: `({left_name} {op} {right_name})`
left: `{left_val:?}`,
right: `{right_val:?}`
at: "#
),
}
}