Commit Graph

24 Commits

Author SHA1 Message Date
Hamish Knight
a359816a3a [Macros] Ban extension macros on local nested types
Make sure we check whether we are in any local parent context.
2025-03-28 21:35:59 +00:00
Hamish Knight
aaeb38d7f4 [Macros] Expand extension macros at the top-level
Ensure we always expand extension macros after the top-level decl
for the given attached decl. This ensures correct unqualified lookup
behavior, and bans macro implementations from extending the
unqualified name (they're expected to use `providingExtensionsOf`
instead, which uses the qualified name).

rdar://148119538
2025-03-28 21:35:59 +00:00
Holly Borla
48eb4b7714 [Availability] Lazily expand type refinement contexts for extensions. 2024-02-02 17:34:40 -08:00
Allan Shortlidge
2730c19891 Macros: Stop explicitly enabling accepted ExtensionMacros feature in tests.
NFC.
2023-12-10 10:10:15 -08:00
Holly Borla
4f68514f0a [Concurrency] Check the outermost parent source file when diagnosing
Sendable conformances outside the defining source file.

This allows macros to add a Sendable conformance when the macro is applied
in the same source file as the primary declaration.
2023-12-02 09:58:11 -08:00
Doug Gregor
221e0e0627 Merge pull request #68059 from DougGregor/module-interface-print-freestanding-macro-expansion
[Swift interface] Print declarations produced by declaration macro expansion
2023-08-22 13:42:00 -07:00
Doug Gregor
8e4484660a [Macros] Add test case for #67989 / rdar://114105615.
The bug described by this report has been fixed on main. Add a test case
to ensure that it doesn't regress.
2023-08-21 23:31:47 -07:00
Doug Gregor
323561b157 [Macros] Use outermost source file for establishing (file)private access scope
Fixes rdar://114048069.
2023-08-21 22:01:19 -07:00
Doug Gregor
ab576b31a7 Always supersede conformances implied by pre-macro-expansion conformances
Pre-macro-expansion conformances are introduced at the point where an
attached extension macro is attached to a particular nominal type, and
can imply other conformances. Once the macro is expanded, they are
expected to be replaced by the real conformance from the extension
produced by the macro. This includes any other conformances that are
implied by that conformances. Ensure that the real conformance---and
every conformances it implies---are considered "better" than the
pre-expansion conformances.

Fixes a bug where we would pick the wrong (pre-expansion)
conformances, which would then fail to get fully type-checked prior to
serialization. This could accept invalid code that then crashed the
compiler, as in rdar://112916159.
2023-08-20 09:27:38 -07:00
Doug Gregor
b7bfaf3522 [Macros] Fix handling of extension macro conformances and witnesses
Fix two inter-related issues with extension macros that provide
conformances to a protocol, the combined effect of which is that one
cannot meaningfully provide extension macros that implement
conformances to a protocol like Equatable or Hashable that also
supports auto-synthesis.

The first issue involves name lookup of operators provided by macro
expansions. The logic for performing qualified lookup in addition to
unqualified lookup (for operators) did not account for extension
macros in the same manner as it did for member macros, so we would not
find a macro-produced operator (such as operator==) in witness
matching.

The second issue is more fundamental, which is that the conformance
lookup table would create `NormalProtocolConformance` instances for
pre-macro-expansion conformance entries, even though these should
always have been superseded by explicit conformances within the macro
expansion buffers. The end result is that we could end up with two
`NormalProtocolConformance` records for the same conformance. Some
code was taught to ignore the pre-expansion placeholder conformances,
other code was not. Instead, we now refuse to create a
`NormalProtocolConformance` for the pre-expansion entries, and remove
all of the special-case checks for this, so we always using the
superseding explicit conformances produced by the macro expansions (or
error if the macros don't produce them).

Fixes rdar://113994346 / https://github.com/apple/swift/issues/66348
2023-08-16 19:18:36 -07:00
Holly Borla
0be80c8287 [Macros] Separate each protocol name with commas when forming the conformance
list buffer for extension macro expansion.
2023-08-10 22:13:22 -07:00
Holly Borla
309e3403f6 [Macros] Always consider pre-macro-expansion conformances as subsumed by
other conformance entry kinds, before considering availability.
2023-08-10 18:19:33 -07:00
Ben Barham
9691060418 [Diagnostics] Add declaration name to macro_attached_to_invalid_decl
This could also use `%kind` for the declaration kind, but that ends up
being "extension of struct" instead of just "extension", which seems
slightly worse.
2023-07-25 16:03:52 -07:00
Ben Barham
18cb5f4f3d [Diagnostics] Update in_macro_expansion note for attached macros
Use the attached atttribute's location as the location of the macro,
rather than the location of the declaration it's attached to. Also add
the kind and name of that declaration to the note itself.
2023-07-25 16:03:52 -07:00
Holly Borla
a60b302754 [Macros] Diagnose invalid attached macros at the attribute location instead
of the declaration.
2023-07-21 16:31:10 -07:00
Holly Borla
3c24932138 [Macros] Diagnose undocumented conformances in extension macro expansions. 2023-07-18 18:06:51 -07:00
Holly Borla
04ea8b0a67 [Macros] Diagnose macros attached to declarations they cannot apply to. 2023-07-13 17:07:24 -07:00
Holly Borla
05c3ffc749 [Macros] Validate that extension macro expansions only include names covered
by the macro declaration.
2023-07-13 06:59:18 -07:00
Holly Borla
0bd898eb12 [Macros] Allow extension macros to suppress conformances that are already
stated in the original source.

If an extension macro can introduce protocol conformances, macro expansion
will check which of those protocols already have a stated conformance in the
original source. The protocols that don't will be passed as arguments to
extension macro expansion, indicating to the macro that it should only add
conformances to those protocols.
2023-06-30 16:01:15 -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
c867c7c917 [Macros] Allow extension macros to specify conformances in the attached
attribute.
2023-06-28 00:42:23 -07:00
Holly Borla
095542ae96 [Macros] Ban extension macros attached to local types. 2023-06-27 21:42:43 -07:00
Holly Borla
d3e2562cd9 [Macros] Pass a qualified extended type to extension macro expansion. 2023-06-27 21:22:12 -07:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -07:00