Skip to content

[Macros] [SE-0382] Implement macros defined as expansions of other macros #64641

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 9 commits into from
Mar 30, 2023

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Mar 27, 2023

Implement support for defining a macro in terms of the expansion of another macro, e.g.,

@freestanding(expression)
macro m1(a: Int) = #m2(a: a, b: 17, c: "Hello")

There are a few steps along the way:

  • Adopt macro definition checking provided by the MacroDeclSyntax.checkDefinition operation (implemented in swift-syntax) in lieu of the checking we currently do on the C++ side
  • Only define $Macros for compilers that can actually expand macros, and make sure we only run tests that require macro checking for compiler that support expansion.
  • Fix an issue with the source ranges for Fix-Its that are bridged from Swift diagnostics over to C++ diagnostics, where we were replacing the leading/trailing trivia of nodes along with the node itself. Only replace the node itself.
  • Fix name lookup within the definitions of macros, so we can find the macro parameters.
  • Make traversal walk the definitions of macros, rather than ignoring them cringe. Improve the AST verifier to better understand macros.

This aspect of macros is tracked by rdar://104044325

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the macro-replacement-expansion branch from 289f05a to ea0398d Compare March 29, 2023 19:50
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor changed the title [Macros] Move most macro definition checking into ASTGen [Macros] Implement macros defined as expansions of other macros Mar 29, 2023
@DougGregor DougGregor changed the title [Macros] Implement macros defined as expansions of other macros [Macros] [SE-0382] Implement macros defined as expansions of other macros Mar 29, 2023
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@@ -2,6 +2,9 @@
//
// REQUIRES: nonexecutable_test
// REQUIRES: swift_swift_parser
// FIXME: Swift parser is not enabled on Linux CI yet.
// REQUIRES: OS=macosx
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you ran into whatever I ran into in #64089 that caused me not to merge it, though I don't remember what it was 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

YEAH :(

In preparation for supporting macros that are defined in terms of other
macros, adopt macro definition checking provided by the
`MacroDeclSyntax.checkDefinition` operation (implemented in
swift-syntax). Use this in lieu of the checking on the C++ side.

This required me to finally fix an issue with the source ranges for
Fix-Its, where we were replacing the leading/trailing trivia of nodes
along with the node itself, even though that's incorrect: we should
only replce the node itself, and there are other Fix-It kinds for
replacing leading or trailing trivia.
We were enabling the `$Macros` feature unconditionally, even when the
compiler itself doesn't support macros (because it's missing
swift-syntax). Change this to only enable the feature when the
compiler supports it.
Handle a trivial macro defined in terms of another macro.
This enables macros to be defined in terms of other macros.
These expressions are a little tricky, because we don't always
type-check them in the way that clients would expect. We might want to
change the representation here, but until then... don't walk the
untypechecked ones.
This should complete the implementation of expanded macro
definitions, rdar://104044325.
@DougGregor DougGregor force-pushed the macro-replacement-expansion branch from 58fe207 to 68b367b Compare March 29, 2023 23:33
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit 7c7459a into swiftlang:main Mar 30, 2023
@DougGregor DougGregor deleted the macro-replacement-expansion branch March 30, 2023 03:47
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