From 426c85b5d897482d524f92b88c47f46b13ccb905 Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Tue, 15 Feb 2022 18:45:10 +0800 Subject: [PATCH 1/8] optimize panic message format in assert_eq macro --- library/core/src/panicking.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 0798076411ac4..cc7599a564939 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -219,7 +219,8 @@ fn assert_failed_inner( Some(args) => panic!( r#"assertion failed: `(left {} right)` left: `{:?}`, - right: `{:?}`: {}"#, + right: `{:?}`, +context: `{:?}`"#, op, left, right, args ), None => panic!( From fa00953d82ec7079041a8dadca1ae011c09feb67 Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Thu, 17 Feb 2022 00:36:12 +0800 Subject: [PATCH 2/8] align colons in assert_failed_inner --- library/core/src/panicking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index cc7599a564939..410e1b90e3501 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -218,8 +218,8 @@ fn assert_failed_inner( match args { Some(args) => panic!( r#"assertion failed: `(left {} right)` - left: `{:?}`, - right: `{:?}`, + left: `{:?}`, + right: `{:?}`, context: `{:?}`"#, op, left, right, args ), From e57521534c9947dd5fa58b914f8dde4c09820ba5 Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Sat, 19 Feb 2022 10:17:11 +0800 Subject: [PATCH 3/8] change message format in 'assert_failed_inner' function --- library/core/src/panicking.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 410e1b90e3501..3fa969870ee08 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -205,8 +205,8 @@ pub fn assert_matches_failed( #[track_caller] fn assert_failed_inner( kind: AssertKind, - left: &dyn fmt::Debug, - right: &dyn fmt::Debug, + upper_bounds: &dyn fmt::Debug, + target: &dyn fmt::Debug, args: Option>, ) -> ! { let op = match kind { @@ -217,17 +217,17 @@ fn assert_failed_inner( match args { Some(args) => panic!( - r#"assertion failed: `(left {} right)` - left: `{:?}`, - right: `{:?}`, -context: `{:?}`"#, - op, left, right, args + r#"assertion failed: `(upper_bounds {} target)` + Error: `{:?}`, + upper_bounds: `{:?}`, + target: `{:?}`"#, + op, args, upper_bounds, target, ), None => panic!( - r#"assertion failed: `(left {} right)` - left: `{:?}`, - right: `{:?}`"#, - op, left, right, + r#"assertion failed: `(upper_bounds {} target)` + upper_bounds: `{:?}`, + target: `{:?}`"#, + op, upper_bounds, target, ), } } From 108f87e4604224132eedd8e3a6ec5d90f313764a Mon Sep 17 00:00:00 2001 From: kougami <15102471859@163.com> Date: Sat, 19 Feb 2022 14:09:43 +0800 Subject: [PATCH 4/8] delete trailing whitespaces --- library/core/src/panicking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 3fa969870ee08..03a1aa7b1c3eb 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -218,7 +218,7 @@ fn assert_failed_inner( match args { Some(args) => panic!( r#"assertion failed: `(upper_bounds {} target)` - Error: `{:?}`, + Error: `{:?}`, upper_bounds: `{:?}`, target: `{:?}`"#, op, args, upper_bounds, target, From 1ef6ba589e63d7cbb936d4b24b3790d0e283dac0 Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Mon, 28 Feb 2022 10:44:27 +0800 Subject: [PATCH 5/8] stringify original tokens --- library/core/src/macros/mod.rs | 16 ++++++++++---- library/core/src/panicking.rs | 40 +++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 19 deletions(-) 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 3fa969870ee08..b923dac616815 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, - upper_bounds: &dyn fmt::Debug, - target: &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,17 +228,16 @@ fn assert_failed_inner( match args { Some(args) => panic!( - r#"assertion failed: `(upper_bounds {} target)` - Error: `{:?}`, - upper_bounds: `{:?}`, - target: `{:?}`"#, - op, args, upper_bounds, target, + r#"assertion failed: `({left_name} {} {right_name})` + {left_name}: `{:?}`, + {right_name}: `{:?}`: {}"#, + op, left_val, right_val, args ), None => panic!( - r#"assertion failed: `(upper_bounds {} target)` - upper_bounds: `{:?}`, - target: `{:?}`"#, - op, upper_bounds, target, + r#"assertion failed: `({left_name} {} {right_name})` + {left_name}: `{:?}`, + {right_name}: `{:?}`"#, + op, left_val, right_val, ), } } From 099818bde828542d3016f4cf0dae726cec9eb8be Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Mon, 28 Feb 2022 11:01:11 +0800 Subject: [PATCH 6/8] accept local changes --- library/core/src/panicking.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 653d52d0ed77f..b923dac616815 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -228,18 +228,10 @@ fn assert_failed_inner( match args { Some(args) => panic!( -<<<<<<< HEAD r#"assertion failed: `({left_name} {} {right_name})` {left_name}: `{:?}`, {right_name}: `{:?}`: {}"#, op, left_val, right_val, args -======= - r#"assertion failed: `(upper_bounds {} target)` - Error: `{:?}`, - upper_bounds: `{:?}`, - target: `{:?}`"#, - op, args, upper_bounds, target, ->>>>>>> 108f87e4604224132eedd8e3a6ec5d90f313764a ), None => panic!( r#"assertion failed: `({left_name} {} {right_name})` From 024f125fbb40c6aa4ef76909f1745efde3e300a6 Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Mon, 28 Feb 2022 10:44:27 +0800 Subject: [PATCH 7/8] stringify original tokens in related macros resolve conflicts and stringify original tokens accept local changes --- library/core/src/macros/mod.rs | 16 ++++++++++---- library/core/src/panicking.rs | 40 +++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 19 deletions(-) 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 03a1aa7b1c3eb..b923dac616815 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, - upper_bounds: &dyn fmt::Debug, - target: &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,17 +228,16 @@ fn assert_failed_inner( match args { Some(args) => panic!( - r#"assertion failed: `(upper_bounds {} target)` - Error: `{:?}`, - upper_bounds: `{:?}`, - target: `{:?}`"#, - op, args, upper_bounds, target, + r#"assertion failed: `({left_name} {} {right_name})` + {left_name}: `{:?}`, + {right_name}: `{:?}`: {}"#, + op, left_val, right_val, args ), None => panic!( - r#"assertion failed: `(upper_bounds {} target)` - upper_bounds: `{:?}`, - target: `{:?}`"#, - op, upper_bounds, target, + r#"assertion failed: `({left_name} {} {right_name})` + {left_name}: `{:?}`, + {right_name}: `{:?}`"#, + op, left_val, right_val, ), } } From ded501c2ce86746e30d19503d982d3496201ba9e Mon Sep 17 00:00:00 2001 From: Mizobrook-kan Date: Sun, 6 Mar 2022 11:02:07 +0800 Subject: [PATCH 8/8] clean up output format --- library/core/src/panicking.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index b923dac616815..ec5a9cff2ff40 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -229,14 +229,15 @@ fn assert_failed_inner( match args { Some(args) => panic!( r#"assertion failed: `({left_name} {} {right_name})` - {left_name}: `{:?}`, - {right_name}: `{:?}`: {}"#, + left: `{:?}`, + right: `{:?}`: + at: {}"#, op, left_val, right_val, args ), None => panic!( r#"assertion failed: `({left_name} {} {right_name})` - {left_name}: `{:?}`, - {right_name}: `{:?}`"#, + left: `{:?}`, + right: `{:?}`"#, op, left_val, right_val, ), }