-
Notifications
You must be signed in to change notification settings - Fork 821
Support constant expressions as instrument field names #3158
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
base: main
Are you sure you want to change the base?
Conversation
acd38e1
to
a2c17f3
Compare
tracing-attributes/tests/fields.rs
Outdated
}); | ||
} | ||
|
||
// TODO(#3158): To be consistent with event! and span! macros, the argument's value should be |
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.
Let's add a test for the panic here, to ensure that this check doesn't get accidentally removed at some point in the future. It may be necessary to move the test to a "ui" test to validate the compiler error message.
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.
I no longer think adding a panic or deduplication logic for constant expressions is feasible at compile time. Every solution I tested incurs a runtime cost, as constant expressions need to be evaluated first, and then every value needs to be compared against each other.
Given that the impact of not doing it is "just" duplicated fields reported, and is limited to people newly opting into braced constant expressions, I don't expect this to cause much problem.
I updated the docs to explain this.
@nico-incubiq , are you still planning to work on this? |
I'd like to pick this back up yes.
|
ecd0231
to
41724a9
Compare
41724a9
to
aab1ff3
Compare
8dca98e
to
e709479
Compare
e709479
to
4f01b67
Compare
This is ready for review again when someone has time 🙏🏼 |
Motivation
The
#[instrument]
proc macro only allows setting field names as identifiers, which is not on par with other macros such asspan!
orevent!
that allow constant expressions. Using constants for example can ensure consistency across a project for the field names used.This addresses #2969, which was already mentioned in this comment here on #2617 (note that "classic macros" mentioned there already work, there are tests attesting of it).
I also updated the doc block to mention field values can be any arbitrary expressions, which was implemented by #672 but never updated.
Solution
I updated the instrument proc macro to allow constant expressions (const, const fn, literal str, ...) as field names, using the same syntax as #2617, enclosed in curly braces.
Notes
s
would be used instead of a function arguments
in the span). As far as I'm aware this is not possible to implement for constant expressions, as the compiler doesn't allow to evaluate constants at build time (MIRI might be a solution but that seems complicated, and https://rustc-dev-guide.rust-lang.org/const-eval seems experimental). It's a minor inconsistency; in this scenario, the code would panic immediately at startup, and the workaround is as simple as skipping the function argument.