Skip to content

Commit f87f41f

Browse files
committed
macros: Allow field path segments to be keywords
1 parent 908cc43 commit f87f41f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

tracing-attributes/tests/fields.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use tracing::collect::with_default;
1+
use tracing::{collect::with_default, error, error_span, field};
22
use tracing_attributes::instrument;
33
use tracing_mock::{collector, expect, span::NewSpan};
44

@@ -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,40 @@ 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+
162+
#[test]
163+
fn keyword_ident_in_field_name_event_macro() {
164+
let (collector, handle) = collector::mock()
165+
.event(expect::event().with_fields(expect::field("crate").with_value(&"tracing")))
166+
.only()
167+
.run_with_handle();
168+
169+
with_default(collector, || error!(crate = "tracing", "message"));
170+
handle.assert_finished();
171+
}
172+
173+
#[test]
174+
fn keyword_ident_in_field_name_span_macro() {
175+
#[derive(Debug)]
176+
struct Foo;
177+
178+
let span =
179+
expect::span().with_fields(expect::field("self").with_value(&field::debug(Foo)).only());
180+
run_test(span, || {
181+
let guard = error_span!("span", self = ?Foo).entered();
182+
drop(guard);
183+
});
184+
}
185+
149186
fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
150187
let (collector, handle) = collector::mock()
151188
.new_span(span)

tracing/src/macros.rs

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)