-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Specialize format! for simple "{}" case #76531
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
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 |
---|---|---|
|
@@ -102,6 +102,10 @@ macro_rules! vec { | |
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! format { | ||
// A faster path for simple format! calls. | ||
("{}", $arg:ident) => {{ | ||
$crate::fmt::Display::to_string(&$arg) | ||
}}; | ||
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. We will want a codegen test I think for this, at least -- it looks like this is just not triggering to me, because I would expect 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.
Ah, one of these? Well, that just made this PR more Exciting. 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. Note that before working on that I would really fix this macro to make sure this arm is getting expanded, because right now that seems pretty unlikely to me. See also the comment below about needing :ident on the arm, most likely, which'll further simplify this branch. |
||
($($arg:tt)*) => {{ | ||
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*)); | ||
res | ||
|
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.
Any reason to make this so restrictive? I think
$arg:expr $(,)?
would catch a lot more and still allow callingto_string
.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.
#76531 (comment)
What's worse than that: even the expr version will not be correct.
Right now,
format!
parses named arguments, likea = 1
. If that was parsed as an expression, then it would evaluate to()
, and this would not pass the assertion.I think it actually needs to be
$arg:ident
, and the optimization probably wouldn't be triggered very often as a result. That's annoying.