-
Notifications
You must be signed in to change notification settings - Fork 144
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
Conversation
This fixes the following error when using Rust 2018 style macro imports. use bitflags::bitflags; error: cannot find macro `__bitflags!` in this scope --> src/main.rs:5:1 | 5 | / bitflags! { 6 | | struct Flags: u32 { 7 | | const A = 0b00000001; 8 | | const B = 0b00000010; ... | 11 | | } 12 | | } | |_^ help: you could try the macro: `bitflags` | The `local_inner_macros` modifier resolves all macro invocations made from within that macro as helpers in the same crate. So if `bitflags!` expands to an invocation of `__bitflags!` then this would be resolved as `$crate::__bitflags!` rather than requiring the caller to have `__bitflags` in scope. The attribute is ignored by pre-2018 compilers so bitflags will continue to work as normal with #[macro_use]. In the future when dropping compatibility with pre-2018 compilers we can remove the `local_inner_macros` modifier and use our own explicit `$crate::` prefixes on invocations of helper macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thanks @dtolnay
// local_inner_macros so can only directly call macros from this crate. | ||
#[macro_export] | ||
#[doc(hidden)] | ||
macro_rules! __bitflags_stringify { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
bors: r+ |
165: Use local_inner_macros to resolve all helper macros within $crate r=Dylan-DPC a=dtolnay This fixes the following error when using Rust 2018 style macro imports. ```rust use bitflags::bitflags; ``` ```console error: cannot find macro `__bitflags!` in this scope --> src/main.rs:5:1 | 5 | / bitflags! { 6 | | struct Flags: u32 { 7 | | const A = 0b00000001; 8 | | const B = 0b00000010; ... | 11 | | } 12 | | } | |_^ help: you could try the macro: `bitflags` | ``` The `local_inner_macros` modifier resolves all macro invocations made from within that macro as helpers in the same crate. So if `bitflags!` expands to an invocation of `__bitflags!` then this would be resolved as `$crate::__bitflags!` rather than requiring the caller to have `__bitflags` in scope. The attribute is ignored by pre-2018 compilers so bitflags will continue to work as normal with #[macro_use]. In the future when dropping compatibility with pre-2018 compilers we can remove the `local_inner_macros` modifier and use our own explicit `$crate::` prefixes on invocations of helper macros. Co-authored-by: David Tolnay <dtolnay@gmail.com>
Build succeeded |
I thought I needed it for Commit::parent_count(); turns out that I didn't, but since I've tested the update anyway I thought I'd include it. I had to delete Cargo.lock and let cargo regenerate it, since otherwise I got an error that I think was fixed in a newer bitflags version: bitflags/bitflags#165
This fixes the following error when using Rust 2018 style macro imports.
The
local_inner_macros
modifier resolves all macro invocations made from within that macro as helpers in the same crate. So ifbitflags!
expands to an invocation of__bitflags!
then this would be resolved as$crate::__bitflags!
rather than requiring the caller to have__bitflags
in scope.The attribute is ignored by pre-2018 compilers so bitflags will continue to work as normal with #[macro_use].
In the future when dropping compatibility with pre-2018 compilers we can remove the
local_inner_macros
modifier and use our own explicit$crate::
prefixes on invocations of helper macros.