Commit Graph

44 Commits

Author SHA1 Message Date
Becca Royal-Gordon
01431b87b2 Make @abi non-experimental
This includes changing the feature name so that compilers with the experimental feature don’t accidentally pick up content that only works in the final version.

Resolves rdar://150065196.
2025-05-05 13:50:51 -07:00
Becca Royal-Gordon
093c2f7216 Diagnose CustomAttrs as needed in @abi (macro tests)
Additional tests for the previous commit “Diagnose CustomAttrs as needed in `@abi`”.
2025-05-05 13:50:16 -07:00
Doug Gregor
d004d24560 Ensure that we wire up *all* custom attribute initializer contexts
Thank you again, Rintaro
2024-12-06 23:02:25 -08:00
Doug Gregor
ee9c066050 Introduce a new Initializer subclass for the arguments of custom attributes
Since the introduction of custom attributes (as part of property
wrappers), we've modeled the context of expressions within these
attributes as PatternBindingInitializers. These
PatternBindingInitializers would get wired in to the variable
declarations they apply to, establishing the appropriate declaration
context hierarchy. This worked because property wrappers only every
applied to---you guessed it!---properties, so the
PatternBindingInitializer would always get filled in.

When custom attributes were extended to apply to anything for the
purposes of macros, the use of PatternBindingInitializer became less
appropriate. Specifically, the binding declaration would never get
filled in (it's always NULL), so any place in the compiler that
accesses the binding might have to deal with it being NULL, which is a
new requirement. Few did, crashes ensued.

Rather than continue to play whack-a-mole with the abused
PatternBindingInitializer, introduce a new CustomAttributeInitializer
to model the context of custom attribute arguments. When the
attributes are assigned to a declaration that has a
PatternBindingInitializer, we reparent this new initializer to the
PatternBindingInitializer. This helps separate out the logic for
custom attributes vs. actual initializers.

Fixes https://github.com/swiftlang/swift/issues/76409 / rdar://136997841
2024-12-06 17:40:32 -08:00
Doug Gregor
bcf2371d31 Insert member-attribute macros right before modifiers
The insertion position of member-attribute macros was right before the
first attribute. However, ASTScope would visit the as-written attributes
first, so the scope tree entries would be out of order, causing name
lookup to fail.

Move the insertion position to where a new modifier would go, i.e.,
after the explicitly-written attributes. Fixes rdar://120496864.
2024-01-05 18:02:03 -08:00
Allan Shortlidge
2730c19891 Macros: Stop explicitly enabling accepted ExtensionMacros feature in tests.
NFC.
2023-12-10 10:10:15 -08:00
Zhiyu Zhu/朱智语
5767e8f6d1 [Macros] Support module-qualified attached macro lookup (#69457)
Allow attached macro expansion syntax to have a module qualifier, `@Foo.Bar`.

rdar: //108621205
2023-12-08 15:57:51 -08:00
Doug Gregor
c3af8cbe42 [Macros] Diagnose availability of attached macros 2023-08-02 23:23:38 -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
Holly Borla
04ea8b0a67 [Macros] Diagnose macros attached to declarations they cannot apply to. 2023-07-13 17:07:24 -07:00
Holly Borla
b7a53f9303 [Macros] Handle the case where an extension macro conformance has a null
type repr because it was deserialized.
2023-07-07 17:16:03 -07:00
Doug Gregor
e89fdd3084 Requestify the computation of the list of memberwise initialized properties.
Fixes rdar://110776763, a case where initialized stored properties
introduced by peer macros weren't working alongside declared stored
properties.
2023-06-23 11:34:52 -07:00
Doug Gregor
e306c11a22 [Macros] Ensure that we visit *all* members when emitting memberwise initializer
Fixes rdar://111122261.
2023-06-21 16:12:11 -07:00
Holly Borla
98249543cd [Macros] Ban freestanding and peer macro expansions that can produce arbitrary
names at global scope.

Freestanding and peer macros applied at top-level scope cannot introduce
arbitrary names. Introducing arbitrary names means that any lookup
into this scope must expand the macro. This is a problem, because
resolving the macro can invoke type checking other declarations, e.g.
anything that the macro arguments depend on. If _anything_ the macro
depends on performs name unqualified name lookup, e.g. type resolution,
we'll get circularity errors. It's better to prevent this by banning
these macros at global scope if any of the macro candidates introduce
arbitrary names.
2023-06-11 23:10:41 -07:00
Richard Wei
9c91d1b705 [Macros] Fix lookup of macro-produced variables (#65939)
Calling `getInnermostDeclContext()->getParentSourceFile()` on a macro-produced decl does not seem to be a reliable way to obtain the macro expansion source file, because `PatternBindingDecl` is not a `DeclContext` and `getInnermostDeclContext()` falls back outside the macro expansion file. This patch switches to using `getSourceFileContainingLocation` when possible.

Resolves rdar://109376568.
2023-05-18 15:32:21 -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
Doug Gregor
1f6b3f6cb2 Only declarations that are members of a type or extension can have member attributes
The last step in breaking the cycle through visible declaration lookup,
e.g., with invalid code. Prior to this, we would get a false cyclic
reference through the parameters of a function when they try to find
property wrappers on the parameters.
2023-04-24 21:25:11 -07:00
Doug Gregor
35b784c432 [Macros] Eliminate overaggressive cycle breaking in getSemanticAttrs()
This cycle break means that some attributes introduced by
member-attribute macros aren't getting processed. I'm still reducing a
test case while I look for a fix, but for now I'm reverting this
change to address a regression for macros.

Fixes rdar://108456284.
2023-04-24 10:08:08 -07:00
Doug Gregor
ab682f1658 [Macros] Break reference cycles involving visible name lookup 2023-04-16 13:31:42 -04: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
Doug Gregor
d790477e17 Merge branch 'main' into name-lookup-filtering-expanded-top-level 2023-04-05 22:21:29 -07:00
Doug Gregor
8e63bf1bc4 [Module-scope lookup] Only add macro-expanded names that match.
When we look into expanded macros to find declarations for module-scope
name lookup, make sure that we only return declarations whose name
matches the requested name.

We were effectively spamming the results with unrelated names, and
most clients trust the results of this lookup (vs. filtering them
further), so they would get confused. The particular failure for this
test is that type reconstruction would fail because we were looking
for one unique name, but we found many non-matching names, and type
reconstruction bails out rather than try to filter further.
2023-04-05 21:31:17 -07:00
Rintaro Ishizaki
6018c81fb3 Merge pull request #64946 from rintaro/macros-test-codesign-rdar107658689
[Tests/Macros] Add %target-codesign before %target-run
2023-04-05 15:53:22 -07:00
Rintaro Ishizaki
02a604e7e7 [Tests/Macros] Add %target-codesign before %target-run
rdar://107658689
2023-04-05 12:48:09 -07:00
Rintaro Ishizaki
655e30b1c7 [Tests] Disable 2 tests in use_os_stdlib
rdar://107658865
rdar://107670464
2023-04-05 12:12:36 -07:00
Doug Gregor
950c5bb52f Merge pull request #64915 from DougGregor/visit-aux-decls-of-class-members-twice 2023-04-04 15:41:23 -07:00
Doug Gregor
b842d01b9a [Macros] Don't visit auxiliary decls of class members twice.
To ensure that class vtables get laid out with the right ABI, visiting
a class declaration visits all of its "ABI members", which includes
visiting all of the auxiliary declarations. However, we also visit the
auxiliary declarations of every declaration, which means some
declarations get visited twice.

This led to another oddly-specific bug for macros, where we would
visit declarations introduce by peer macros on class members twice.
When combined with function-body-skipping (e.g., to emit a module
file), this would result in spurious errors of the form:

    Expected '{' in body of function declaration

Stop visiting the auxiliary declarations of class members twice,
eliminating this error. This one has been annoying me off and on for a
while :).

Fixes rdar://107429169.
2023-04-04 11:48:47 -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
Rintaro Ishizaki
f56eb8e47b [Test] Add missing REQUIRES: executable_test
rdar://107591637
2023-04-03 20:24:20 -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
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
467f8f5e0f [Macros] Ensure that we can find peer macros for members of extensions.
When populating the name lookup tables for a nominal type, we were only
visiting declarations with peer macros within the nominal type
declaration itself. Also visit the members of extensions of the
nominal type.

Without this change, peer-macro-defined members of extensions are not
visible.
2023-03-31 23:05:18 -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
Doug Gregor
88b76b2f88 Make sure we visit auxiliary declarations for all SIL symbol visitation
Addresses IRGen, TBD generation, and so on
2023-03-05 23:55:36 -08:00
Richard Wei
98c2a837d2 [Macros] Always visit macro-produced decls as auxiliary decls
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.
2023-03-04 23:48:21 -08:00
Holly Borla
be558d99d0 [Macros] Implement unqualified lookup for global macro-expanded peer declarations. 2023-03-03 14:02:04 -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
Holly Borla
46db62b5b5 [Macros] Pass the attached macro mangling to ASTGen to use as the discriminator
when creating the macro expansion context.
2023-02-14 16:24:27 -08:00
Holly Borla
f4b2b60446 [Macros] Enable global peer macros.
Global peer macro expansions are not injected into the AST. Instead, they
are visited as "auxiliary declarations" when needed, such as in the decl
checker and during SILGen. This is the same mechanism used for local property
wrappers and local lazy variables.
2023-02-14 16:24:25 -08:00
Holly Borla
56a9e17863 [Macros] Expand peer macros. 2023-02-10 14:43:17 -08:00
Holly Borla
f04f512184 [Macros] Add a new macro role for attached peer macros. 2023-02-10 14:38:22 -08:00