tracing: support bare display and debug in macros
#3426
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.
Motivation
The
valueset!macro importsdisplay,debugandValuefromtracing::field. This is unhygienic in that tracing macros that usevalueset!(all the event and span macros) cannot use any symbols nameddisplayordebugwhen defining fields inside the macro.A side affect of this is that these three symbols can be used
unqualified within the definition of fields within the tracing macros.
So the following is valid:
As per Hyrum's Law, this behavior depended on by plenty of users and
has been for years. At least 2 attempts have been made to clean this up,
but they've had to be reverted as it is effectively a breaking change
(see #820 and #3424 for details).
Solution
To partially resolve this situation, this change adds support for
displayanddebugpatterns to thevalueset!macro explicitly. Assuch, the use of
display(expr),display(ident)and the same fordebugare matched in the patterns the same way that the%and?sigils already are.
These patterns then get expanded the same way that the sigils do, into
the fully qualified path to the appropriate function.
As the use of bare
displayanddebugare now caught by the macro, wecan safely remove the import of those 2 symbols. This still leaves the
import of
$crate::field::Value, but I don't know if that's avoidable,and has also proved to be less problematic as it's a type and less
likely to clash to the use of variables. However, we still don't want to
break user code by removing it.
The bare
displayanddebugalso continue to work with the#[instrument]attribute macro:Fairly extensive tests have been added to cover all the cases that the
macros support (which are all the cases that could be thought up).
This means that we can support all previously valid macro invocations,
but we can also support the following, which didn't work before:
This change deliberately doesn't add support for wrapping an identifier
in
debugordisplaywith no key. This isn't supported now and thereis no need to support it when the sigils are available. That is to say
that the following didn't compile before and will continue not to
compile:
To make this clear, UI tests have been added for the compilation error.