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

Merged
dtolnay merged 1 commit from local into master 2018-07-27 01:27:42 -05:00
dtolnay commented 2018-07-26 12:01:37 -05:00 (Migrated from github.com)

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.

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.
Dylan-DPC-zz (Migrated from github.com) approved these changes 2018-07-26 12:28:16 -05:00
KodrAus (Migrated from github.com) approved these changes 2018-07-26 18:44:06 -05:00
KodrAus (Migrated from github.com) left a comment

Looks good to me! Thanks @dtolnay

Looks good to me! Thanks @dtolnay
KodrAus (Migrated from github.com) commented 2018-07-26 18:42:45 -05:00

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?

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?
dtolnay (Migrated from github.com) reviewed 2018-07-27 00:53:03 -05:00
dtolnay (Migrated from github.com) commented 2018-07-27 00:53:03 -05:00

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.

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.
Dylan-DPC-zz commented 2018-07-27 01:23:08 -05:00 (Migrated from github.com)

bors: r+

bors: r+
bors[bot] commented 2018-07-27 01:27:41 -05:00 (Migrated from github.com)
# Build succeeded * [continuous-integration/travis-ci/push](https://travis-ci.com/bitflags/bitflags/builds/80180945?utm_source=github_status&utm_medium=notification)
Sign in to join this conversation.
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags!165
No description provided.