Skip to content

Commit

Permalink
tracing: removed core imports in macros (#2762)
Browse files Browse the repository at this point in the history
When a user has a crate named `core`, it can cause issues
because our crates import from `::core::*`. Now we are
importing from `$crate::__macro_support::*` and there will
be no more name clashes.
  • Loading branch information
kaffarell authored and hawkw committed Nov 7, 2023
1 parent 346d6e6 commit 119f91a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
5 changes: 4 additions & 1 deletion tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,10 @@ pub mod subscriber;
pub mod __macro_support {
pub use crate::callsite::Callsite;
use crate::{subscriber::Interest, Metadata};
pub use core::concat;
// 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};

/// Callsite implementation used by macro-generated code.
///
Expand Down
56 changes: 28 additions & 28 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ macro_rules! event {
target: $target,
parent: $parent,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -678,7 +678,7 @@ macro_rules! event {
name: $name,
target: $target,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -736,7 +736,7 @@ macro_rules! event {
target: $target,
parent: $parent,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -789,7 +789,7 @@ macro_rules! event {
name: $name,
parent: $parent,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -839,7 +839,7 @@ macro_rules! event {
$crate::event!(
name: $name,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -894,7 +894,7 @@ macro_rules! event {
$crate::event!(
target: $target,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand All @@ -910,7 +910,7 @@ macro_rules! event {
target: module_path!(),
parent: $parent,
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
(parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($field:tt)*) => (
Expand Down Expand Up @@ -970,7 +970,7 @@ macro_rules! event {
$crate::event!(
target: module_path!(),
$lvl,
{ message = ::core::format_args!($($arg)+), $($fields)* }
{ message = $crate::__macro_support::format_args!($($arg)+), $($fields)* }
)
);
( $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
Expand Down Expand Up @@ -2802,120 +2802,120 @@ macro_rules! valueset {
// };
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$val) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$val) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$val as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$val as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$($k).+ as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$($k).+ as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$($k).+) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$($k).+) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$($k).+) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$($k).+) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = ?$val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$val) as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = %$val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$val) as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+ = $val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$val as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$val as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $($k:ident).+) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$($k).+ as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$($k).+ as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, ?$($k:ident).+) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$($k).+) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$($k).+) as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, %$($k:ident).+) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$($k).+) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$($k).+) as &dyn Value)) },
$next,
)
};

// Handle literal names
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$val) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$val) as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr, $($rest:tt)*) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$val as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$val as &dyn Value)) },
$next,
$($rest)*
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = ?$val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&debug(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&debug(&$val) as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = %$val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&display(&$val) as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&display(&$val) as &dyn Value)) },
$next,
)
};
(@ { $(,)* $($out:expr),* }, $next:expr, $k:literal = $val:expr) => {
$crate::valueset!(
@ { $($out),*, (&$next, ::core::option::Option::Some(&$val as &dyn Value)) },
@ { $($out),*, (&$next, $crate::__macro_support::Option::Some(&$val as &dyn Value)) },
$next,
)
};
Expand Down Expand Up @@ -2963,7 +2963,7 @@ macro_rules! valueset {

// Remainder is unparsable, but exists --- must be format args!
(@ { $(,)* $($out:expr),* }, $next:expr, $($rest:tt)+) => {
$crate::valueset!(@ { (&$next, ::core::option::Option::Some(&::core::format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, )
$crate::valueset!(@ { (&$next, $crate::__macro_support::Option::Some(&$crate::__macro_support::format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, )
};

// === entry ===
Expand All @@ -2974,7 +2974,7 @@ macro_rules! valueset {
let mut iter = $fields.iter();
$fields.value_set($crate::valueset!(
@ { },
::core::iter::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
$crate::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
$($kvs)+
))
}
Expand Down

0 comments on commit 119f91a

Please sign in to comment.