Skip to content

Commit

Permalink
macros: Allow field path segments to be keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte committed Apr 23, 2024
1 parent 908cc43 commit f87f41f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
39 changes: 38 additions & 1 deletion tracing-attributes/tests/fields.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tracing::collect::with_default;
use tracing::{collect::with_default, error, error_span, field};
use tracing_attributes::instrument;
use tracing_mock::{collector, expect, span::NewSpan};

Expand Down Expand Up @@ -34,6 +34,9 @@ fn fn_string(s: String) {
let _ = s;
}

#[instrument(fields(keywords.impl.type.fn = _arg), skip(_arg))]
fn fn_keyword_ident_in_field(_arg: &str) {}

#[derive(Debug)]
struct HasField {
my_field: &'static str,
Expand Down Expand Up @@ -146,6 +149,40 @@ fn string_field() {
});
}

#[test]
fn keyword_ident_in_field_name() {
let span = expect::span().with_fields(
expect::field("keywords.impl.type.fn")
.with_value(&"test")
.only(),
);
run_test(span, || fn_keyword_ident_in_field("test"));
}

#[test]
fn keyword_ident_in_field_name_event_macro() {
let (collector, handle) = collector::mock()
.event(expect::event().with_fields(expect::field("crate").with_value(&"tracing")))
.only()
.run_with_handle();

with_default(collector, || error!(crate = "tracing", "message"));
handle.assert_finished();
}

#[test]
fn keyword_ident_in_field_name_span_macro() {
#[derive(Debug)]
struct Foo;

let span =
expect::span().with_fields(expect::field("self").with_value(&field::debug(Foo)).only());
run_test(span, || {
let guard = error_span!("span", self = ?Foo).entered();
drop(guard);
});
}

fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
let (collector, handle) = collector::mock()
.new_span(span)
Expand Down
4 changes: 2 additions & 2 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3069,8 +3069,8 @@ macro_rules! level_to_log {
#[doc(hidden)]
#[macro_export]
macro_rules! __tracing_stringify {
($s:expr) => {
stringify!($s)
($($t:tt)*) => {
stringify!($($t)*)
};
}

Expand Down

0 comments on commit f87f41f

Please sign in to comment.