-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
improve custom message format in assert_eq macro #94016
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
426c85b
optimize panic message format in assert_eq macro
MakitaToki fa00953
align colons in assert_failed_inner
MakitaToki e575215
change message format in 'assert_failed_inner' function
MakitaToki 108f87e
delete trailing whitespaces
1ef6ba5
stringify original tokens
MakitaToki a1aed4a
resolve merged conflict
MakitaToki 099818b
accept local changes
MakitaToki 024f125
stringify original tokens in related macros
MakitaToki 657a52b
Merge branch 'issue-94005-fix' of github.com:Mizobrook-kan/rust into …
MakitaToki ded501c
clean up output format
MakitaToki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -171,15 +171,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!` | ||||||||||||||||
|
@@ -198,15 +200,24 @@ pub fn assert_matches_failed<T: fmt::Debug + ?Sized>( | |||||||||||||||
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<fmt::Arguments<'_>>, | ||||||||||||||||
) -> ! { | ||||||||||||||||
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 | ||||||||||||||||
), | ||||||||||||||||
Comment on lines
+233
to
236
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.
Suggested change
|
||||||||||||||||
None => panic!( | ||||||||||||||||
r#"assertion failed: `(left {} right)` | ||||||||||||||||
r#"assertion failed: `({left_name} {} {right_name})` | ||||||||||||||||
left: `{:?}`, | ||||||||||||||||
right: `{:?}`"#, | ||||||||||||||||
Comment on lines
+238
to
240
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.
Suggested change
|
||||||||||||||||
op, left, right, | ||||||||||||||||
op, left_val, right_val, | ||||||||||||||||
), | ||||||||||||||||
} | ||||||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
at:
here shouldn't be followed by a formatting specifier that we print, this is meant to be where the location goes, which is printed by the caller ofassert_failed_inner
, so we should just finish the output here. Theargs
that you're printing here is the error message and should come beforeleft
.