-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve assert_eq! and assert_ne! #79100
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
Changes from all commits
bbad2b2
52197d3
f138e26
546d062
a357d86
7333759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,3 +91,54 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { | |
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. | ||
unsafe { panic_impl(&pi) } | ||
} | ||
|
||
#[derive(Debug)] | ||
#[doc(hidden)] | ||
pub enum AssertKind { | ||
Eq, | ||
Ne, | ||
} | ||
|
||
/// Internal function for `assert_eq!` and `assert_ne!` macros | ||
#[cold] | ||
#[track_caller] | ||
#[doc(hidden)] | ||
pub fn assert_failed<T, U>( | ||
a1phyr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
kind: AssertKind, | ||
left: &T, | ||
right: &U, | ||
args: Option<fmt::Arguments<'_>>, | ||
) -> ! | ||
where | ||
T: fmt::Debug + ?Sized, | ||
U: fmt::Debug + ?Sized, | ||
{ | ||
#[track_caller] | ||
fn inner( | ||
kind: AssertKind, | ||
left: &dyn fmt::Debug, | ||
right: &dyn fmt::Debug, | ||
args: Option<fmt::Arguments<'_>>, | ||
) -> ! { | ||
let op = match kind { | ||
AssertKind::Eq => "==", | ||
AssertKind::Ne => "!=", | ||
}; | ||
|
||
match args { | ||
Some(args) => panic!( | ||
r#"assertion failed: `(left {} right)` | ||
left: `{:?}`, | ||
right: `{:?}: {}`"#, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was the change that moves The panic message now looks suboptimal to me: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0cbe6c5669d0b7458c6d9f67a3ea7518 Nightly
Stable
(In case it was unintentional, I have a patch.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hyd-dev oh, good catch! Can you send that patch as a PR? (Feel free to assign me with |
||
op, left, right, args | ||
), | ||
None => panic!( | ||
r#"assertion failed: `(left {} right)` | ||
left: `{:?}`, | ||
right: `{:?}`"#, | ||
op, left, right, | ||
), | ||
} | ||
} | ||
inner(kind, &left, &right, args) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.