Skip to content

Commit 95f6809

Browse files
svix-jplattehds
authored andcommitted
macros: allow field path segments to be keywords (#2925)
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¹. 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 59a48c7 commit 95f6809

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 (subscriber, handle) = subscriber::mock()
151164
.new_span(span)

tracing/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3066,8 +3066,8 @@ macro_rules! level_to_log {
30663066
#[doc(hidden)]
30673067
#[macro_export]
30683068
macro_rules! __tracing_stringify {
3069-
($s:expr) => {
3070-
stringify!($s)
3069+
($($t:tt)*) => {
3070+
stringify!($($t)*)
30713071
};
30723072
}
30733073

tracing/tests/event.rs

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

607607
handle.assert_finished();
608608
}
609+
610+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
611+
#[test]
612+
fn keyword_ident_in_field_name() {
613+
let (collector, handle) = collector::mock()
614+
.event(expect::event().with_fields(expect::field("crate").with_value(&"tracing")))
615+
.only()
616+
.run_with_handle();
617+
618+
with_default(collector, || error!(crate = "tracing", "message"));
619+
handle.assert_finished();
620+
}

tracing/tests/span.rs

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

88
use tracing::{
9+
error_span,
910
field::{debug, display},
1011
subscriber::with_default,
1112
Level, Span,
@@ -898,3 +899,20 @@ fn constant_field_name() {
898899

899900
handle.assert_finished();
900901
}
902+
903+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
904+
#[test]
905+
fn keyword_ident_in_field_name_span_macro() {
906+
#[derive(Debug)]
907+
struct Foo;
908+
909+
let (subscriber, handle) = subscriber::mock()
910+
.new_span(expect::span().with_fields(expect::field("self").with_value(&debug(Foo)).only()))
911+
.only()
912+
.run_with_handle();
913+
914+
with_default(subscriber, || {
915+
error_span!("span", self = ?Foo);
916+
});
917+
handle.assert_finished();
918+
}

0 commit comments

Comments
 (0)