Commit Graph

17 Commits

Author SHA1 Message Date
Holly Borla
9ac257baa1 [Macros] Diagnose conformance macros as being replaced by extension macros. 2023-07-19 10:02:12 -07:00
Holly Borla
c3e214cbde [Macros] Expand conformance macros as extension macros.
ConformanceMacro now refines ExtensionMacro, so these roles can share
the same expansion request.
2023-06-30 14:25:14 -07:00
Holly Borla
83030a9c63 [Macros] Don't visit macro-generated extensions in 'visitAuxiliaryDecls'.
Macro-generated extensions are hoisted to file scope, because extensions are
not valid in nested scopes. Callers of 'visitAuxiliaryDecls' assume that the
auxiliary decls are in the same decl context as the original, which is clearly
not the case for extensions, and it leads to issues like visiting extension at
the wrong time during SILGen. The extensions are already added to the top-level
decls, so we don't need to visit them as auxiliary decls, and we can type-check
macro-expanded decls at the end of visitation in TypeCheckDeclPrimary.
2023-06-23 23:25:32 -07:00
Doug Gregor
418471494b [Module printing] Don't print attached macros in generated interfaces
Macros are expanded as part of interface generation, so they shouldn't
also be printed into the Swift interface.

Fixes rdar://109378191.
2023-05-25 11:24:53 -07:00
Doug Gregor
8d02e39e62 Ensure that macro-generated conformances make it into .swiftinterface files 2023-05-08 22:26:37 -07:00
Doug Gregor
d30feef92f Ensure that we serialize declarations in synthesized files
Without this, conformances synthesized by macros aren't visible
across modules.

Fixes rdar://109051626.
2023-05-08 22:21:21 -07:00
Doug Gregor
c155bc4cbe Don't visit synthesized extensions twice.
Teach `getTopLevelDeclsWithAuxiliaryDecls` not to provide extension
declarations, because those are covered by the synthesized file, which
all clients need to walk anyway. Without this, we end up asserting in
TBD generation about duplicate symbol visitation.

Encountered while investigating rdar://108056018.
2023-04-19 10:51:55 -07:00
Holly Borla
ae32673553 [Macros] Fix visiting nested conformance macro declarations in SIL and IRGen.
When expanding a conformance macro, the generated extension decls are added to
the TopLevelDecls vector of the synthesized file unit to expose them to
sf->getSynthesizedFile()->getTopLevelDecls(). There are two problems with this:

  1. These decls are also visited via visitAuxiliaryDecls() for the purpose of
     type checking. This causes problems in code generation because the extensions
     are visited separately, and because SIL and IRGen assume nested auxiliary decls
     are members.
  2. SILGen only emits top-level decls directly from the source file rather than its
     synthesized file. Auxiliary decls are visited here, but this doesn't work for
     nested conformance macros because the attached-to decl is not at the top-level,
     so macro-generated conformances for nested types never emit their descriptor.

To fix this in the short term, visit top-level decls in the synthesized file that are
generated by conformance macros, and skip auxiliary extension decls when emitting type
members. This fix is narrowly scoped to only impact macros, but in the future this
warrants a more structural fix to better handle top-level decls in the synthesized file.
2023-04-17 10:33:25 -07:00
Doug Gregor
a23d39bdfb [Macros] Mangle attached macro expansions based only on syntactic information
The mangling of attached macro expansions based on the declaration to
which they are attached requires semantic information (specifically,
the interface type of that declaration) that caused cyclic
dependencies during type checking. Replace the mangling with a
less-complete mangling that only requires syntactic information from
the declaration, i.e., the name of the declaration to which the macro
was attached.

This eliminates reference cycles that occur with attached macros that
produce arbitrary names.
2023-04-11 23:40:28 -04:00
Rintaro Ishizaki
02a604e7e7 [Tests/Macros] Add %target-codesign before %target-run
rdar://107658689
2023-04-05 12:48:09 -07:00
Ben Barham
e5a28caa9a [Test] Fix swift_swift_parser feature and use in tests
The macro tests were all using "REQUIRES: OS=macosx" as a proxy for
"have the Swift Swift parser". There was an existing feature for this,
but it was just checking whether the path was passed through. Fix that
to use the same variable as in CMake.

Also remove all extraneous `-I` and `-L` to the host libs in the target
invocations.
2023-04-03 09:25:03 -07:00
Rintaro Ishizaki
1d2fd4223f [Test] Add %host_triple and %host_sdkroot substitutions
Macro tests need to build host libraries/tools. We can't use %target-*
substitutions for that.

rdar://107398734
2023-03-31 07:41:41 -07:00
Holly Borla
c9a86ac4fa [Macros] Diagnose macro expansions containing invalid declarations, such as
new macro decls or value decls whose names are not covered by the macro.
2023-03-04 15:31:24 -08:00
Doug Gregor
f88d2c638f Clean up feature flags for macros.
Enable expression macros by default, and add separate feature flags for
attached and freestanding macros.
2023-03-02 14:34:59 -08:00
Doug Gregor
d1bfe72a45 [Macros] Register the extension for a conformance macro fully.
When we form an extension for a conformance macro, make sure to
register that extension as being part of the nominal type to which the
conformance macro is attached. This ensures that the conformance is
seen everywhere. Crucially, this ensures that that protocol
conformance table handles conformances that are implied by the
conformance macro. For example, a conformance macro that introduces a
`Hashable` conformance will also imply an `Equatable` conformance;
that wasn't happening before this change.

This is needed to make the `OptionSet` macro work fully.
2023-03-01 21:45:04 -08:00
Holly Borla
4b2571aa9c [Macros] Add tests for conditional conformance expansion. 2023-02-23 21:33:03 -08:00
Holly Borla
a3caacd309 [Macros] Initial implementation of conformance macros. 2023-02-23 20:43:11 -08:00