Skip to content

Support #[macro_use] on extern crate #1743

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

Merged
merged 5 commits into from
Sep 5, 2019

Conversation

oxalica
Copy link
Contributor

@oxalica oxalica commented Aug 31, 2019

Unfortunately, #1688 is still an issue. My guess is wrong :(

@oxalica oxalica force-pushed the extern_crate_macro_use branch from 0f998aa to dfa758f Compare August 31, 2019 18:04

self.update(module_id, Some(import_id), &macros);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This I think goes into a slightly wrong direction at the moment.

The problem is that there are essentially two modes for resolving macros.

In the new mode, macros are items, and are imported by paths.

In the old mode, there's a mutable set of currently defined macros in the parser, and foo!() invocation is looked up in the map.

This code uses the new model of scopes, but macro_use / macro_export are fundamentally tied to the old mode.

Instead, I think we should do the following:

  • Add exported_macros: FxHashSet<Name, MacroDef> to CrateDefMap (note, this should be crate level)
  • Populate this set when we collect the crate
  • When we encounter an #[macro_use] extern crate, add the crates exported_macros to global_macro_scope in DeCollector

Copy link
Member

Choose a reason for hiding this comment

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

Also, let's put a mark into this if and the corresponding test

Copy link
Member

Choose a reason for hiding this comment

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

And I think we need some special handing for core and std crates, which are macro_use even without an explicit extern crate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

For the prelude, it is now handled in name resolving. Would it be better to also search exported_macros when resolving names (here) instead of early-import?

@oxalica
Copy link
Contributor Author

oxalica commented Sep 5, 2019

When resolving names, if there is no update on a module scope, its unresolved macros will not get expanded.

So I have moved codes for macro importing into ModCollector::collect, the same level to resolve macro_rules, and the updated test shows that it seems to work as expected now.

@matklad
Copy link
Member

matklad commented Sep 5, 2019

Perfect, thanks @uHOOCCOOHu

bors r+

bors bot added a commit that referenced this pull request Sep 5, 2019
1743: Support `#[macro_use]` on `extern crate` r=matklad a=uHOOCCOOHu

Unfortunately, #1688 is still an issue. My guess is wrong :(

Co-authored-by: uHOOCCOOHu <hooccooh1896@gmail.com>
@bors
Copy link
Contributor

bors bot commented Sep 5, 2019

Build succeeded

@bors bors bot merged commit 3ff5d7e into rust-lang:master Sep 5, 2019
bors bot added a commit that referenced this pull request Sep 5, 2019
1771: Further tweak for macro_use on extern crate r=matklad a=uHOOCCOOHu

Some more tweaks to #1743 to behave more like `rustc`
1. Hoist macros from `#[macro_use] extern crate`, so that they can be used before `extern crate`.
2. Implicit `#[macro_use]` for `prelude` if exists


Co-authored-by: uHOOCCOOHu <hooccooh1896@gmail.com>
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.

2 participants