attributes: Globally qualify attribute paths (#3126)

Avoid ambiguities with any user-defined `tracing` modules by globally qualifying types used in the attribute-generated code e.g., `::tracing::Level`.
This commit is contained in:
Heath Stewart 2025-05-28 07:44:19 +00:00 committed by Hayden Stainsby
parent 2931b6f426
commit 4deba4aaea
3 changed files with 33 additions and 15 deletions

View File

@ -378,7 +378,7 @@ impl ToTokens for Field {
// `instrument` produce empty field values, so changing it now // `instrument` produce empty field values, so changing it now
// is a breaking change. agh. // is a breaking change. agh.
let name = &self.name; let name = &self.name;
tokens.extend(quote!(#name = tracing::field::Empty)) tokens.extend(quote!(#name = ::tracing::field::Empty))
} else { } else {
self.kind.to_tokens(tokens); self.kind.to_tokens(tokens);
self.name.to_tokens(tokens); self.name.to_tokens(tokens);
@ -454,11 +454,11 @@ impl Parse for Level {
impl ToTokens for Level { impl ToTokens for Level {
fn to_tokens(&self, tokens: &mut TokenStream) { fn to_tokens(&self, tokens: &mut TokenStream) {
match self { match self {
Level::Trace => tokens.extend(quote!(tracing::Level::TRACE)), Level::Trace => tokens.extend(quote!(::tracing::Level::TRACE)),
Level::Debug => tokens.extend(quote!(tracing::Level::DEBUG)), Level::Debug => tokens.extend(quote!(::tracing::Level::DEBUG)),
Level::Info => tokens.extend(quote!(tracing::Level::INFO)), Level::Info => tokens.extend(quote!(::tracing::Level::INFO)),
Level::Warn => tokens.extend(quote!(tracing::Level::WARN)), Level::Warn => tokens.extend(quote!(::tracing::Level::WARN)),
Level::Error => tokens.extend(quote!(tracing::Level::ERROR)), Level::Error => tokens.extend(quote!(::tracing::Level::ERROR)),
Level::Path(ref pat) => tokens.extend(quote!(#pat)), Level::Path(ref pat) => tokens.extend(quote!(#pat)),
} }
} }

View File

@ -199,7 +199,7 @@ fn gen_block<B: ToTokens>(
}) })
.map(|(user_name, (real_name, record_type))| match record_type { .map(|(user_name, (real_name, record_type))| match record_type {
RecordType::Value => quote!(#user_name = #real_name), RecordType::Value => quote!(#user_name = #real_name),
RecordType::Debug => quote!(#user_name = tracing::field::debug(&#real_name)), RecordType::Debug => quote!(#user_name = ::tracing::field::debug(&#real_name)),
}) })
.collect(); .collect();
@ -223,7 +223,7 @@ fn gen_block<B: ToTokens>(
let custom_fields = &args.fields; let custom_fields = &args.fields;
quote!(tracing::span!( quote!(::tracing::span!(
target: #target, target: #target,
#(parent: #parent,)* #(parent: #parent,)*
#level, #level,
@ -241,10 +241,10 @@ fn gen_block<B: ToTokens>(
let level_tokens = event_args.level(Level::Error); let level_tokens = event_args.level(Level::Error);
match event_args.mode { match event_args.mode {
FormatMode::Default | FormatMode::Display => Some(quote!( FormatMode::Default | FormatMode::Display => Some(quote!(
tracing::event!(target: #target, #level_tokens, error = %e) ::tracing::event!(target: #target, #level_tokens, error = %e)
)), )),
FormatMode::Debug => Some(quote!( FormatMode::Debug => Some(quote!(
tracing::event!(target: #target, #level_tokens, error = ?e) ::tracing::event!(target: #target, #level_tokens, error = ?e)
)), )),
} }
} }
@ -256,10 +256,10 @@ fn gen_block<B: ToTokens>(
let level_tokens = event_args.level(args_level); let level_tokens = event_args.level(args_level);
match event_args.mode { match event_args.mode {
FormatMode::Display => Some(quote!( FormatMode::Display => Some(quote!(
tracing::event!(target: #target, #level_tokens, return = %x) ::tracing::event!(target: #target, #level_tokens, return = %x)
)), )),
FormatMode::Default | FormatMode::Debug => Some(quote!( FormatMode::Default | FormatMode::Debug => Some(quote!(
tracing::event!(target: #target, #level_tokens, return = ?x) ::tracing::event!(target: #target, #level_tokens, return = ?x)
)), )),
} }
} }
@ -320,7 +320,7 @@ fn gen_block<B: ToTokens>(
let __tracing_instrument_future = #mk_fut; let __tracing_instrument_future = #mk_fut;
if !__tracing_attr_span.is_disabled() { if !__tracing_attr_span.is_disabled() {
#follows_from #follows_from
tracing::Instrument::instrument( ::tracing::Instrument::instrument(
__tracing_instrument_future, __tracing_instrument_future,
__tracing_attr_span __tracing_attr_span
) )
@ -344,7 +344,7 @@ fn gen_block<B: ToTokens>(
// regression in case the level is enabled. // regression in case the level is enabled.
let __tracing_attr_span; let __tracing_attr_span;
let __tracing_attr_guard; let __tracing_attr_guard;
if tracing::level_enabled!(#level) || tracing::if_log_enabled!(#level, {true} else {false}) { if ::tracing::level_enabled!(#level) || ::tracing::if_log_enabled!(#level, {true} else {false}) {
__tracing_attr_span = #span; __tracing_attr_span = #span;
#follows_from #follows_from
__tracing_attr_guard = __tracing_attr_span.enter(); __tracing_attr_guard = __tracing_attr_span.enter();
@ -724,7 +724,7 @@ impl<'block> AsyncInfo<'block> {
let async_attrs = &async_expr.attrs; let async_attrs = &async_expr.attrs;
if pinned_box { if pinned_box {
quote! { quote! {
Box::pin(#(#async_attrs) * async move { #instrumented_block }) ::std::boxed::Box::pin(#(#async_attrs) * async move { #instrumented_block })
} }
} else { } else {
quote! { quote! {

View File

@ -324,3 +324,21 @@ fn target_name_ident() {
handle.assert_finished(); handle.assert_finished();
} }
#[test]
fn user_tracing_module() {
use ::tracing::field::Empty;
// Reproduces https://github.com/tokio-rs/tracing/issues/3119
#[instrument(fields(f = Empty))]
fn my_fn() {
assert_eq!("test", tracing::my_other_fn());
}
mod tracing {
#[allow(dead_code)]
pub fn my_other_fn() -> &'static str {
"test"
}
}
}