Skip to content

[Macros] Always visit macro-produced decls as auxiliary decls #64071

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 1 commit into from
Mar 5, 2023

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Mar 3, 2023

Always use Decl::visitAuxiliaryDecls to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove MacroExpansionDecl::getRewritten().

Also make ExpandMacroExpansionDeclRequest cache the buffer ID (similar to other macros) instead of an array of decls.

Copy link
Member

@hborla hborla left a comment

Choose a reason for hiding this comment

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

Awesome!

@rxwei
Copy link
Contributor Author

rxwei commented Mar 3, 2023

@swift-ci please test

@rxwei rxwei force-pushed the macro-aux-decl branch 2 times, most recently from cb83b40 to 1f1188c Compare March 3, 2023 22:08
@xedin xedin removed their request for review March 3, 2023 23:16
@rxwei rxwei force-pushed the macro-aux-decl branch 3 times, most recently from 96e11df to cb079cd Compare March 4, 2023 08:04
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove `MacroExpansionDecl::getRewritten()`.

Also make `ExpandMacroExpansionDeclRequest` cache the buffer ID (similar to other macros) instead of an array of decls.
@rxwei
Copy link
Contributor Author

rxwei commented Mar 5, 2023

@swift-ci please smoke test

// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -disable-availability-checking
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -disable-availability-checking -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: Needed to add -parse-as-library to use @main.

}
// Expand macros based on the name.
if (introducesArbitraryNames || allIntroducedNames.contains(name))
member->visitAuxiliaryDecls([&](Decl *decl) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: Since visitAuxiliaryDecls already expands all attached peer macros, I'm unconditionally adding all macro expansions to the lookup table without checking whether the expansion is from the macro that declared the name. A future improvement could be specifying which CustomAttr to expand as an argument to visitAuxiliaryDecls.

@rxwei
Copy link
Contributor Author

rxwei commented Mar 5, 2023

@swift-ci please smoke test

@rxwei rxwei merged commit 31d73b8 into swiftlang:main Mar 5, 2023
@rxwei rxwei deleted the macro-aux-decl branch March 5, 2023 11:20
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.

3 participants