Skip to content
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

macros: Allow field path segments to be keywords #2925

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tracing-attributes/tests/fields.rs
Original file line number Diff line number Diff line change
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,16 @@ 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"));
}

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
12 changes: 12 additions & 0 deletions tracing/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,15 @@ fn constant_field_name() {

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn keyword_ident_in_field_name() {
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();
}
18 changes: 18 additions & 0 deletions tracing/tests/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::thread;

use tracing::{
collect::with_default,
error_span,
field::{debug, display},
Level, Span,
};
Expand Down Expand Up @@ -866,3 +867,20 @@ fn constant_field_name() {

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn keyword_ident_in_field_name_span_macro() {
#[derive(Debug)]
struct Foo;

let (collector, handle) = collector::mock()
.new_span(expect::span().with_fields(expect::field("self").with_value(&debug(Foo)).only()))
.only()
.run_with_handle();

with_default(collector, || {
error_span!("span", self = ?Foo);
});
handle.assert_finished();
}
Loading