Commit Graph

2036 Commits

Author SHA1 Message Date
Robert Widmann
26d8bad7c2 Add DirectLookupRequest 2020-01-25 11:04:53 -08:00
Brent Royal-Gordon
7df141cdad Merge pull request #29341 from brentdax/path-dependence
[MiscDiagnostics] Warn if magic identifiers don’t match
2020-01-21 20:45:47 -08:00
Brent Royal-Gordon
b43b1ec1be [NFC] Allow getParameter{List,At} to be called on any ValueDecl
These now return nullptr for parameter-list-free types; previously those would have failed a cast.
2020-01-21 14:28:16 -08:00
Nathan Hawes
b380cf9934 [AST|ASTWalker] Fix assertion hit walking an invalid AST with a protocol decl inside an extension
The AST walker calling getGenericParams on a protocol decl would eventually
call computeNominalType. computeNominalType checks that the protocol (as a
nomimnal type decl) appears within a type context, and if so, asks for the
SelfNominalTypeDecl of that context. If the context is an extension, that
ends up asking for its extended nominal, which is a problem if we're walking
a pre-typechecked AST – we hit the assertion "Extension must have already been
bound (by bindExtensions)").

Unlike other nominal types, it's not valid Swift for protocols to appear within
type contexts, so exclude protocol decls from taking this code path. This
results in us providing Type() as the parent type of the produced NominalType,
just like we do for protocols inside functions or other invalid contexts.

Resolves <rdar://problem/58549036>
2020-01-21 11:57:07 -08:00
Dan Zheng
44d937d7c2 [AutoDiff upstream] Add @differentiable declaration attribute type-checking. (#29231)
The `@differentiable` attribute marks a function as differentiable.

Example:
```
@differentiable(wrt: x, jvp: derivativeFoo where T: Differentiable)
func id<T>(_ x: T) -> T { x }
```

The `@differentiable` attribute has an optional `wrt:` clause specifying the
parameters that are differentiated "with respect to", i.e. the differentiability
parameters. The differentiability parameters must conform to the
`Differentiable` protocol.

If the `wrt:` clause is unspecified, the differentiability parameters are
currently inferred to be all parameters that conform to `Differentiable`.

The `@differentiable` attribute also has optional `jvp:` and `vjp:` labels
for registering derivative functions. These labels are deprecated in favor of
the `@derivative` attribute and will be removed soon.

The `@differentiable` attribute also has an optional `where` clause, specifying
extra differentiability requirements for generic functions.

The `@differentiable` attribute is gated by the
`-enable-experimental-differentiable-programming` flag.

Code changes:
- Add `DifferentiableAttributeTypeCheckRequest`.
  - Currently, the request returns differentiability parameter indices, while
    also resolving `JVPFunction`, `VJPFunction`, and
    `DerivativeGenericSignature` and mutating them in-place in
    `DifferentiableAttr`. This was the simplest approach that worked without
    introducing request cycles.
- Add "is type-checked" bit to `DifferentiableAttr`.
  - Alternatively, I tried changing `DifferentiableAttributeTypeCheckRequest` to
    use `CacheKind::Cache` instead of `CacheKind::SeparatelyCached`, but it did
    not seem to work: `@differentiable` attributes in non-primary-files were
    left unchecked.

Type-checking rules (summary):
- `@differentiable` attribute must be declared on a function-like "original"
  declaration: `func`, `init`, `subscript`, `var` (computed properties only).
- Parsed differentiability parameters must be valid (if they exist).
- Parsed `where` clause must be valid (if it exists).
- Differentiability parameters must all conform to `Differentiable`.
- Original result must all conform to `Differentiable`.
- If JVP/VJP functions are specified, they must match the expected type.
  - `@differentiable(jvp:vjp:)` for derivative registration is deprecated in
    favor of `@derivative` attribute, and will be removed soon.
- Duplicate `@differentiable` attributes with the same differentiability
  parameters are invalid.
- For protocol requirements and class members with `@differentiable` attribute,
  conforming types and subclasses must have the same `@differentiable` attribute
  (or one with a superset of differentiability parameter indices) on
  implementing/overriding declarations.
2020-01-19 12:23:04 -08:00
Xi Ge
3ee9b1175c TBDGen: when previous install name map is specified, emit $ld$previous linker directives.
Progress towards: rdar://58281536
2020-01-17 15:57:25 -08:00
Brent Royal-Gordon
0c478b6be6 Revert "Merge pull request #28665 from CodaFi/the-phantom-menace"
This reverts commit 43a3ab7e35, reversing
changes made to 4f39d9c749.

# Conflicts:
#	include/swift/AST/Attr.def
#	lib/AST/Attr.cpp
#	lib/Serialization/Deserialization.cpp
#	lib/Serialization/ModuleFormat.h
#	lib/Serialization/Serialization.cpp
2020-01-15 15:28:42 -08:00
Slava Pestov
4771ce092b AST: Weak-link declarations in extensions of weak-linked types
This is a workaround to fix weak linking of frameworks that define
types with availability, but then define extensions of those types
without availability.

This can come up if the framework itself is built with a newer
deployment target than the client that uses the framework. Since the
type checker only enforces that an extension has availability if
the extension is less available than the deployment target, we were
failing to weak link the members of the extension in this case.

This is not a perfect fix; ideally such frameworks should be built
with -require-explicit-availability, and all reported warnings
fixed by adding explicit availability.

However, it allows clients to weak link when using existing
swiftinterface files that have already shipped in the mean time,
and it should not cause any problems once the frameworks are properly
annotated in the future.

Fixes <rdar://problem/58490723>.
2020-01-13 22:47:53 -05:00
AG
f34c97069a Merge pull request #28678 from bitjammer/acgarland/symbol-graph
Symbol graph support
2020-01-10 14:53:19 -08:00
Ashley Garland
7a3a0a9e23 Symbol graph support
Adds a tool `swift-symbolgraph-extract` that reads an existing Swift
module and prints a platform- and language-agnostic JSON description of
the module, primarly for documentation.

Adds a small sub-library `SymbolGraphGen` which houses the core
implementation for collecting relevant information about declarations.
The main entry point is integrated directly into the driver as a mode:
the tool is meant to be run outside of the normal edit-compile-run/test
workflow to avoid impacting build times.

Along with common options for other tools, unique options include
`pretty-print` for debugging, and a `minimum-access-level` options for
including internal documentation.

A symbol graph is a directed graph where the nodes are symbols in a
module and the edges are relationships between them. For example, a
`struct S` may have a member `var x`. The graph would have two nodes for
`S` and `x`, and one "member-of" relationship edge. Other relationship
kinds include "inherits-from" or "conforms to". The data format for a
symbol graph is still under development and may change without notice
until a specificiation and versioning scheme is published.

Various aspects about a symbol are recorded in the nodes, such as
availability, documentation comments, or data needed for printing the
shapes of declarations without having to understand specifics about the
langauge.

Implicit and public-underscored stdlib declarations are not included by
default.

rdar://problem/55346798
2020-01-10 09:53:37 -08:00
Doug Gregor
946eee9cf8 Merge pull request #29102 from DougGregor/function-builder-via-constraint-system
[Constraint solver] Use a constraint system to apply all function builders
2020-01-09 23:42:32 -08:00
Doug Gregor
ac24491dae [Constraint system] Generalize function builder application APIs.
Teach the constraint system to handle matching a function builder to a
function as well as a closure.
2020-01-08 16:19:44 -08:00
Varun Gandhi
022314a640 Merge pull request #28643 from kitaisreal/using-located-instead-of-pair
[Compiler]: Using Located<T> instead of std::pair<SourceLoc, T>
2020-01-06 14:22:29 -08:00
Harlan Haskins
283854a012 [Sema] Requestify hasMissingDesignatedInitializers
We’re going to start serializing this for public types that have non-public-or-@usableFromInline initializers, so turn it into a request that we can query and cache it in the existing bit.
2020-01-06 10:15:07 -08:00
Saleem Abdulrasool
64c170b5e9 AST: remove unnecessary variable (NFC)
This removes the accidental variable that was being created during the
switch.  The value is available without the variable.
2020-01-02 10:34:00 -08:00
Robert Widmann
a2ba135b71 Merge pull request #28948 from CodaFi/wysiwyg-interface-type
Wysiwyg interface type
2020-01-02 13:14:08 -05:00
Robert Widmann
0f14ad442c [NFC] Make it illegal to reset the interface type to null 2019-12-25 17:39:51 -05:00
Robert Widmann
47a2bf3fdc [NFC] One-Shot Name Lookup
Simplify lookupDirect to attempt one-shot name lookup based on some ideas Slava had.  This means we'll try to perform a cache fill up front, then access the table rather than assuming the table is always (relatively) up to date and filling when we miss the first cache access.

This avoids a walk over the deserialized members of an extension that fails named lazy member loading.  Instead, we eagerly page the members of the extension into the table and remove it from consideration for lazy member loading entirely.

In the future, we can convince the Clang Importer to avoid falling off the lazy member loading happy path.
2019-12-20 17:30:33 -08:00
Kita, Maksim
ea6a2dc094 SR-11889: Fixed code review issues
1. Updated Located field names with Pascal Case
2. Updated Located constuctor
3. Formatted lines with more than 80 symbols
2019-12-20 17:18:59 +03:00
Kita, Maksim
c1444dea18 SR-11889: Fixed code review issues 2019-12-20 17:18:59 +03:00
Slava Pestov
e4626263c3 AST: More consistent definition of a 'local capture'
This commit adds a new ValueDecl::isLocalCapture() predicate and
uses it in the right places. The predicate is true if the
declaration is in local context, *or* if its at the top level of
the main source file and follows a 'guard' statement.

Fixes <rdar://problem/23051362> / <https://bugs.swift.org/browse/SR-3528>.
2019-12-19 23:47:24 -05:00
Slava Pestov
8e6dc39f14 Parse: Keep track of top-level declarations that follow a 'guard' statement 2019-12-19 23:46:56 -05:00
Robert Widmann
4a943abbdd Merge pull request #28845 from CodaFi/a-little-looksie
Refactor Direct Lookup
2019-12-19 19:43:26 -08:00
Frederick Kellison-Linn
71697c37ca Allow implicit self in escaping closures when self usage is unlikely to cause cycle (#23934)
* WIP implementation

* Cleanup implementation

* Install backedge rather than storing array reference

* Add diagnostics

* Add missing parameter to ResultFinderForTypeContext constructor

* Fix tests for correct fix-it language

* Change to solution without backedge, change lookup behavior

* Improve diagnostics for weak captures and captures under different names

* Remove ghosts of implementations past

* Address review comments

* Reorder member variable initialization

* Fix typos

* Exclude value types from explicit self requirements

* Add tests

* Add implementation for AST lookup

* Add tests

* Begin addressing review comments

* Re-enable AST scope lookup

* Add fixme

* Pull fix-its into a separate function

* Remove capturedSelfContext tracking from type property initializers

* Add const specifiers to arguments

* Address review comments

* Fix string literals

* Refactor implicit self diagnostics

* Add comment

* Remove trailing whitespace

* Add tests for capture list across multiple lines

* Add additional test

* Fix typo

* Remove use of ?: to fix linux build

* Remove second use of ?:

* Rework logic for finding nested self contexts
2019-12-20 02:38:41 +00:00
Rintaro Ishizaki
10936f6d49 Merge pull request #28727 from rintaro/sourcekit-completion-reuseinstance
[SourceKit] ️Fast code completion within function bodies
2019-12-19 14:20:09 -08:00
Robert Widmann
1a9bdaffa2 Refactor Direct Name Lookup
The old name lookup would frequently try to flush and rebuild the name lookup cache.  Instead, never flush the cache, and use the cache misses as an opportunity to load members and bring the lookup table up to date with any added extensions.
2019-12-18 22:36:27 -08:00
Rintaro Ishizaki
62c44126b6 [SourceKit] Reuse compiler instance between multiple completion
- Introduce ide::CompletionInstance to manage CompilerInstance
- `CompletionInstance` vends the cached CompilerInstance when:
-- The compiler arguments (i.e. CompilerInvocation) has has not changed
-- The primary file is the same
-- The completion happens inside function bodies in both previous and
   current completion
-- The interface hash of the primary file has not changed
- Otherwise, it vends a fresh CompilerInstance and cache it for the next
  completion

rdar://problem/20787086
2019-12-18 21:52:20 -08:00
Marc Rasi
72194c547b Merge branch 'master' into ast-nondiff 2019-12-18 17:25:09 -08:00
Xi Ge
0823d8b6e4 Check for nullptr inside NominalTypeDecl::isResilient
rdar://58049956
2019-12-18 13:33:11 -08:00
Xi Ge
1ebc78b22b [RemoteAST] Using module names specified by @_originallyDefinedIn for top-level decls marked as so
This should allow runtime de-mangling of those moved symbols.
2019-12-16 17:57:32 -08:00
Xi Ge
03fab30ee0 Merge branch 'master' into tbdgen-ld-hide 2019-12-14 20:57:05 -08:00
Xi Ge
25376025ae IRGen: keep emitting protocol witness table symbols for refactored protocols
When Protocol P and Struct S are in a same module and S conforms to P, the protocol
witness table is emitted directly as a symbol. If we move P to a lower-level module,
the protocol witness table symbol isn't emitted, breaking users' existing executable as
a result.

This change checks whether the protocol used to be defined in the same
module and marks it as non-resilient if so. The compiler will continue
emitting witness table as symbols to maintain cross-module ABI stability.
2019-12-12 22:22:28 -08:00
Brent Royal-Gordon
1df792ae9f [NFC] Convert TypeRepr to use DeclName(Loc)?
Replaces `ComponentIdentTypeRepr::getIdentifier()` and `getIdLoc()` with `getNameRef()` and `getNameLoc()`, which use `DeclName` and `DeclNameRef` respectively.
2019-12-11 00:45:08 -08:00
Robert Widmann
785bda0250 Discard ResolveEncodable and ResolveDecodable 2019-12-10 16:28:51 -08:00
Brent Royal-Gordon
e1e7a3fe75 Merge pull request #25656 from brentdax/file-name-basis
Shorten #file and add #filePath (behind an experimental flag)
2019-12-06 21:34:58 -08:00
Doug Gregor
f51119ad6b [Property wrappers] Sync up implicit "nil" initialization defaulting.
Perform implicit "nil" initialization for a wrapped property only when
the memberwise initializer's parameter is of the wrapped type.
2019-12-05 22:04:18 -08:00
Doug Gregor
493bed0043 [Property wrappers] Fix crash due to wrapped type/wrapper type confusion.
We had two predicates that were used to determine whether the default
argument for a wrapped property in the memberwise initializer would be
of the wrapper type (e.g., Lazy<Int>) vs. the wrapped type
(Int). Those two predicates could disagree, causing a SILGen assertion
and crash. Collapse the two predicates into one correct one,
fixing rdar://problem/57545381.
2019-12-05 09:27:30 -08:00
Brent Royal-Gordon
63ec1cf5af Introduce a separate #filePath, remove -pound-file
This makes the path behavior more first-class. The feature is now hidden behind an experimental flag, -enable-experimental-concise-pound-file.
2019-12-04 16:35:13 -08:00
Erik Eckstein
cb7ea10ce1 Support fileprivate access level to be usableFromInline.
This is needed for cross-module-optimization.
So far only internal and private was supported.
2019-12-04 17:02:56 +01:00
Robert Widmann
4e36fc0bd5 Replace semantic usages of DynamicReplacementAttr with the request
Complete the refactoring by splitting the semantic callers for the original decl of a dynamically replaced declaration.

There's also a change to the way this attribute is validated and placed.  The old model visited the attribute on any functions and variable declarations it encountered in the primary.  Once there, it would strip the attribute off of variables and attach the corresponding attribute to each parsed accessor, then perform some additional ObjC-related validation.

The new approach instead leaves the attribute alone.  The request exists specifically to perform the lookups and type matching required to find replaced decls, and the attribute visitor no longer needs to worry about revisiting decls it has just grafted attributes onto.  This also means that a bunch of parts of IRGen and SILGen that needed to fan out to the accessors to ask for the @_dynamicReplacement attribute to undo the work the type checker had done can just look at the storage itself.  Further, syntactic requests for the attribute will now consistently succeed, where before they would fail dependending on whether or not the type checker had run - which was generally not an issue by the time we hit SIL.
2019-12-03 15:30:04 -08:00
Robert Widmann
8201b5ceb8 Rephrase hasDidSetOrWillSetDynamicReplacement
Use the new dynamically replaced request to power this predicates.
2019-12-03 15:30:04 -08:00
Robert Widmann
c60b661c29 Add a request for replaced decls
Add DynamicallyReplacedDeclRequest to ValueDecl and plumb the request through to TypeCheckAttr where it replaces TypeChecker::findReplacedDynamicFunction.
2019-12-03 15:30:04 -08:00
Xi Ge
1390576528 Merge pull request #28528 from nkcsgexi/use-alternate-module-name
ASTMangler: use specified module names from @_originalDefinedIn to mangle symbols names
2019-12-03 07:25:22 -08:00
Erik Eckstein
a5397b434c Cross module optimization
This is a first version of cross module optimization (CMO).

The basic idea for CMO is to use the existing library evolution compiler features, but in an automated way. A new SIL module pass "annotates" functions and types with @inlinable and @usableFromInline. This results in functions being serialized into the swiftmodule file and thus available for optimizations in client modules.
The annotation is done with a worklist-algorithm, starting from public functions and continuing with entities which are used from already selected functions. A heuristic performs a preselection on which functions to consider - currently just generic functions are selected.

The serializer then writes annotated functions (including function bodies) into the swiftmodule file of the compiled module. Client modules are able to de-serialize such functions from their imported modules and use them for optimiations, like generic specialization.

The optimization is gated by a new compiler option -cross-module-optimization (also available in the swift driver).
By default this option is off. Without turning the option on, this change is (almost) a NFC.

rdar://problem/22591518
2019-12-03 14:37:01 +01:00
Doug Gregor
dc258b2083 Merge pull request #28531 from DougGregor/explicit-init-property-wrapper-opt
[AST] Properties can be explicitly initialized via their wrapper.
2019-12-02 20:35:28 -08:00
Doug Gregor
9eac6fe364 [AST] Properties can be explicitly initialized via their wrapper.
The computation for "is explicitly initialized" on a pattern binding
entry didn't account for explicit initialization via the property
wrapper (e.g. @Wrapper(closure: { ... })), which lead to a crash for
properties with optional type. Fixes rdar://problem/57411331.
2019-12-02 18:03:26 -08:00
Xi Ge
7e8ed50233 ASTMangler: use specified module names from @_originalDefinedIn to mangle symbols names
When an original module name is specified via @_originalDefinedIn attribute, we need to
use the original module name for all related runtime symbol names instead of the current
module names.

rdar://55268186
2019-12-02 16:58:31 -08:00
Hamish Knight
c667d2b361 Use DefaultArgumentExpr for caller-side defaults
This commit changes how we represent caller-side
default arguments within the AST. Instead of
directly inserting them into the call-site, use
a DefaultArgumentExpr to refer to them indirectly.

The main goal of this change is to make it such
that the expression type-checker no longer cares
about the difference between caller-side and
callee-side default arguments. In particular, it
no longer cares about whether a caller-side
default argument is well-formed when type-checking
an apply. This is important because any
conversions introduced by the default argument
shouldn't affect the score of the resulting
solution.

Instead, caller-side defaults are now lazily
type-checked when we want to emit them in SILGen.
This is done through introducing a request, and
adjusting the logic in SILGen to be more lenient
with ErrorExprs. Caller-side defaults in primary
files are still also currently checked as a part
of the declaration by `checkDefaultArguments`.

Resolves SR-11085.
Resolves rdar://problem/56144412.
2019-11-20 15:07:32 -08:00
Slava Pestov
ef05accd31 Sema: Remove even more vestigial TypeChecker usages 2019-11-19 17:40:00 -05:00
Marc Rasi
e5e25a6c71 Merge branch 'master' of github.com:apple/swift into ast-nondiff 2019-11-15 16:41:30 -08:00