Commit Graph

214 Commits

Author SHA1 Message Date
Doug Gregor
acc8a081ca Merge pull request #65670 from DougGregor/freestanding-member-lookup
[Macros] Fix lazy expansion of freestanding macros within types/extensions
2023-05-04 16:02:31 -07:00
Doug Gregor
a690fc4abb [Macros] Fix lazy expansion of freestanding macros within types/extensions
An incorrectly-specified starting context for name lookup meant that
macro lookup itself was failing, and therefore did not trigger
expansion of the freestanding macro or introduction of the
macro-expanded declarations into the context.

Fixes rdar://108824784.
2023-05-04 12:26:12 -07:00
Doug Gregor
b65bf70b7f [Macros] Ensure imported macros work with top-level freestanding macros 2023-05-04 12:26:10 -07:00
Hamish Knight
62b021030f [SourceKit] Include generated macro buffers in diagnostic responses
Introduce a new key `generated_buffers`, which
stores an array of generated buffers. These
include the buffer text, as well as its original
location and any parent buffers.

While here, also fix rdar://107281079 such that
only apply the filename fallback logic to the
pretty-printed Decl case. We ought to remove this
fallback once the editor can handle it though.

rdar://107281079
rdar://107952288
2023-05-02 16:21:44 +01:00
Doug Gregor
00c67413fd Merge pull request #65509 from DougGregor/stored-property-aux-decls
Walk auxiliary declarations when gathering stored properties
2023-04-28 16:43:30 -07:00
Ben Barham
38f2e6c2b9 Merge pull request #65450 from bnbarham/format-macro-expansions
[Macros] Automatically format expanded macros
2023-04-28 16:21:33 -07:00
Doug Gregor
aff9751a12 Walk auxiliary declarations when gathering stored properties
Make sure we walk auxiliary declarations when gathering stored
properties. This ensures that we will get any stored properties
introduced by peer macros.

Fixes rdar://108534298.
2023-04-28 14:05:16 -07:00
Ben Barham
fe2104ca99 [Macros] Automatically format expanded macros
Rather than requiring macro implementations to add required whitespace
and indentation, basic format all macro expansions. Right now this uses
the default four space indentation, we can consider having that inferred
later. Macros can opt-out of automatic formatting by implementing
`formatMode` and setting it to `.disabled`.

Also moves the extra newlines before/after expansions to a new "Inline
Macro" refactoring.

Resolves rdar://107731047.
2023-04-27 21:24:46 -07:00
Doug Gregor
28d24a1095 [Macros] Make sure we get different discriminators for top-level macros
Thank you, Hamish, for the test case!
2023-04-27 14:30:15 -07:00
Doug Gregor
101f018bc0 [Macros] Adjust starting context for macro expansion mangling
Eliminate another circular reference through macro expansion mangling
by adjusting the starting declaration context to ensure that it is from
a suitable "outer" context.

Fixes rdar://108511666.
2023-04-27 10:16:02 -07:00
Doug Gregor
aa127f7535 [Type refinement context] Don't recurse into macro expansions.
Macro expansion buffers will have their type refinement contexts built
lazily, associated with the source file for the macro expansion
buffer. By not recursing into macro expansions, we break false
reference cycles among different files.

Fixes rdar://107394143
2023-04-26 16:06:36 -07:00
Alex Hoppen
3b0dc8d3a9 [Macros] Remove usages of deprecated methods in SwiftSyntaxMacros 2023-04-22 09:52:10 -07:00
Kim de Vos
4209297ea4 Merge pull request #65141 from kimdv/kimdv/remove-ComputedLocation 2023-04-20 17:46:19 +02:00
Holly Borla
d7388346c5 Merge pull request #65313 from hborla/fix-macro-expansion-walking
[Macros] Fix type-checking local pattern bindings in macro-expanded closures.
2023-04-20 07:39:55 -07:00
Kim de Vos
b5698798d1 Remove force unwrapping for source location
Companion of https://github.com/apple/swift-syntax/pull/1532
2023-04-20 08:17:32 +02:00
Holly Borla
265c8a47d9 [Macros] Fix an issue where the constraint system skipped local declarations
inside closures while type checking a macro expansion.

PreCheckExpr, ConstraintGenerator, and other walkers do not walk into macro
expansions. However, the implementation of this macro walking behavior in
ASTWalker would skip any declaration that appears inside any macro expansion
buffer. This is incorrect for cases where the parent is in the same macro
expansion buffer, because the local declaration is not inside a new macro
expansion. This caused bogus errors when type checking expanded macro expressions
containing closures with local declarations, because pre-check and constraint
generation mistakenly skipped local pattern bindings.
2023-04-19 20:11:38 -07:00
Doug Gregor
c38ef86401 Disable lexing #assert unless experimental static assertions are enabled
`#assert` should be usable as a macro name, but the experimental feature
was getting in the way.

Fixes rdar://107894668.
2023-04-19 13:33:29 -07:00
Alex Hoppen
42cb41ded9 Adjustment for SwiftSyntax rename members -> memberBlock
Companion of https://github.com/apple/swift-syntax/pull/1524
2023-04-13 16:28:54 -07:00
Alex Hoppen
9b814218b8 Merge pull request #64954 from ahoppen/ahoppen/swiftsyntax-public-api
[ASTGen] Remove usage of methods that shouldn’t be part of SwiftSynax’s public API
2023-04-11 16:04:08 -07:00
Doug Gregor
efd589018b Merge pull request #64942 from DougGregor/sil-symbol-visitor-aux-decls 2023-04-05 17:35:24 -07:00
Alex Hoppen
32ecb2858d [ASTGen] Remove usage of methods that shouldn’t be part of SwiftSynax’s public API 2023-04-05 16:47:02 -07:00
Alex Hoppen
f3f1a5ed81 Merge pull request #64697 from ahoppen/ahoppen/split-function-parameter
[ASTGen/Macros] Adjustments to split `FunctionParameterSyntax` into multiple nodes for function parameters, closure parameters and enum parameters
2023-04-05 13:33:14 -07:00
Doug Gregor
0e2cce59a8 Make sure we visit auxiliary declarations for symbol visitation.
SIL symbol visitation wasn't visiting top-level auxiliary declarations,
which meant that it skipped peer and freestanding macros at module
scope. This would manifest in errors of the form

    <symbol> is in the generated IR file, but not in TBD file

Fixes rdar://107426276.
2023-04-05 11:45:41 -07:00
Richard Wei
01e6fe2936 [Macros] Code item macros
Add support for declaring and expanding code item macros.  Add experimental feature flag `CodeItemMacros`.
2023-04-04 09:54:57 -07:00
Doug Gregor
1715963540 Merge pull request #64892 from DougGregor/peer-macro-scope-tree-fix 2023-04-04 09:03:46 -07:00
Doug Gregor
f12d1f8652 [ASTScope] Adjust parent location for peer macro to after its attached decl.
Peer macro produce declarations that are semantically alongside the
declaration to which the macro is attached. However, the source
location used for constructing the scope tree was based on the
attribute itself, which meant it was inside the scope of the
declaration to which the macro was attached. This lead to incorrect
unqualified name lookup; in the example that's now a test case, this
meant that the `Self` of the protocol to which the macro was attached
was visible from within the peer declaration. Hilarity (in the form of
a type checker crash) ensued.

Fix the location used for constructing the scope tree of peer macros to
be right after the attached declaration. Fixes rdar://107228586.
2023-04-03 21:27:34 -07:00
Ben Barham
c1b08e591e Merge pull request #64814 from bnbarham/empty-trivia
[Test] Fix up macros using optional trivia API
2023-04-03 09:17:23 -07:00
Doug Gregor
f7e479759d Merge pull request #64854 from DougGregor/top-level-macro-lookup 2023-04-03 06:50:39 -07:00
Doug Gregor
828de17b00 [Macros] Resolve macro names using unqualified lookup that ignores expansions
The macro name resolution in the source lookup cache was only looking at
macros in the current module, meaning that any names introduced by peer
or declaration macros declared in one module but used in another would
not be found by name lookup.

Switch the source lookup cache over to using the same
`forEachPotentialResolvedMacro` API that is used by lookup within
types, so we have consistent name-lookup-level macro resolution in both
places.

... except that would be horribly cyclic, of course, so introduce name
lookup flags to ignore top-level declarations introduced by macro
expansions. This is semantically correct because macro expansions are
not allowed to introduce new macros anyway, because that would have
been a terrible idea.

Fixes rdar://107321469. Peer and declaration macros at module scope
should work a whole lot better now.
2023-04-02 23:15:38 -07:00
Doug Gregor
7dca692c01 Merge pull request #64840 from DougGregor/macro-lookup-in-types 2023-04-02 07:23:58 -07:00
Doug Gregor
5e4118ad97 Allow expansion of member macros on extensions and make those names visible.
Ensure that name lookup triggers the expansion of member macros on
extensions, so we can find those names. And fix a bug that caused
expansion of member macros to crash.
2023-03-31 23:29:40 -07:00
Richard Wei
eb8e984b97 [Macros] Private discriminators for outermost-private MacroExpansionDecl (#64813)
Add a private discriminator to the mangling of an outermost-private `MacroExpansionDecl` so that declaration macros in different files won't have colliding macro expansion buffer names.

rdar://107462515
2023-03-31 20:36:29 -07:00
Ben Barham
2ad8db4ad4 [Test] Fix up macros using optional trivia API
https://github.com/apple/swift-syntax/pull/1477 makes `leadingTrivia`
and `trailingTrivia` non-optional.
2023-03-31 11:57:02 -07:00
Alex Hoppen
962664a2e5 [ASTGen/Macros] Adjustments to split FunctionParameterSyntax into multiple nodes for function parameters, closure parameters and enum parameters
Companion of https://github.com/apple/swift-syntax/pull/1455
2023-03-30 08:31:26 -07:00
Doug Gregor
82de47c494 [Macros] Fix name lookup in macro expansion declaration arguments.
Teach ASTScope to create child nodes for each of the arguments of a
macro expansion declaration, so that we can perform name lookup into
them. Otherwise, we cannot have nontrivial closures in arguments to a
macro expansion declaration.

Fixes rdar://107388151.
2023-03-30 00:10:20 -07:00
Doug Gregor
7c7459ac6b Merge pull request #64641 from DougGregor/macro-replacement-expansion 2023-03-29 20:47:30 -07:00
Doug Gregor
49277f7e89 [Macros] Move most macro definition checking into ASTGen
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.
2023-03-29 16:30:42 -07:00
Doug Gregor
a2737df55a [Macros] Unpack DiagnosticsError into separate diagnostics
Fixes rdar://107289985.
2023-03-29 15:28:52 -07:00
Rintaro Ishizaki
a49ab25ae8 [Macros] Recovery after executable plugin crash
When executable plugins crashed or somehow decided to exit, the compiler
should relaunch the plugin executable before sending another message.
2023-03-23 22:26:42 -07:00
Doug Gregor
906033a30b Expand conformance macros within the conformance lookup table
The conformance lookup table is the central point of truth to
establish which protocols a nominal type conforms to. Ensure that we
expand conformance macros into that table.

Fixes rdar://106886651.
2023-03-21 18:03:44 -07:00
swift-ci
0473879b04 Merge pull request #64184 from DougGregor/macro-unique-name-lookup-type-reconstruction
[Macros] Ensure that name lookup can find uniquely-generated macro names
2023-03-07 15:41:16 -08:00
Richard Wei
aea93fc8ef Merge pull request #63962 from rxwei/rename-createuniquename
[Macros] Rename `createUniqueName` to `makeUniqueName`
2023-03-07 15:34:03 -08:00
Doug Gregor
6ab3bc27f3 Ensure that we lazily find macro-unique names in nominal type context 2023-03-07 12:30:37 -08:00
Doug Gregor
613ec8827c Merge pull request #64132 from hborla/validate-freestanding-macro-expansions
[Macros] Validate freestanding macro expansions.
2023-03-06 08:17:39 -08:00
Richard Wei
833338f9ce [Macros] Top-level freestanding macros (#63553)
Allow freestanding macros to be used at top-level.
- Parse top-level `#…` as `MacroExpansionDecl` when we are not in scripting mode.
- Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
- Experimental support for script mode and brace-level declaration macro expansions: When type-checking a `MacroExpansionExpr`, assign it a substitute `MacroExpansionDecl` if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.
2023-03-06 07:15:20 -08:00
Holly Borla
ed4ce5cd8f [Macros] Validate freestanding macro expansions. 2023-03-05 22:55:28 -08:00
Holly Borla
9b722b9bd2 Merge pull request #64097 from hborla/macro-expansion-validation
[Macros] Diagnose macro expansions containing invalid declarations.
2023-03-05 13:22:38 -08:00
Doug Gregor
ba3504ad96 Add a test for a macro introducing members to an enum
There was a short time where this was broken. Add a test to make sure
we don't regress it later. rdar://105228228
2023-03-04 21:53:55 -08:00
Holly Borla
4a319b2a4e [Macros] Diagnose default literal type overrides in macro expansions. 2023-03-04 15:31:25 -08:00
Holly Borla
cd62c6d513 [Macros] Fix a few issues with invalid macro expansion diagnostics, and add tests. 2023-03-04 15:31:25 -08:00