Commit Graph

129 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
a19bc238d6 [Legacy parser] No freestanding macros in @abi (macro tests)
Additional tests for the previous commit “[Legacy parser] No freestanding macros in `@abi`”.
2025-05-05 13:50:16 -07:00
Hamish Knight
b8ab2c048b [ASTScope] Match parent scopes in lookupCatchNode for brace stmts
Rather than looking for a given BraceStmtScope child for a particular
catch node, check whether the given catch node is the parent of the
innermost BraceStmtScope we've found, looking through an intermediate
source file if needed. This ensures it works correctly when the
BraceStmtScope is in a macro expansion.

rdar://149036108
2025-04-19 21:11:22 +01:00
Becca Royal-Gordon
9d63cf84c7 Tweak redecl checking’s handling of generated code
Macro expansions are now treated like a part of the source file they belong to, for purposes of the “second declaration is the one that’s diagnosed” rule. This helps stabilize a behavior that was easy to perturb.
2025-04-18 14:44:24 -07:00
Hamish Knight
b6a09d1b33 Reapply: [CS] Make sure macro arguments go through coerceCallArguments
Previously we would avoid rewriting the arguments in CSApply, but
that can result in incorrect behavior in MiscDiagnostics passes, e.g
incorrectly treating all closure arguments as escaping. Make sure
we rewrite the arguments as we would in regular type-checking.

rdar://148665502
2025-04-16 19:22:52 +01:00
Hamish Knight
475e02a381 Revert "[CS] Make sure macro arguments go through coerceCallArguments" 2025-04-15 18:44:14 +01:00
Hamish Knight
5674b6e047 Merge pull request #80583 from hamishknight/macro-arg-apply
[CS] Make sure macro arguments go through `coerceCallArguments`
2025-04-08 11:51:32 +01:00
Hamish Knight
cc23c25979 Merge pull request #80535 from hamishknight/cap-req
[Sema] Requestify PatternBindingDecl capture computation
2025-04-07 17:28:18 +01:00
Hamish Knight
b82f95b508 [CS] Make sure macro arguments go through coerceCallArguments
Previously we would avoid rewriting the arguments in CSApply, but
that can result in incorrect behavior in MiscDiagnostics passes, e.g
incorrectly treating all closure arguments as escaping. Make sure
we rewrite the arguments as we would in regular type-checking.

rdar://148665502
2025-04-07 16:29:50 +01:00
Hamish Knight
da1d37de26 [Mangler] Avoid mangling local discriminator for attached macros
If we're using the macro-specific local discriminator, we need to
make sure we avoid mangling the regular local discriminator in
`appendDeclName`, since that could prematurely kick local discriminator
assignment before type-checking has finished.

rdar://143834482
2025-04-04 19:41:04 +01:00
Hamish Knight
4676eaa110 [Sema] Requestify PatternBindingDecl capture computation
Introduce `PatternBindingCaptureInfoRequest`, and kick it after
contextualizing a property initializer. This ensures it gets run
for stored properties added by macro expansions.

rdar://143429551
2025-04-04 16:01:39 +01:00
Doug Gregor
65a9bff5b0 Rework mangling of macro expansions in local contexts to not trigger type checking.
The mangling of macro expansions relies on having a type-checked AST
for its enclosing context. When that enclosing context is within a
local context (say, a local type), mangling would trigger type
checking of that local type, which could then involve assigning local
discriminators. However, if this happens before type checking of the
enclosing function body, we would end up failing to assign closure
discriminators to (e.g.) autoclosures within the body.

The fundamental problem here is the interaction between discriminator
assignment (which can only happen after type checking) and mangling of
macro expansion buffers (which can happen during that type checking).
Break this cycle by providing a different approach to mangling macro
expansions within local contexts as the innermost non-local context +
a name-based discriminator within that local context. These manglings
are not ABI and are not stable, so we can adjust them later if we come
up with a scheme we like better. However, by breaking this cycle, we
eliminate assertions and miscompiles that come from missing
discriminators in this case.

Fixes rdar://139734958.
2024-12-05 16:09:02 -08:00
Doug Gregor
7ea1599d20 Add missing availability annotation to test 2024-07-03 07:08:36 -07:00
Doug Gregor
7e4acfaa02 [Macros] Put the insertion location for freestanding macros at the beginning
When building the ASTScope tree, we set the insertion location for the
macro expansion just before the end of the macro use. This is the right
approach for attached macros, because we need to put the entities
after the macro.

However, for freestanding macros, which can have trailing closures,
this location is a problem, because the scope for the macro
expansion ends up inside the scope for the trailing closure. Use the
start location of the freestanding macro instead.

Fixes rdar://130923190.
2024-07-02 20:47:40 -07:00
Daniel Rodríguez Troitiño
39b056375b [test] Fix test for cases where TMPDIR shares a prefix with CWD (#74741)
There is code in `IRGenDebugInfo.cpp` ([1]) that tries to move the
common part of the `filename` into the `directory`. If this test is
build with a temporary directory that shares a prefix with the current
working directory, the common part of the path will be moved into
`directory`, but since the test checks for an empty directory, it will
fail the test, even if the results are correct. This situation is not
uncommon in CI setups where each job might have an isolated temporary
directory sharing a prefix with the current working directory (example:
`/ci/build/XXX/tmp` and `/ci/build/XXX/cwd`).

Modify the regex to allow anything in the `directory` value, since it is
not really important for the test to prove that the debug information is
generated.`

[1]: 9c4232c89d/lib/IRGen/IRGenDebugInfo.cpp (L564-L585)
2024-06-26 14:04:48 -07:00
Rintaro Ishizaki
5ebd715a2a Merge pull request #73725 from rintaro/macros-inproc-plugin
[Macros] In-process plugin server
2024-06-26 06:33:38 -07:00
Adrian Prantl
c9f5af5617 Fix a regression that caused the compiler to drop the path names of generated
macro expansion source code when emitting DWARF 4 or earlier.

rdar://126688061
2024-06-21 18:58:11 -07:00
Rintaro Ishizaki
2f7aa428db [Macros] In-process plugin server
Separate swift-syntax libs for the compiler and for the library plugins.
Compiler communicates with library plugins using serialized messages
just like executable plugins.

* `lib/swift/host/compiler/lib_Compiler*.dylib`(`lib/CompilerSwiftSyntax`):
  swift-syntax libraries for compiler. Library evolution is disabled.
* Compiler (`ASTGen` and `swiftIDEUtilsBridging`) only depends on
  `lib/swift/host/compiler` libraries.
* `SwiftInProcPluginServer`: In-process plugin server shared library.
  This has one `swift_inproc_plugins_handle_message` entry point that
  receives a message and return the response.
* In the compiler
  * Add `-in-process-plugin-server-path` front-end option, which specifies
    the `SwiftInProcPluginServer` shared library path.
  * Remove `LoadedLibraryPlugin`, because all library plugins are managed
    by `SwiftInProcPluginServer`
  * Introduce abstract `CompilerPlugin` class that has 2 subclasses:
    * `LoadedExecutablePlugin` existing class that represents an
      executable plugin
    * `InProcessPlugins` wraps `dlopen`ed `SwiftInProcPluginServer`
  * Unified the code path in `TypeCheckMacros.cpp` and `ASTGen`, the
    difference between executable plugins and library plugins are now
    abstracted by `CompilerPlugin`
2024-06-17 11:36:52 -07:00
Slava Pestov
9793f77daa AST: New mangling for expansion locations to avoid request cycles
Fixes rdar://127078338.
2024-06-13 17:45:30 -04:00
Rintaro Ishizaki
8a8c96bd66 [Tests] Update for "swift" diagnostic style change
SwiftDiagnostics now emits ASCII characters instead of Unicode line
characters.
2024-04-01 17:09:23 -07:00
Adrian Prantl
1e50be370d Emit original-source-range comment in inline macro expansion files.
rdar://123117522
2024-02-27 15:30:24 -08:00
Doug Gregor
be2466c72d Updates for new freestanding macro discriminators 2024-01-11 08:19:15 -08:00
Doug Gregor
41b0104c94 Macro expansion scopes are inserted at the last character in their insertion range
Macro expansion scope ranges are in terms of characters, so the end of the
insertion range is one past the last character of the insertion range. If
the last character in the insertion range is also the last character in
the enclosing scope, we would build the scope tree incorrectly.

Adjust the position for scope creation to the last character in the
insertion range, not one past it. Fixes rdar://120559184.
2024-01-05 14:35:20 -08:00
Adrian Prantl
8626aeff03 [Debug Info] Store the source code for macro expansions in the debug info
This patch stores the source code of macro in the source field of DIFile, which
can be used together with the DW_LLVM_LNCT_source extension to emit the source
code into DWARF debug info.

rdar://110926109
2023-11-30 14:49:57 -08:00
Rintaro Ishizaki
444fcfabba [Macros] Tweak macro resolution error messages 2023-10-13 10:38:08 -07:00
Rintaro Ishizaki
bdd4c005e5 [Macros] Diagnose how ExternalMacroDefinition request was failed
Return the failure reason as the result.
2023-10-12 16:08:48 -07:00
Holly Borla
1f07cbae2a [Macros] Diagnose attached and freestanding declaration macros that produce
something other than a declaration.

The validation code already diagnosed all sorts of invalid declarations, but
it was ignoring AST nodes that aren't declarations at all.
2023-10-09 22:50:12 -07:00
Doug Gregor
06bcd0641f Fix handling of anonymous closure arguments within macro expansions
We were incorrectly emitting a nonsensical error.

Fixes rdar://114340542.
2023-09-12 15:57:03 -07:00
Doug Gregor
9c7639d9ee Merge pull request #68289 from DougGregor/swiftsyntax-diags-cleanup 2023-09-05 06:44:04 -07:00
Doug Gregor
9bf512adf0 [Diagnostics] Remove the C++ formatter for the "Swift" diagnostic style
We'll be using the new swift-syntax diagnostic formatter in the near
future, as it is nearly available on all host platforms. So, remove
the C++ formatter that did source-line annotation, falling back to the
"LLVM" style when swift-syntax is not compiled in.
2023-09-04 22:22:17 -07:00
Saleem Abdulrasool
c549028f88 test: sink -no-toolchain-stdlib-rpath into lit
This is a platform specific option and should be handled by lit patterns
rather than encoded into the tests.
2023-09-04 09:07:27 -07:00
Doug Gregor
1c5dcb51fa Merge pull request #68154 from DougGregor/remark-macro-loading 2023-08-25 19:19:20 -07:00
Doug Gregor
339d31fabb [Macros] Add a frontend flag -Rmacro-loading to remark on macro resolution
Macro implementations can come from various locations associated with
different search paths. Add a frontend flag `-Rmacro-loading` to emit
a remark when each macro implementation module is resolved, providing
the kind of macro (shared library, executable, shared library loaded
via the plugin server) and appropriate paths. This allows one to tell
from the build load which macros are used.

Addresses rdar://110780311.
2023-08-25 15:09:49 -07:00
Doug Gregor
8371d3b517 [Macros] Ensure that we find operators in any macro-expanded context.
In any nominal declaration or extension thereof that is produced by a
macro expansion, make sure we perform qualified name lookup when
resolving operators so that we're guaranteed to find the
macro-introduced operators. Otherwise, expanding a macro that defines
a new type with conformances involving operators doesn't work.

Fixes rdar://114257019,
2023-08-23 04:54:56 -07:00
Doug Gregor
0a1fbe7f30 [Macros] Emit deprecation warnings for uses of freestanding macros.
Fixes rdar://113138432.
2023-08-02 22:58:11 -07:00
Adrian Prantl
1f6091aa45 Merge pull request #66744 from asavonic/debuginfo-fragment-distinct-inline-loc
[DebugInfo] Emit distinct DILocation for different inline instances
2023-07-28 08:45:53 -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
Doug Gregor
939376b61e [Macros] Enable freestanding macros at module scope in script mode
Eliminate the error message

    error: global freestanding macros not yet supported in script mode

by implementing name lookup, type checking, and code emission for
freestanding macros. The key problem here is that, in script mode,
it is ambiguous whether a use of a freestanding macro is an expression
or a declaration. We parse as an expression (as we do within a
function body), which then gets wrapped in a top-level code
declaration.

Teach various parts of the compiler to look through a top-level code
declaration wrapping a macro expansion expression that is for a
declaration or code-item macro, e.g., by recording these for global
name lookup and treating their expansions as "auxiliary" declarations.

Fixes rdar://109699501.
2023-07-03 21:00:51 -07:00
Doug Gregor
6fca08acf7 [Macros] Cope with local types and opaque result types in macros and expansions
Address a few related issues that affect local types and opaque result types within macros:
* Don't add local types or opaque types encountered while parsing the
arguments of a freestanding macro to the global list. When we do add
them, make sure we're adding them to the outermost source file so
they'll get seen later. This avoids trying to generate code for these
types, because they aren't supposed to be part of the program. Note
that a similar problem remains for arguments to attached macros, which
will need to be addressed with a more significant refactoring.
* When determining whether opaque types should be substituted within a
resilience domain, check the outermost source files rather than the exact
source file, otherwise we will end up with a mismatch in
argument-passing conventions.
* When delaying the type checking of functions that occur as part of a
macro expansion, make sure we record them in the outermost Swift source
file. Otherwise, we won't come back to them.

There is a common theme here of using AST state on the source file in
a manner that isn't ideal, and starts to break down with macros. In
these cases, we're relying on side effects from earlier phases
(parsing and type checking) to inform later phases, rather than
properly expressing the dependencies through requests.

Fixes rdar://110674997&110713264.
2023-07-03 15:09:54 -07:00
Andrew Savonichev
ca6e74226d [DebugInfo] Emit distinct DILocation for different inline instances
LLVM seems to determine a variable instance as a combination of DILocalVariable
and DILocation. Therefore if multiple llvm.dbg.declare have the same
variable/location parameters, they are considered to be referencing the same
instance of variable.

Swift IRGen emits a set of llvm.dbg.declare calls for every variable
instance (with unique SILDebugScope), so it is important that these calls have
distinct variable/location parameters. Otherwise their DIExpression may be
incorrect when treated as referencing the same variable. For example, if they
have a DIExpression with fragments, we will see this as multiple declarations of
the same fragment. LLVM detects this and crashes with assertion failure:

DwarfExpression.cpp:679: void llvm::DwarfExpression::addFragmentOffset(const
llvm::DIExpression *): Assertion `FragmentOffset >= OffsetInBits &&
"overlapping or duplicate fragments"' failed.

The patch resolves #55703. The LIT test (debug_scope_distinct.swift) is the
reproducer from that issue.
2023-06-29 17:06:24 +09:00
Doug Gregor
3c04cff8dd [Macros] Provide the freestanding macro role for expansion operations.
The compiler knows (from a macro declaration) what freestanding macro
role a macro implementation is expected to implement. Pass that through
to the macro expansion code itself, rather than guessing based on the
protocol conformances of the implementation type. We already use this
approach with attached macros, so this is more of the same.

Eliminates a crash and improves diagnostics when the freestanding macro
role and its implementation are out of sync, fixing rdar://110418969.
2023-06-09 12:59:56 -07:00
Doug Gregor
e964d1290f [Parser] Add local type declarations to the outermost enclosing source file
The parser is currently responsible for adding local type declarations
to a `SourceFile`, which IR generation later queries. However, IRGen
never sees the source files associated with macro expansion buffers,
so local types introduced there don't get recorded.

In time, this approach of using the parser to record semantic
information should be replaced with something more "pull" oriented.
For now, however, record local type declarations in the outermost
enclosing source file... so we see the ones produced by macro
expansions, too.

Fixes rdar://109370309.
2023-06-04 21:54:25 -07:00
Doug Gregor
b7b6a1db72 Ensure that macro-provided operators can be found by witness lookup
When looking for an operator to satisfy a protocol requirement, we
currently depend on global operator lookup for everything except local
types. However, macro-provided operators aren't found by global
operator lookup, so perform a member lookup for such cases in addition
to global operator lookup. This makes macro-provided operators visible
through protocol requirements they witness.
2023-06-03 23:19:24 -07:00
Doug Gregor
343947a532 [Module-scope lookup] Don't expand macros associated with members of types
The module-scope lookup tables use the same code for adding
module-scope declarations as for adding member operators, which are
found via "global" operator lookup. This requires us to expand macros
that can produce members of types, which violates the outside-in
expansion rule described in the proposals.

Stop recording member-producing macros, whether they are peer macros
applied to member declarations or are freestanding declaration macros
within a member context. This re-establishes the outside-in expansion
rule. It also means that member operators introduced by macro
expansion won't be found by global operator lookup, which is a
(necessary) semantic change.
2023-06-03 23:06:29 -07:00
Doug Gregor
88dd6fc7c5 [Macros] Introduce an example where member operators aren't found
Operators introduced by member macros are not getting found by global
operator lookup. This is technically a bug, but due to the potential
cost of having to expand macros on/in types just to find operators,
for now we're just going to document here that it doesn't work and
reconsider the semantics.
2023-06-03 00:07:04 -07:00
Doug Gregor
ebe0b63c5e [Module-scope lookup] Only inject macro-introduced operators at module scope
Well, this is fun. Due to the use of the module-scope lookup table to
find operators, we need to carefully filter out any macro-introduced
declarations that *aren't* operators when forming the module-scope
lookup table. Otherwise, we can find macro-introduced static entities
within types... from completely unrelated scopes.

Fixes rdar://109219036.
2023-06-02 23:55:55 -07:00
Doug Gregor
19ca63ac3a [Test] Add test for already-fixed macro example.
This used to cause a circular reference and crash. No more, but make
sure we don't regress it. From rdar://109280926.
2023-06-02 23:11:12 -07:00
Doug Gregor
e6c5871df3 [Test] Add test for crash while emitting reference dependencies.
This bug was fixed as part of the rework of our handling of
closure/local discriminators with respect to macros, but add a test
case to ensure that we don't regress. Tracked by rdar://108300632.
2023-06-02 15:01:19 -07:00
Kim de Vos
8140efb616 [SwiftSyntax] Remove requiresLeadingNewline from basic format 2023-05-30 09:53:33 +02:00
Doug Gregor
638c2cd11a [Walker] Don't visit VarDecls encountered in freestanding macro expansions
VarDecls are always walked via their pattern bindings, so we end up
with double-visitation if we also visit them here.

Fixes rdar://109376102.
2023-05-28 23:35:23 -07:00