Skip to content

Commit

Permalink
fix: prefix macro calls with ::core to avoid clashing with local macr…
Browse files Browse the repository at this point in the history
…os (#3024)

fix: prefix macro calls with `__macro_support ` to avoid clashes with local macros

Fixes: <#3023>

This ensures that the tracing lib correctly builds when a crate is named
`core`. See #2761 and
#2762 for more info.
  • Loading branch information
joshka authored Jul 24, 2024
1 parent 28b3ce7 commit 1898311
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
14 changes: 11 additions & 3 deletions tracing-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -264,9 +272,9 @@ macro_rules! metadata {
$name,
$target,
$level,
::core::option::Option::Some(file!()),
::core::option::Option::Some(line!()),
::core::option::Option::Some(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,
)
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
18 changes: 9 additions & 9 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: $crate::__macro_support::concat!(
"event ",
file!(),
$crate::__macro_support::file!(),
":",
line!()
$crate::__macro_support::line!()
),
kind: $crate::metadata::Kind::EVENT,
target: $target,
Expand Down Expand Up @@ -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: $crate::__macro_support::concat!(
"event ",
file!(),
$crate::__macro_support::file!(),
":",
line!()
$crate::__macro_support::line!()
),
kind: $crate::metadata::Kind::EVENT,
target: $target,
Expand Down Expand Up @@ -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: $crate::__macro_support::concat!(
"enabled ",
file!(),
$crate::__macro_support::file!(),
":",
line!()
$crate::__macro_support::line!()
),
kind: $kind.hint(),
target: $target,
Expand Down

0 comments on commit 1898311

Please sign in to comment.