Skip to content

Commit

Permalink
tracing: removed core imports in macros (tokio-rs#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 Oct 20, 2023
1 parent 90ef8e3 commit f93cfa0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
4 changes: 4 additions & 0 deletions tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,10 @@ pub mod __macro_support {
use crate::{collect::Interest, Metadata};
use core::fmt;
use core::sync::atomic::{AtomicU8, Ordering};
// 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 @@ -628,7 +628,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 @@ -679,7 +679,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 @@ -737,7 +737,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 @@ -790,7 +790,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 @@ -840,7 +840,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 @@ -895,7 +895,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 @@ -911,7 +911,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 @@ -971,7 +971,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, $($k:ident).+ = $($field:tt)*) => (
Expand Down Expand Up @@ -2805,120 +2805,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 @@ -2966,7 +2966,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 @@ -2977,7 +2977,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 f93cfa0

Please sign in to comment.