Skip to content

Commit 44861ca

Browse files
authored
macros: allow field path segments to be keywords (#2925)
## Motivation Currently, a keyword like `type` fails compilation as (a path segment of) a field name, for no clear reason. Trying to use `r#type` instead leads to the `r#` being part of the field name, which is unhelpful¹. ## Solution Don't require the field path to match a `macro_rules!` `expr`, use repeated `tt` instead. I can't tell why this was ever required: The internal stringify macro was introduced in 55091c9#diff-315c02cd05738da173861537577d159833f70f79cfda8cd7cf1a0d7a28ace31b with an `expr` matcher without any explanation, and no tests are failing from making it match upstream's `stringify!` input format. Special thanks to whoever implemented the unstable `macro-backtrace` feature in rustc, otherwise this would have been nigh impossible to track down! ¹ this can likely be fixed too by some sort of "unraw" macro that turns `r#foo` into `foo`, but that's a separate change not made in this PR
1 parent ba387dd commit 44861ca

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

tracing-attributes/tests/fields.rs

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ fn fn_string(s: String) {
3434
let _ = s;
3535
}
3636

37+
#[instrument(fields(keywords.impl.type.fn = _arg), skip(_arg))]
38+
fn fn_keyword_ident_in_field(_arg: &str) {}
39+
3740
#[derive(Debug)]
3841
struct HasField {
3942
my_field: &'static str,
@@ -146,6 +149,16 @@ fn string_field() {
146149
});
147150
}
148151

152+
#[test]
153+
fn keyword_ident_in_field_name() {
154+
let span = expect::span().with_fields(
155+
expect::field("keywords.impl.type.fn")
156+
.with_value(&"test")
157+
.only(),
158+
);
159+
run_test(span, || fn_keyword_ident_in_field("test"));
160+
}
161+
149162
fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
150163
let (collector, handle) = collector::mock()
151164
.new_span(span)

tracing/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3069,8 +3069,8 @@ macro_rules! level_to_log {
30693069
#[doc(hidden)]
30703070
#[macro_export]
30713071
macro_rules! __tracing_stringify {
3072-
($s:expr) => {
3073-
stringify!($s)
3072+
($($t:tt)*) => {
3073+
stringify!($($t)*)
30743074
};
30753075
}
30763076

tracing/tests/event.rs

+12
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,15 @@ fn constant_field_name() {
497497

498498
handle.assert_finished();
499499
}
500+
501+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
502+
#[test]
503+
fn keyword_ident_in_field_name() {
504+
let (collector, handle) = collector::mock()
505+
.event(expect::event().with_fields(expect::field("crate").with_value(&"tracing")))
506+
.only()
507+
.run_with_handle();
508+
509+
with_default(collector, || error!(crate = "tracing", "message"));
510+
handle.assert_finished();
511+
}

tracing/tests/span.rs

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::thread;
77

88
use tracing::{
99
collect::with_default,
10+
error_span,
1011
field::{debug, display},
1112
Level, Span,
1213
};
@@ -866,3 +867,20 @@ fn constant_field_name() {
866867

867868
handle.assert_finished();
868869
}
870+
871+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
872+
#[test]
873+
fn keyword_ident_in_field_name_span_macro() {
874+
#[derive(Debug)]
875+
struct Foo;
876+
877+
let (collector, handle) = collector::mock()
878+
.new_span(expect::span().with_fields(expect::field("self").with_value(&debug(Foo)).only()))
879+
.only()
880+
.run_with_handle();
881+
882+
with_default(collector, || {
883+
error_span!("span", self = ?Foo);
884+
});
885+
handle.assert_finished();
886+
}

0 commit comments

Comments
 (0)