Commit Graph

1019 Commits

Author SHA1 Message Date
Alexis Laferrière
3ca1de0a0f [Sema] Clean up lookupImportedSPIGroups 2022-11-08 16:35:43 -08:00
Doug Gregor
0f30950fde Introduce an LRU cache for getSourceFileContainingLocation(). 2022-11-01 10:09:02 -07:00
Doug Gregor
0f9a70601a Parse and record top-level "items" rather than always forcing declarations.
In the Swift grammar, the top-level of a source file is a mix of three
different kinds of "items": declarations, statements, and expressions.
However, the existing parser forces all of these into declarations at
parse time, wrapping statements and expressions in TopLevelCodeDecls,
so the primary API for getting the top-level entities in source files
is based on getting declarations.

Start generalizing the representation by storing ASTNode instances at
the top level, rather than declaration pointers, updating many (but
not all!) uses of this API. The walk over declarations is a (cached)
filter to pick out all of the declarations. Existing parsed files are
unaffected (the parser still creates top-level code declarations), but
the new "macro expansion" source file kind skips creating top-level
code declarations so we get the pure parse tree. Additionally, some
generalized clients (like ASTScope lookup) will now look at the list
of items, so they'll be able to walk into statements and expressions
without the intervening TopLevelCodeDecl.

Over time, I'd like to phase out `getTopLevelDecls()` entirely,
relying on the new `getTopLevelItems()` for parsed content. We can
introduce TopLevelCodeDecls more lazily for semantic walks.
2022-11-01 08:04:15 -07:00
Doug Gregor
0cb2746c49 Keep track of source files created for macro expansions and such.
Introduce a new source file kind to describe source files for macro
expansions, and include the macro expression that they expand. This
establishes a "parent" relationship

Also track every kind of auxiliary source file---whether for macro
expansions or other reasons---that is introduced into a module, adding
an operation that allows us to find the source file that contains a
given source location.
2022-11-01 08:03:26 -07:00
Holly Borla
c4b946195e [AST] Replace the "type sequence" terminology with "parameter pack". 2022-10-10 16:28:13 -07:00
Nate Chandler
de19777da3 [AST] Retrieve @main type from module.
Added a convenience function to ModuleDecl to look up the entry point
@main type if there is one.
2022-09-23 15:26:22 -07:00
Alexis Laferrière
df85825592 [Sema|NFC] Rename enum cases to MissingImport
The concept of implicit import was already in use so let's differentiate
better the case of a missing import.
2022-09-19 15:13:57 -07:00
Slava Pestov
60e2fcb855 AST: ModuleDecl::lookupConformance() looks for conformances on Builtin.TheTupleType given a tuple type 2022-09-10 00:26:43 -04:00
Slava Pestov
70b553bcdc AST: Allow ModuleDecl::lookupConformance() to construct PackConformances for PackTypes 2022-09-10 00:26:43 -04:00
Alexis Laferrière
7b9d4e645c Merge pull request #61011 from xymus/spi-only-exportability
[Sema] Check exportability of @_spiOnly imported decls
2022-09-09 11:56:24 -07:00
Alexis Laferrière
6d396b4834 [Sema] Check @_spiOnly imported decls exportability
Decls imported via an @_spiOnly import can only be used in @_spi decls
signatures, internal decl signatures and non-inlinable function bodies.
2022-09-09 09:20:51 -07:00
QuietMisdreavus
f674b473ec introduce a @_documentation(...) attribute to influence SymbolGraphGen (#60242)
* add @_documentation(...) attribute to influence SymbolGraphGen

rdar://79049241
2022-09-06 14:12:42 -06:00
Alexis Laferrière
aaaef3411e [Sema] Intro @_spiOnly attribute and import filter
Introduce the attribute and basic import filter logic.
2022-09-01 15:16:44 -07:00
Alexis Laferrière
ae9bc59a93 Merge pull request #60659 from xymus/print-missing-imports
[ModuleInterface] Print missing imports in swiftinterface
2022-08-23 09:47:25 -07:00
Alexis Laferrière
66a994baa9 [Sema] Insert missing imports from any non-API modules
We can insert imports to any module from a source file that's part of a
non-API module without risking breaking the public swiftinterface.
2022-08-22 17:18:23 -07:00
Alexis Laferrière
9e3f5f9cea [ModuleInterface] Print missing imports in swiftinterface
Hack to fix swiftinterfaces in case of missing imports. We can get rid
of this logic when we don't leak the use of non-locally imported things
in API.
2022-08-19 12:32:43 -07:00
Allan Shortlidge
40eb1422bb IRGen/SIL: Fix IR linkage computation for inlined function references from modules imported @_weakLinked.
Include the parent `ModuleDecl` when serializing a `SILFunction` so that it is available on deserialized functions even though the full `DeclContext` is not present. With the parent module always available we can reliably compute whether the `SILFunction` comes from a module that was imported `@_weakLinked`.

Serialize the `DeclContext` member of `SILFunction` so that it can be used to look up the module that a function belongs to in order to compute weak import status.

Resolves rdar://98521248
2022-08-19 09:56:45 -07:00
Allan Shortlidge
1739528f6c Sema: Diagnose inconsistent use of @_weakLinked on import declarations. 2022-08-12 09:45:49 -07:00
Allan Shortlidge
7106c60e5a AST: Refactor HasImplementationOnlyImportsRequest to handle querying for imports with any ImportFlag. 2022-08-11 15:47:42 -07:00
Allan Shortlidge
f71a3232cf IRGen: Use extern_weak linkage for Clang symbols from @_weakLinked imported modules.
rdar://96098097
2022-08-11 11:02:57 -07:00
Allan Shortlidge
bc5f13cb6b AST: Accept @_weakLinked on import decls to force weak linkage of symbols from a module.
The effect of declaring an import `@_weakLinked` is to treat every declaration from the module as if it were declared with `@_weakLinked`. This is useful in environments where entire modules may not be present at runtime. Although it is already possible to instruct the linker to weakly link an entire dylib, a Swift attribute provides a way to declare intent in source code and also opens the door to diagnostics and other compiler behaviors that depend on knowing that all the module's symbols will be weakly linked.

rdar://96098097
2022-08-11 11:02:57 -07:00
Slava Pestov
63307760e4 AST: The underlying conformance of a specialized conformance is always a root conformance 2022-08-03 00:28:43 -04:00
Hamish Knight
9da53193da [AST] Remove ParameterTypeFlags from ParenType and TupleType
The last clients that relied on stashing parameter
type flags on these types are now gone.
2022-08-02 13:56:32 +01:00
Alexis Laferrière
35ff60530d Merge pull request #59955 from xymus/force-objc-import-publicly
[Sema] Force considering the `__ObjC` module as imported publicly
2022-07-08 09:16:55 -07:00
QuietMisdreavus
2d874788f6 [SymbolGraphGen] only include the given symbol for qualified imports (#59852)
* only include the given symbol for qualified imports

rdar://96309088

* re-exporting one type should not allow unrelated types to sneak in

* ensure that children of re-exported types are also re-exported
2022-07-08 09:13:08 -06:00
Alexis Laferrière
82e93806a8 [Sema] Force considering the __ObjC module as imported publicly 2022-07-07 14:57:00 -07:00
Evan Wilde
8f6d22ce3c Merge pull request #59919 from etcwilde/ewilde/improve-main-error
Adding actionable note to @main error
2022-07-06 12:51:40 -07:00
Evan Wilde
aba6b9294e Adding actionable note to @main error
The compiler enters "script" mode if there is a file called
`main.swift`, or if there is only one file. Parsing files as a script
means that anything in the file is interpreted as top-level code, which
is incompatible with a valid @main-annotated struct, so an error is
emitted.

Unfortunately, it is intentional in many cases, and the diagnostic
didn't provide anything actionable to indicate that the explicit main
function is intentional and that the file being passed in is not
actually a top-level context.

The new note indicates that passing `-parse-as-library` to the compiler
invocation will fix it if the explicit main function is intentional.
2022-07-06 09:27:54 -07:00
Alexis Laferrière
9127f90d8d [Sema] Report uses of implicitly imported decls in inlinable code
Implicitly imported decls may end up in inlinable code and break the
module API. This have been known to lead to deserialization crash and
could in theory break the generated swiftinterfaces files. Let's
explicitly check for such a case, keeping it to a warning until Swift 6
where we can make it an error.

rdar://95816286
2022-06-29 16:09:42 -07:00
Alexis Laferrière
4c995a3a87 [Sema] Generalize isImplementationOnlyImported
Renaming isImportedImplementationOnly to getRestrictedImportKind will
allow us to support different kind of restrictive imports in the next
commits.
2022-06-29 16:09:42 -07:00
Slava Pestov
bd46bdaaaa AST: Narrow the filtering of unavailable conformances to Sendable only
Remove the allowUnavailable parameter to lookupConformance(), and instead
explicitly check the result for hasUnavailableConformance() in the places
where we used to pass 'false'.

Also, narrow down this check in those places to the Sendable protocol
only, fixing a regression with Hashable conformance synthesis.

Fixes rdar://problem/94460143.
2022-06-14 21:24:08 -04:00
Robert Widmann
0d117c1345 Don't Mutate ModuleDecl's File List When Creating Synthesized Files
See #59144 for more on why this is a bad idea.

Patch out the synthesized file unit accessor to only clear the source cache, then patch up all the places that were assuming they could iterate over the module's file list and see synthesized files.

rdar://94164512
2022-06-11 11:07:05 -06:00
Robert Widmann
bbe4608fa4 Patch Out a Source of Iterator Invalidation
Synthesized file units were designed for autodiff to emit synthesized declarations, and also to sidestep the design implications of doing so late in the compiler pipeline.

A call to materialize synthesized file units was added to the GetImplicitSendable request. This introduced a source of iterator invalidation into forEachFileToTypeCheck in whole-module builds. Any call to insert a new file into the module has the potential to cause the underlying SmallVector to reallocate.

This patch provides a narrow workaround that stops using iterators altogether in forEachFileToTypeCheck. However, this bug reveals a severe architectural flaw in the concept of a synthesized file unit. Iterating over the files in a module is an extremely common operation, and there now are myriad ways we could wind up calling a function that mutates the module's list of files in the process. This also means the number and kind of files being visited by compiler analyses is dependent upon whether a request that inserts these files has or has not been called.

This suggests the call to ModuleDecl::addFile in FileUnit::getOrCreateSynthesizedFile is deleterious and should be removed. Doing so will come as part of a larger refactoring.

rdar://94043340
2022-05-28 12:37:24 -07:00
Doug Gregor
c9c50b4ae0 [Requirement machine] Ignore unavailable conformances on superclass constraints.
When determining whether a superclass conforms to a particular protocol,
skip unavailable conformances. This way, we don't minimize away a
constraint that might only apply to subclasses of the specified
superclass.

Fixes rdar://91853658.
2022-05-27 13:09:15 -07:00
Ben Barham
4a4dded2fc [IDE] Skip walking serialized non-visible extension decls
fec7a0b79b skipped all non-visible
`ValueDecls` but missed `ExtensionDecls`, which have the same issue.
Make sure to skip these too.

Resolves rdar://91279771.
2022-05-23 11:40:41 -07:00
Doug Gregor
8c3d1fb2ca Make sure we don't provide duplicate synthesized conformance table entries.
This was benign with `Sendable`, but is not benign for the `Encodable`
and `Decodable` synthesis for distributed actors, which results in a
crash in TBD generation.

Fixes rdar://92008955.
2022-04-22 15:41:01 -07:00
Alexis Laferrière
29cd5518fa Merge pull request #42432 from xymus/consider-more-paths-as-spi
[Sema] Consider more paths in the SDK as defining private modules
2022-04-18 17:47:44 -07:00
Alexis Laferrière
91001f0257 [Sema] Consider modules imported from more paths as private
Consider both frameworks and free floating modules imported from more
private path as SPI. This will make the compiler raise errors on public
imports of more private modules.

rdar://91904154
2022-04-18 14:37:48 -07:00
Alexis Laferrière
fb155133a3 [Sema][NFC] Refactor the logic matching paths in the SDK 2022-04-18 14:36:53 -07:00
Ben Barham
fec7a0b79b [IDE] Skip walking serialized internal top level decls in SemaAnnotator
`SemaAnnotator` always attempts to retrieve the location of the decl. This
requires generating the USR, which needs to resolve the type. But that's
invalid in the presence of `@_implementationOnly`.

This most commonly comes up during index while building as even though
it skip internal decls, by that point it's too late (`SemaAnnotator` has
already tried retrieving the location). Other uses of
`SourceEntityWalker` should be unaffected as they only run over the
current decls in the current module.

For now skip walking internal top level decls from serialized modules in
`SemaAnnotator`. Consider expanding this to most, if not all,
`ASTWalker` clients in the future - it's unlikely `SemaAnnotator` is the
only one with this problem.

Resolves rdar://91279771.
2022-04-15 17:08:47 -07:00
John McCall
5519749ade [NFC] Collect protocol decls, not type, in ExistentialLayout
Another thing that will be necessary for correctness with
compositions of parameterized protocols.
2022-04-11 22:15:16 -04:00
Konrad `ktoso` Malawski
b86bb0ddeb fix require-explicit-sendable 2022-03-11 18:07:14 +09:00
Konrad `ktoso` Malawski
6d502fc042 [Distributed] Implicit Codable conformance for Dist Actors
[Witness] implement dump() on witness
2022-03-11 18:07:14 +09:00
Franklin Schrans
9cd44ca5d1 [SymbolGraphGen] Emit symbols from exported modules
When emitting a symbol graph file for a module that import modules via
`@_exported import`, emits those modules' symbols as well.

SR-15753

rdar://89547374
2022-02-28 17:21:25 +00:00
Victoria Mitchell
cab1669e09 only recurse getDisplayDecls in SymbolGraphGen 2022-02-19 10:32:29 -07:00
QuietMisdreavus
036d78f6ad Merge pull request #41115 from apple/QuietMisdreavus/duplicate-clang-syms
skip implicit clang decls in the duplicate-decl assertion

SR-15801
2022-02-19 10:30:53 -07:00
Victoria Mitchell
46f6b72ed6 skip implicit clang decls in the duplicate-decl assertion
SR-15801

rdar://88299954
2022-01-31 18:17:34 -07:00
Evan Wilde
3b90b08582 Use presence of 'await' to make top-level async
To help maintain source-compatibility, the presence of an `await` in
top-level code to kick the top-level code over to being a concurrent
context.

This, of course, means that in the test cases that exist today, they
will go back to behaving identically to how they did before I added all
of this because they don't have any awaits in the top-level. I'll be
adding new tests to verify the differences in behavior between swift 5,
swift 6, with and without async top level enabled in the next commit.
2022-01-28 07:55:14 -08:00
Doug Gregor
7149702d12 Rename @_predatesConcurrency to @preconcurrency.
Introduce the `@preconcurrency` attribute name for `@_predatesConcurrency`,
which has been the favored name in the pitch thread so far. Retain the
old name for now to help smooth migration.
2022-01-26 08:39:01 -08:00
Victoria Mitchell
aeaf6b22db [AST] check modules before recursing for display decls 2022-01-18 08:51:29 -07:00