diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 628b679236e1d..ce144c67157ec 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -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); } } } @@ -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)+))); } } } @@ -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); } } } @@ -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)+))); } } } diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 0798076411ac4..ec5a9cff2ff40 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -171,15 +171,17 @@ pub enum AssertKind { #[doc(hidden)] pub fn assert_failed( kind: AssertKind, - left: &T, - right: &U, + left_val: &T, + right_val: &U, + left_name: &'static str, + right_name: &'static str, args: Option>, ) -> ! 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!` @@ -198,15 +200,24 @@ pub fn assert_matches_failed( fmt::Display::fmt(self.0, f) } } - 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. #[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>, ) -> ! { let op = match kind { @@ -217,16 +228,17 @@ fn assert_failed_inner( match args { Some(args) => panic!( - r#"assertion failed: `(left {} right)` + r#"assertion failed: `({left_name} {} {right_name})` left: `{:?}`, - right: `{:?}`: {}"#, - op, left, right, args + right: `{:?}`: + at: {}"#, + op, left_val, right_val, args ), None => panic!( - r#"assertion failed: `(left {} right)` + r#"assertion failed: `({left_name} {} {right_name})` left: `{:?}`, right: `{:?}`"#, - op, left, right, + op, left_val, right_val, ), } }