Commit Graph

889 Commits

Author SHA1 Message Date
Kuba Mracek
25eb997a28 [embedded] Add basics of module serialization, importing and validation in embedded Swift.
- Add a flag to the serialized module (IsEmbeddedSwiftModule)
- Check on import that the mode matches (don't allow importing non-embedded module in embedded mode and vice versa)
- Drop TBD support, it's not expected to work in embedded Swift for now
- Drop auto-linking backdeploy libraries, it's not expected to backdeploy embedded Swift for now
- Drop prespecializations, not expected to work in embedded Swift for now
- Use CMO to serialize everything when emitting an embedded Swift module
- Change SILLinker to deserialize/import everything when importing an embedded Swift module
- Add an IR test for importing modules
- Add a deserialization validation test
2023-09-06 20:06:36 -07:00
zachary0kent
25f078adb5 Merge branch 'main' into lazy-immediate 2023-08-16 19:45:27 -07:00
Zak Kent
831cfb575d [Sema] [NFC] Don't accumulate lazily typechecked functions in lazy immediate mode 2023-08-16 19:30:31 -07:00
Pavel Yaskevich
b21e8426a0 [AST] NFC: Remove @runtimeMetadata related code 2023-08-15 12:16:40 -07:00
Slava Pestov
c26cafc47a AST: Remove bogus PackExpansionType logic in lookupConformance() 2023-08-09 18:28:22 -04:00
Slava Pestov
f219274e9b AST: Remove generic signature and conditional requirements from BuiltinProtocolConformance 2023-08-09 17:42:57 -04:00
Slava Pestov
9f51ea1de6 AST: Remove old tuple conformance logic from getBuiltinTupleTypeConformance() 2023-08-09 17:42:25 -04:00
Slava Pestov
e6d611cda3 AST: Pass allowMissing=true from getPackTypeConformance() 2023-08-09 17:42:25 -04:00
Allan Shortlidge
c3af951f38 AST: Get SPI status of PatternBindingDecl from anchoring VarDecl.
For property declarations, the `@_spi` attribute is attached to the VarDecl AST
node, rather than the PatternBindingDecl AST node, so the `isSPI()` query
should take this into account. Failing to do so caused the availability checker
to erroneously require that `@_spi` properties of types in `-library-level api`
libraries have availability annotations.

Resolves rdar://113587321.
2023-08-08 16:06:36 -07:00
Alexis Laferrière
69493d1f4a [Sema] @_private imports brings is all SPI of the imported module
Within one module, SPI decls are always visible. Conceptually we want
the same behavior for `@_private` imports where the client pretends it's
part of the same module.

rdar://81240984
2023-08-04 10:30:36 -07:00
Sophia Poirier
974081c065 [Variadic Generics] abstract tuple of Sendable elements does not conform to Sendable 2023-07-19 12:35:01 -07:00
Doug Gregor
8df508c453 Merge pull request #67105 from DougGregor/freestanding-macro-global-scope-in-scripts 2023-07-04 12:17:47 -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
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
Slava Pestov
5ce14265e4 Fix some random compiler warnings 2023-06-29 16:29:10 -04:00
Holly Borla
7a46466b65 Merge pull request #66967 from hborla/extension-macro
[Macros] Generalize `conformance` macros as `extension` macros
2023-06-29 08:57:04 -07:00
Erik Eckstein
6b1697eb06 use new llvm::Optional APIs to fix deprecation warnings 2023-06-28 14:28:38 +02:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Holly Borla
da8e84caac Merge pull request #66914 from hborla/conformance-macro-in-extension
[Macros] Don't visit macro-generated extensions in `visitAuxiliaryDecls`.
2023-06-26 15:45:03 -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
Rintaro Ishizaki
096d8ea142 [CodeCompletion] Suggest synthesized declarations from macros
* Don't invalidate the lookup cache in 'getOrCreateSynthesizedFile()'
  Adding a synthesized file itself doesn't introduce any decls. Instead,
  we should invalidate the right after the actual declrations are added
  in the file
* Remove 'SourceLookupCache::invalidate()' method. It was just used
  right before the destruction. It was just not necessary
* Include auxiliary decls in 'SourceLookupCache::lookupVisibleDecls()'
  Previously, global symbol completion didn't include decls synthesized
  by peer macros or freestanding decl macros
* Include "auxiliary decls" in visible member lookup, and visible local
  decl lookup
* Hide macro unique names

rdar://110535113
2023-06-22 13:18:46 -07:00
Ben Barham
ee3a47b624 [Frontend] Ignore adjacent swiftmodule in compiler host modules
`lib/swift/host` contains modules/libraries that are built by the host
compiler. Their `.swiftmodule` will never be able to be read, ignore
them entirely.
2023-06-12 16:53:48 -07:00
Alex Lorenz
ba8d4d7801 [cxx-interop] compilations that do not enable C++ interoperability should not be able to import modules that do enable C++ interoperability by default
A supplemental hidden frontend option allows advanced users to opt-out of this requirement.

Fixes https://github.com/apple/swift/issues/65833
Fixes https://github.com/apple/swift/issues/65832
2023-06-09 15:38:16 -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
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
Ellie Shin
32f53d1eb8 Add implicit SPI import feature
API development sometimes requires a redesign while supporting early
adopters. Currently this is done by adding @_spi(name) to the API but
that requires adding the attribute in import statements as well, causing
manual overhead of adding and then removing when the redesign is done.

This PR introduces a special spi group name '_' and allows an implicit
spi import of a module containing API attributed with '@_spi(_)'

Resolves rdar://109797632
2023-06-01 15:33:04 -07:00
Hamish Knight
58d6694783 Requestify ExportedSourceFile parsing
Avoid parsing the syntax tree up-front, and instead
only parse it when required, which happens when either:

1. ASTGen parsing is enabled (currently disabled
   by default)
2. Round trip checking is enabled for a primary
   file (enabled by default in a debug build,
   except when dep scanning or doing an IDE
   operation)
3. We need to evaluate a macro in that file

This change therefore means that we now no longer
need to parse the syntax tree for secondary files
by default unless we specifically need to evaluate
a macro in them (e.g if we need to lookup a member
on a decl with an attached macro). And the same
for primaries in release builds.

rdar://109283847
2023-05-22 14:55:19 +01:00
Kavon Farvardin
103bd043cd Revert "Revert "Prevent noncopyable metatypes from being converted to Any""
This reverts commit 1a3b42cd63.
2023-05-04 11:38:00 -07:00
Hamish Knight
1a3b42cd63 Revert "Prevent noncopyable metatypes from being converted to Any" 2023-05-04 12:10:10 +01:00
Kavon Farvardin
1df3912794 Merge pull request #65606 from kavon/noncopyable-metatypes
Prevent noncopyable metatypes from being converted to `Any`
2023-05-03 22:05:17 -07:00
Kavon Farvardin
4710fc675b Prevent noncopyable metatypes from being converted to Any
These metatypes are a gateway to more incorrect
uses of these noncopyable values because we don't
yet have the corresponding runtime support yet.
The other use cases of using metatypes of
noncopyable types in generics is not high enough to
warrant people using them yet.

resolves rdar://106452518
2023-05-03 10:23:11 -07:00
Doug Gregor
7c4547ff82 Only synthesize a private discriminator when needed 2023-04-27 16:15:26 -07:00
Doug Gregor
0eb095b32c [Macros] Use private discriminators for expression macros
Expression macros ascribed to non-private contexts need private
discriminators so they don't conflict with other uses of the same macro
in other source files.

Thank you, Richard, for noticing this omission!
2023-04-27 14:30:17 -07:00
Hamish Knight
c1843479f1 Drop SerializedOK parameter from getRawComment
It doesn't seem like there's any client that's
actually taking advantage of setting it to `false`,
and its default value of `false` is more likely
than not going to cause clients to accidentally
miss comments that they may want. In fact, this
was exactly the case for code completion's brief
field. Finally, the parameter wasn't even
consistently applied, as we would attempt to
deserialize swiftdoc comments even if it were
`false`.
2023-04-26 12:38:37 +01: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
Holly Borla
b958e43528 [Macros] Remove ModuleDecl::isInGeneratedBuffer.
This method was misleading. The majority of callers (all but one!) don't want
to unconditionally treat all locations in any macro expansion buffer the
same way, because the code also must handle nested macro expansions. There
is one part of SourceKit (and possibly others) that really do want to ignore
all macro expansions, but those can be handled within SourceKit / IDE code,
because I don't believe this utility is useful in the frontend.
2023-04-19 21:28:09 -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
Doug Gregor
bbe4ff1e56 [Module-scope lookup] Find arbitrary names in macro expansions.
Whenever we perform a name lookup, we need to make sure to expand
all macros that use `names: arbitrary`, because of course they can
produce... wait for it... arbitrary names, and we don't know what they
are until we instatiate them.
2023-04-06 16:04:43 -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
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
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
b9810fb0a3 [SourceKit] Return the original location for decls within generated code
Pass back the original location (ie. where the macro was expanded, not
the location that the generated code would be inserted) for cursor info
and indexing. Also mark any declarations/references within generated
source as implicit.

Resolves rdar://107209132.
2023-03-30 11:20:33 -07:00
Ben Barham
8e6c9962d6 [AST] Do not copy SearchPathOptions in updateNonUserModule
`updateNonUserModule` was accidentally copying `SearchPathOptions`. Take
a reference to it instead. Also, since `addFile` is actually called many
times (once for every submodule, of which there are many), change
`isNonUserModule` to a request so that it's only calculated when needed.

Resolves rdar://107155587.
2023-03-24 17:05:57 -07:00
Alexis Laferrière
cc433261ba Merge pull request #64522 from xymus/access-level-import-testable
[Serialization] Load non-public dependencies of modules when testing is enabled
2023-03-22 12:54:39 -07:00
QuietMisdreavus
f3ad288a9a don't assert when a module re-exports a module and one of its symbols (#64479)
rdar://106807038
2023-03-22 09:39:44 -06:00
Alexis Laferrière
599346885e [Serialization] Differentiate module loading behavior for non-public imports
Differentiate `internal` and `fileprivate` imports from
implementation-only imports at the module-wide level to offer a
different module loading strategy. The main difference is for non-public
imports from a module with testing enabled to be loaded by transitive
clients.

Ideally, we would only load transitive non-public dependencies on
testable imports of the middle module. The current module loading logic
doesn't allow for this behavior easily as a module may be first loaded
for a normal import and extra dependencies would have to be loaded on
later imports. We may want to refactor the module loading logic to allow
this if needed.

rdar://106514965
2023-03-21 16:46:53 -07:00
Alexis Laferrière
d7cd65cdd6 [Serialization] Move some package-only import logic from writer to reader
Let's centralize the logic deciding if we load a transitive dependency
on the client side and have the producer write the truth in the
swiftmodule.
2023-03-21 16:45:49 -07:00