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

Conversation

dtolnay
Copy link
Contributor

@dtolnay dtolnay commented Jul 26, 2018

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.

    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.
@dtolnay dtolnay requested a review from KodrAus July 26, 2018 17:03
Copy link
Member

@KodrAus KodrAus left a 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 {
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.

@Dylan-DPC-zz
Copy link
Member

bors: r+

bors bot added a commit that referenced this pull request Jul 27, 2018
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>
@bors
Copy link
Contributor

bors bot commented Jul 27, 2018

Build succeeded

@bors bors bot merged commit e72681e into master Jul 27, 2018
@dtolnay dtolnay deleted the local branch August 21, 2018 19:49
nickolay added a commit to nickolay/git-absorb that referenced this pull request Jul 15, 2019
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants