macros: Remove 'r#' prefix from raw identifiers in field names #3130
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
Currently a call like
info!(..., r#type = "...")
ends up emitting a field named "r#type", which is never desirable. Previous work in #2925 made the desired behavior expressible asinfo!(..., type = "...")
, and even before that PR one could write it asinfo!(..., "type" = "...")
.However, when dealing with macro-generated use of tracing, we do still sometimes end up with raw identifiers. For example, if user-provided identifiers are used for both declaring (or instantiating, or accessing) a struct, and also for tracing. Something like this:
If we ask the user of
example!
to passtype
(unraw), there is no way for the macro to accessGLOBAL.r#type
from that. But if we ask the user ofexample!
to passr#type
(raw), there is no straightforward way for the macro to trace the field name as "type". The best option would have been something like:but this gets even more ridiculous when dealing with field names that are not just a single identifier. (
prefix.r#keyword.suffix
).Solution
r#keyword = $value
is made equivalent tokeyword = $value
. The field will be emitted with the name "keyword", not "r#keyword" as before.