Resolve inner attributes from inside the module#155623
Draft
nik-rev wants to merge 1 commit intorust-lang:mainfrom
Draft
Resolve inner attributes from inside the module#155623nik-rev wants to merge 1 commit intorust-lang:mainfrom
nik-rev wants to merge 1 commit intorust-lang:mainfrom
Conversation
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.
Note: Draft PR, will likely be ready tomorrow
This PR changes the resolution of inner attributes to resolve inside the module, rather than outside.
Example:
The "target module" is the module that the attribute applies to. In this case, it is
b.Before:
super::xis resolved inside ofa, and resolves to// 1.After:
super::xis resolved inside ofb, and resolves to// 2.Motivation
@petrochenkov said
Avoiding problems with new names
If new names are introduced inside of the target module in a way that would change the resolution of the inner attribute, the compilation fails.
Before this PR, the
xwould resolve to// 1.Now, a compilation error is raised:
// 2, the resolution would fail.// 2, the resolution succeeds.This avoids issues where macros expanded inside of the module would create ambiguities in name resolution.
Implementation
Consider:
When resolving
x, we haven't resolvedtargetyet (we don't know whattargetwill contain -- it does not even have aDefIdyet)We create an empty module
mod _ {}as a sibling oftarget, and resolvexinside of it. The resolution (Res) is recorded.When finalizing macro resolutions, we resolve
xagain, this time inside of thetargetmodule, with all items populated.We compare the old resolution (
// 1) (obtained during initial stage) with the new resolution (// 2) (obtained during finalization), if the 2 resolutions differ (which they do), we error.Keeping track of the
targetmoduleWhen resolving
x,targetis not yet available - so itsNodeIdis just a placeholder that we can't actually keep.We need to somehow hold on to the
targetmodule so we can resolve inside of it at a later point, infinalize_macro_resolutions.The way this is done in this PR is hacky, so if there's a better way, let me know:
Spanof thetargetmodule is recordedExpnId) of the inner attribute is recordedfinalize_macro_resolutionswe get theModulecorresponding to the recordedExpnIdModule, and we find a child who'sSpanis inside of the originaltargetmodule's span.targetmodule, yet this time we have theDefIdfor it, so we can resolve inside of it.targetmodule is the crate root.Related
#![rustfmt]and other inert attributes: Stabilize#![rustfmt::skip], and other inner tool attributes #154770r? petrochenkov