Make macro scope a real name scope and fix some details#1795
Merged
bors[bot] merged 2 commits intorust-lang:masterfrom Sep 9, 2019
Merged
Make macro scope a real name scope and fix some details#1795bors[bot] merged 2 commits intorust-lang:masterfrom
bors[bot] merged 2 commits intorust-lang:masterfrom
Conversation
Fix some details about module scoping
Contributor
Hm, interesting! So, the original motivation for handing macros separately was that macros are only needed in the initial expansion phase. After that, the rest of the hir should assume a fully macro expanded code, and, for example, name resolution operations should be forbidden from returning a macro. However, I think that this reasoning is actually flawed: the main thing that tries to resolve paths is name resolution and macro expansion itself! Basically the rest of the hir should see paths already resolved. |
matklad
reviewed
Sep 9, 2019
matklad
reviewed
Sep 9, 2019
Contributor
|
bors r+ |
bors Bot
added a commit
that referenced
this pull request
Sep 9, 2019
1795: Make macro scope a real name scope and fix some details r=matklad a=uHOOCCOOHu This PR make macro's module scope a real name scope in `PerNs`, instead of handling `Either<PerNs, MacroDef>` everywhere. In `rustc`, the macro scope behave exactly the same as type and value scope. It is valid that macros, types and values having exact the same name, and a `use` statement will import all of them. This happened to module `alloc::vec` and macro `alloc::vec!`. So `Either` is not suitable here. There is a trap that not only does `#[macro_use]` import all `#[macro_export] macro_rules`, but also imports all macros `use`d in the crate root. In other words, it just _imports all macros in the module scope of crate root_. (Visibility of `use` doesn't matter.) And it also happened to `libstd` which has `use alloc_crate::vec;` in crate root to re-export `alloc::vec`, which it both a module and a macro. The current implementation of `#[macro_use] extern crate` doesn't work here, so that is why only macros directly from `libstd` like `dbg!` work, while `vec!` from `liballoc` doesn't. This PR fixes this. Another point is that, after some tests, I figure out that _`macro_rules` does NOT define macro in current module scope at all_. It defines itself in legacy textual scope. And if `#[macro_export]` is given, it also is defined ONLY in module scope of crate root. (Then being `macro_use`d, as mentioned above) (Well, the nightly [Declarative Macro 2.0](rust-lang/rust#39412) simply always define in current module scope only, just like normal items do. But it is not yet supported by us) After this PR, in my test, all non-builtin macros are resolved now. (Hover text for documentation is available) So it fixes #1688 . Since compiler builtin macros are marked as `#[rustc_doc_only_macro]` instead of `#[macro_export]`, we can simply tweak the condition to let it resolved, but it may cause expansion error. Some critical notes are also given in doc-comments. <img width="447" alt="Screenshot_20190909_223859" src="https://user-images.githubusercontent.com/14816024/64540366-ac1ef600-d352-11e9-804f-566ba7559206.png"> Co-authored-by: uHOOCCOOHu <hooccooh1896@gmail.com>
Contributor
Build succeeded |
bors Bot
added a commit
that referenced
this pull request
Sep 11, 2019
1796: Support completion for macros r=matklad a=uHOOCCOOHu This is based on #1795 , and fixes #1727 Also prettify hover text of macros. Some screenshorts below: Completion in item place. <img width="416" alt="Screenshot_20190910_134056" src="https://user-images.githubusercontent.com/14816024/64587159-fa72da00-d3d0-11e9-86bb-c98f169ec08d.png"> After pressing `tab`. <img width="313" alt="Screenshot_20190910_134111" src="https://user-images.githubusercontent.com/14816024/64587160-fa72da00-d3d0-11e9-9464-21e3f6957bd7.png"> Complete macros from `std`. <img width="588" alt="Screenshot_20190910_134147" src="https://user-images.githubusercontent.com/14816024/64587161-fb0b7080-d3d0-11e9-866e-5161f0d1b546.png"> Hover text. <img width="521" alt="Screenshot_20190910_134242" src="https://user-images.githubusercontent.com/14816024/64587162-fb0b7080-d3d0-11e9-8f09-ad17e3f6702a.png"> Co-authored-by: uHOOCCOOHu <hooccooh1896@gmail.com>
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR make macro's module scope a real name scope in
PerNs, instead of handlingEither<PerNs, MacroDef>everywhere.In
rustc, the macro scope behave exactly the same as type and value scope.It is valid that macros, types and values having exact the same name, and a
usestatement will import all of them. This happened to modulealloc::vecand macroalloc::vec!.So
Eitheris not suitable here.There is a trap that not only does
#[macro_use]import all#[macro_export] macro_rules, but also imports all macrosused in the crate root.In other words, it just imports all macros in the module scope of crate root. (Visibility of
usedoesn't matter.)And it also happened to
libstdwhich hasuse alloc_crate::vec;in crate root to re-exportalloc::vec, which it both a module and a macro.The current implementation of
#[macro_use] extern cratedoesn't work here, so that is why only macros directly fromlibstdlikedbg!work, whilevec!fromliballocdoesn't.This PR fixes this.
Another point is that, after some tests, I figure out that
macro_rulesdoes NOT define macro in current module scope at all.It defines itself in legacy textual scope. And if
#[macro_export]is given, it also is defined ONLY in module scope of crate root. (Then beingmacro_used, as mentioned above)(Well, the nightly Declarative Macro 2.0 simply always define in current module scope only, just like normal items do. But it is not yet supported by us)
After this PR, in my test, all non-builtin macros are resolved now. (Hover text for documentation is available) So it fixes #1688 . Since compiler builtin macros are marked as
#[rustc_doc_only_macro]instead of#[macro_export], we can simply tweak the condition to let it resolved, but it may cause expansion error.Some critical notes are also given in doc-comments.