From f1ab24eccd9dd00ea383c62afe56572be484df86 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Sun, 30 Jun 2024 12:16:33 -0700 Subject: [PATCH 1/2] fix: prefix macro calls with ::core to avoid clashing with local macros This commit fixes a bug where a macro call to macros defined in the standard lib from the `tracing` and `tracing-core` crates could be resolved to a local macro with the same name, causing a compilation error. This commit prefixes these calls with `::core::` to ensure that they are resolved to the standard library macros. Fixes: --- tracing-core/src/lib.rs | 6 +++--- tracing/src/macros.rs | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tracing-core/src/lib.rs b/tracing-core/src/lib.rs index eafdba9120..9d4a3ccc79 100644 --- a/tracing-core/src/lib.rs +++ b/tracing-core/src/lib.rs @@ -264,9 +264,9 @@ macro_rules! metadata { $name, $target, $level, - ::core::option::Option::Some(file!()), - ::core::option::Option::Some(line!()), - ::core::option::Option::Some(module_path!()), + ::core::option::Option::Some(::core::file!()), + ::core::option::Option::Some(::core::line!()), + ::core::option::Option::Some(::core::module_path!()), $crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)), $kind, ) diff --git a/tracing/src/macros.rs b/tracing/src/macros.rs index 5de45cb75b..8710ee1723 100644 --- a/tracing/src/macros.rs +++ b/tracing/src/macros.rs @@ -693,11 +693,11 @@ macro_rules! event { (target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* } )=> ({ use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: concat!( + name: ::core::concat!( "event ", - file!(), + ::core::file!(), ":", - line!() + ::core::line!() ), kind: $crate::metadata::Kind::EVENT, target: $target, @@ -854,11 +854,11 @@ macro_rules! event { (target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({ use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: concat!( + name: ::core::concat!( "event ", - file!(), + ::core::file!(), ":", - line!() + ::core::line!() ), kind: $crate::metadata::Kind::EVENT, target: $target, @@ -1184,11 +1184,11 @@ macro_rules! enabled { if $crate::level_enabled!($lvl) { use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: concat!( + name: ::core::concat!( "enabled ", - file!(), + ::core::file!(), ":", - line!() + ::core::line!() ), kind: $kind.hint(), target: $target, From 5960e0aa4493d33df5e4911248e4746259726559 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Tue, 2 Jul 2024 06:40:04 -0700 Subject: [PATCH 2/2] fix: use __macro_support:: instead of ::core:: in macros This ensures that the tracing lib correctly builds when a crate is named `core`. See https://github.com/tokio-rs/tracing/issues/2761 and https://github.com/tokio-rs/tracing/issues/2762 for more info. --- tracing-core/src/lib.rs | 14 +++++++++++--- tracing/src/lib.rs | 2 +- tracing/src/macros.rs | 18 +++++++++--------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/tracing-core/src/lib.rs b/tracing-core/src/lib.rs index 9d4a3ccc79..b78cfcecdf 100644 --- a/tracing-core/src/lib.rs +++ b/tracing-core/src/lib.rs @@ -165,6 +165,14 @@ #[cfg(feature = "alloc")] extern crate alloc; +#[doc(hidden)] +pub mod __macro_support { + // Re-export the `core` functions that are used in macros. This allows + // a crate to be named `core` and avoid name clashes. + // See here: https://github.com/tokio-rs/tracing/issues/2761 + pub use core::{file, line, module_path, option::Option}; +} + /// Statically constructs an [`Identifier`] for the provided [`Callsite`]. /// /// This may be used in contexts, such as static initializers, where the @@ -264,9 +272,9 @@ macro_rules! metadata { $name, $target, $level, - ::core::option::Option::Some(::core::file!()), - ::core::option::Option::Some(::core::line!()), - ::core::option::Option::Some(::core::module_path!()), + $crate::__macro_support::Option::Some($crate::__macro_support::file!()), + $crate::__macro_support::Option::Some($crate::__macro_support::line!()), + $crate::__macro_support::Option::Some($crate::__macro_support::module_path!()), $crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)), $kind, ) diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index de08ec408f..3e88cf9f2a 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -1005,7 +1005,7 @@ pub mod __macro_support { // Re-export the `core` functions that are used in macros. This allows // a crate to be named `core` and avoid name clashes. // See here: https://github.com/tokio-rs/tracing/issues/2761 - pub use core::{concat, format_args, iter::Iterator, option::Option}; + pub use core::{concat, file, format_args, iter::Iterator, line, option::Option}; /// Callsite implementation used by macro-generated code. /// diff --git a/tracing/src/macros.rs b/tracing/src/macros.rs index 8710ee1723..9d178636ff 100644 --- a/tracing/src/macros.rs +++ b/tracing/src/macros.rs @@ -693,11 +693,11 @@ macro_rules! event { (target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* } )=> ({ use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: ::core::concat!( + name: $crate::__macro_support::concat!( "event ", - ::core::file!(), + $crate::__macro_support::file!(), ":", - ::core::line!() + $crate::__macro_support::line!() ), kind: $crate::metadata::Kind::EVENT, target: $target, @@ -854,11 +854,11 @@ macro_rules! event { (target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({ use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: ::core::concat!( + name: $crate::__macro_support::concat!( "event ", - ::core::file!(), + $crate::__macro_support::file!(), ":", - ::core::line!() + $crate::__macro_support::line!() ), kind: $crate::metadata::Kind::EVENT, target: $target, @@ -1184,11 +1184,11 @@ macro_rules! enabled { if $crate::level_enabled!($lvl) { use $crate::__macro_support::Callsite as _; static __CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! { - name: ::core::concat!( + name: $crate::__macro_support::concat!( "enabled ", - ::core::file!(), + $crate::__macro_support::file!(), ":", - ::core::line!() + $crate::__macro_support::line!() ), kind: $kind.hint(), target: $target,