Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local_inner_macros to resolve all helper macros within $crate #165

Merged
merged 1 commit into from
Jul 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub extern crate core as _core;
/// assert_eq!(format!("{:?}", Flags::B), "B");
/// }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! bitflags {
(
$(#[$outer:meta])*
Expand Down Expand Up @@ -386,7 +386,7 @@ macro_rules! bitflags {
};
}

#[macro_export]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __bitflags {
(
Expand Down Expand Up @@ -415,7 +415,7 @@ macro_rules! __bitflags {
};
}

#[macro_export]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_bitflags {
(
Expand Down Expand Up @@ -467,14 +467,14 @@ macro_rules! __impl_bitflags {
$(
if <$BitFlags as __BitFlags>::$Flag(self) {
if !first {
try!(f.write_str(" | "));
f.write_str(" | ")?;
}
first = false;
try!(f.write_str(stringify!($Flag)));
f.write_str(__bitflags_stringify!($Flag))?;
}
)+
if first {
try!(f.write_str("(empty)"));
f.write_str("(empty)")?;
}
Ok(())
}
Expand Down Expand Up @@ -771,6 +771,16 @@ macro_rules! __impl_bitflags {
};
}

// Same as std::stringify but callable from __impl_bitflags, which needs to use
// local_inner_macros so can only directly call macros from this crate.
#[macro_export]
#[doc(hidden)]
macro_rules! __bitflags_stringify {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting... Is this a workaround to an inherent feature of local_inner_macros or just an implementation detail that might not be necessary in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what local_inner_macros means -- macro calls in the expansion are resolved as local helper macros. So a local_inner_macros macro cannot directly call a macro from a different crate, such as stringify from std.

($s:ident) => {
stringify!($s)
};
}

#[cfg(feature = "example_generated")]
pub mod example_generated;

Expand Down