Commit Graph

351 Commits

Author SHA1 Message Date
Alexis Laferrière
c38d1773d2 [Serialization] Restrict loading swiftmodule files to the builder's SDK
Serialize the canonical name of the SDK used when building a swiftmodule
file and use it to ensure that the swiftmodule file is loaded only with
the same SDK. The SDK name must be passed down from the frontend.

This will report unsupported configurations like:

- Installing roots between incompatible SDKs without deleting the
swiftmodule files.
- Having multiple targets in the same project using different SDKs.
- Loading a swiftmodule created with a newer SDK (and stdlib) with an
older SDK.

All of these lead to hard to investigate deserialization failures and
this change should detect them early, before reaching a deserialization
failure.

rdar://78048939
2021-09-13 16:44:08 -07:00
Andrew Trick
4828285944 Add pointer_to_address [align=] option.
Support for addresses with arbitrary alignment as opposed to their
element type's natural in-memory alignment.

Required for bytestream encoding/decoding without resorting to memcpy.

SIL instruction flag, documentation, printing, parsing, serialization,
and IRGen.
2021-09-13 10:26:14 -07:00
Michael Gottesman
5590c7b526 [sil] Add a move_value instruction.
This is a new instruction that can be used by SILGen to perform a semantic move
in between two entities that are considered separate variables at the AST
level. I am going to use it to implement an experimental borrow checker.

This PR contains the following:

1. I define move_value, setup parsing, printing, serializing, deserializing,
   cloning, and filled in all of the visitors as appropriate.
2. I added createMoveValue and emitMoveValueOperation SILBuilder
   APIs. createMoveValue always creates a move and asserts is passed a trivial
   type. emitMoveValueOperation in contrast, will short circuit if passed a
   trivial value and just return the trivial value.
3. I added IRGen tests to show that we can push this through the entire system.

This is all just scaffolding for the instruction to live in SIL land and as of
this PR doesn't actually do anything.
2021-09-12 11:07:42 -07:00
Andrew Trick
33fecc9b21 Serialize SIL forwarding ownership on terminators. 2021-09-07 10:20:14 -07:00
Anthony Latsis
eb1bd07bb3 NFC: Remove the now dead ProtocolDecl::existentialTypeSupported() 2021-08-31 19:21:26 +03:00
Kavon Farvardin
c0607b345f Revert "Merge pull request #33767 from theblixguy/chore/remove-self-or-associated-type-diagnostic"
The following regression test added for this feature is not passing:

Swift(linux-x86_64) :: decl/protocol/protocols_with_self_or_assoc_reqs_executable.swift

with a compiler crash happening during SILFunctionTransform "Devirtualizer".

Reverting to unblock CI.

This reverts commit f96057e260, reversing
changes made to 3fc18f3603.
2021-08-26 16:46:42 -07:00
Konrad `ktoso` Malawski
f96057e260 Merge pull request #33767 from theblixguy/chore/remove-self-or-associated-type-diagnostic
SE-0309: Unlock existential types for all protocols
2021-08-27 06:09:47 +09:00
Anthony Latsis
755f0f96e0 NFC: Remove the now dead ProtocolDecl::existentialTypeSupported() 2021-08-16 18:30:26 +03:00
Doug Gregor
d54abea922 Implement customizable Sendable conformance diagnostics.
Rework Sendable checking to be completely based on "missing"
conformances, so that we can individually diagnose missing Sendable
conformances based on both the module in which the conformance check
happened as well as where the type was declared. The basic rules here
are to only diagnose if either the module where the non-Sendable type
was declared or the module where it was checked was compiled with a
mode that consistently diagnoses `Sendable`, either by virtue of
being Swift 6 or because `-warn-concurrency` was provided on the
command line. And have that diagnostic be an error in Swift 6 or
warning in Swift 5.x.

There is much tuning to be done here.
2021-08-14 08:13:10 -07:00
Kavon Farvardin
89629fe06f Generalize BodyKind::MemberwiseInitializer
We'd like to support factor initializers
for distributed actor types that are
synthesized by SILGen.

We already do something similar for
memberwise initializers for structs.
Thus, this patch generalizes that
concept into a new BodyKind for
AbstractFunctionDecls called
BodyKind::SILSynthesize.

In addition, to help differentiate the
kinds of AFDs that are SILSynthesized
into different families for SILGen to
recognize, we also have a new enum
SILSynthesizeKind to indicate whether it
is a memberwise init, etc.
2021-08-12 14:05:31 +09:00
Robert Widmann
592e90af9b Add Sugar for Variadics
We used to represent the interface type of variadic parameters directly
with ArraySliceType. This was awfully convenient for the constraint
solver since it could just canonicalize and open [T] to Array<$T>
wherever it saw a variadic parameter. However, this both destroys the
sugaring of T... and locks the representation to Array<T>. In the
interest of generalizing this in the future, introduce
VariadicSequenceType. For now, it canonicalizes to Array<T> just like
the old representation. But, as you can guess, this is a new staging
point for teaching the solver how to munge variadic generic type bindings.

rdar://81628287
2021-08-06 12:51:39 -07:00
Becca Royal-Gordon
627ecbdfff Rip designated types out of the AST
Designated types were removed from the constraint solver in #34315, but they are currently still represented in the AST and fully checked. This change removes them as completely as possible without breaking source compatibility (mainly with old swiftinterfaces) or changing the SwiftSyntax tree. Designated types are still parsed, but they are dropped immediately and a warning is diagnosed. During decl checking we also still check if the precedence group is really a designated type, but only so that we can diagnose a warning and fall back to DefaultPrecedence.

This change also fixes an apparent bug in the parser where we did not diagnose operator declarations that contained a `:` followed by a non-identifier token.
2021-08-03 16:13:59 -07:00
Doug Gregor
c79fa23339 Implicitly build "missing" conformances to Sendable.
When looking up a conformance to Sendable fails, implicitly create a
"missing" builtin conformance. Such conformances allow type checking
to continue even in the presence of Sendable-related problems.

Diagnose these missing conformances when they are used in an actual
program, as part of availability checking for conformances and when we
are determining Sendability. This allows us to decide between an
error, a warning, and suppressing the diagnostic entirely without
affecting how the program is compiled. This is a step toward enabling
selective enforcement of Sendable.

Part of rdar://78269348.
2021-08-02 16:16:59 -07:00
Slava Pestov
4e1c2b2e47 Serialization: Serialize the list of associated types in a protocol
This allows ProtocolDecl::getAssociatedTypeMembers() on a serialized
ProtocolDecl to avoid deserializing the full member list.
2021-07-31 00:25:22 -04:00
Ben Barham
e7e9b57051 Replace @completionHandlerAsync with @available(*, renamed:)
Instead of a new attribute `@completionHandlerAsync`, allow the use of
the existing `renamed` parameter of `@available` to specify the
asynchronous alternative of a synchronous function.

No errors will be output from invalid names as `@completionHandlerAsync`
had, but if a function is correctly matched then it will be used to
output warnings when using the synchronous function in an asynchronous
context (as before).

Resolves rdar://80612731
2021-07-29 09:14:44 +10:00
Doug Gregor
ecd830e342 Rework BuiltinProtocolConformance to be more like NormalProtocolConformance.
Give BuiltinProtocolConformance a generic signature, which can be used to
describe the generic parameters used within the builtin conformance, e.g.,
`<T1, T2, T3>` for a tuple type `(T1, T2, T3)`. Also store the
conditional requirements as trailing objects, requiring them to be
precomputed by whatever builds the conformances. Together, this means
that builtin protocol conformances act like normal conformances with
respect to conditional requirements and substitutions: they will be
defined generically, then a specialized conformance will be layered on
top to provide the substitutions.
2021-07-27 23:49:27 -07:00
Azoy
20cc067844 [AST] Introduce BuiltinProtocolConformance 2021-07-27 23:49:27 -07:00
Doug Gregor
06bbc70b3e Module printing and serialization support for @unchecked Sendable 2021-07-11 12:29:54 -07:00
Doug Gregor
29f5d7a64a [SE-0302] Implement '@unchecked Sendable' syntax.
Parse and provide semantic checking for '@unchecked Sendable', for a
Sendable conformance that doesn't perform additional semantic checks
for correctness.

Part of rdar://78269000.
2021-07-11 12:29:53 -07:00
Michael Gottesman
3fdb0fd541 [assembly-vision] Add function attribute '@_assemblyVision'
This is just a shortcut for @_semantics("optremark") to make it easier for
people to remember how to enable assembly vision remarks.

Now one can just type:

```
@_assemblyVision
func foo() {
  ...
}
```

and get all normal opt-remarks + assembly vision remarks.
2021-06-12 15:09:51 -07:00
Doug Gregor
bd8626fa1b [Concurrency] Parse and add 'isolated' parameters to the type system. 2021-06-07 23:59:38 -07:00
Doug Gregor
03f55d7bb4 Revert "Revert "Remove @actorIndependent attribute.""
This reverts commit 7c0b50e8ea.
2021-06-03 14:03:57 -07:00
Karoy Lorentey
7c0b50e8ea Revert "Remove @actorIndependent attribute." 2021-05-24 14:43:32 -07:00
Doug Gregor
4d91f79bd5 Merge pull request #37462 from DougGregor/remove-actor-independent 2021-05-18 12:08:17 -07:00
Doug Gregor
d6e8fd81f5 Remove @actorIndependent attribute.
`@actorIndependent` has been superseded by `nonisolated`. Remove
the old spelling entirely.
2021-05-17 12:21:10 -07:00
Xi Ge
5c5715f626 Serialization: embed subminor and build version in the user module version entry 2021-05-17 10:15:47 -07:00
Erik Eckstein
762e9c7882 Serialization: serialize if the module is a static library
If the `-static` option is specified, store that in the generated
swiftmodule file.  When de-serializing, recover this information in the
representative SILModule.

This will be used for code generation on Windows.  It is the missing
piece to allow static linking to function properly.  It additionally
opens the path to additional optimization on ELF-ish targets - GOT, PLT
references can be avoided when the linked module is known to be static.

Co-authored by: Saleem Abdulrasool <compnerd@compnerd.org>
2021-05-02 09:34:52 -07:00
Xi Ge
01800ffc73 Remove empty line. NFC 2021-04-30 10:21:33 -07:00
Xi Ge
043d492c65 Merge branch 'main' into 73992299-1 2021-04-30 10:19:57 -07:00
Xi Ge
fe5c7ef995 ModuleInterface/Serialization: allow library authors to define a custom module version number
This allows library authors to pass down a project version number so that library users can conditionally
import that library based on the available version in the search paths.

Needed for rdar://73992299
2021-04-30 10:00:45 -07:00
Hamish Knight
f439275380 Mark imported @completionHandlerAsync attrs as implicit
Mark imported `@completionHandlerAsync` attrs as
implicit, which avoids printing them in generated
interfaces. And for the sake of completion,
serialize the implicit bit in case it's used
elsewhere in the future.

To make sure we continue to print
`@completionHandlerAsync` attributes explicitly
written by the user in Swift, add a SourceKit
interface test.

Resolves rdar://76685011
2021-04-30 11:14:50 +01:00
Kavon Farvardin
4f6eb85fe3 allow type initializers to be 'async'
implicit calls to an async super.init are not allowed
2021-04-15 10:08:53 -07:00
Doug Gregor
26551b16e6 [SIL] Fix egregious errors in extract_executor handling.
Make sure it is properly treated as a SingleValueInstruction, and
don't blatantly read freed memory in the LowerHopToExecutor pass.
2021-04-14 23:33:51 -07:00
Alex Hoppen
380db634fa [Serialization] Serialize internal closure labels
Since 865e80f9c4 we are keeping track of internal closure labels in the closure’s type. With this change, wer are also serializing them to the swiftmodules.

Furthermore, this change adjusts the printing behaviour to print the parameter labels in the swiftinterfaces.

Resolves rdar://63633158
2021-04-13 08:53:46 +02:00
Kavon Farvardin
f9452b23f4 implement parsing for effects specifiers on 'get' accessors
An effectful 'get' accessor must be the only accessor for the
property.
2021-03-26 07:54:53 -07:00
Michael Gottesman
504b5f2058 [ossa][frontend] Separately namespace enable-ossa-modules in the prebuilt module cache so that the flag causes recompilation of imported resilient modules when the flag is enabled.
This will enable users to try out the '-enable-ossa-modules' flag if their
compiler supports it and get OSSA code on all inlinable code that they use. The
idea is that this is a nice way to stage this in and get more testing.

The specific implementation is that the module interface loader:

1. Knows if enable ossa modules is enabled not to search for any compiled
modules. We always rebuild from the interface file on the system.

2. Knows that if enable ossa modules is enabled to mixin a bit into the module
interface loader cache hash to ensure that we consider the specialized ossa
compiled modules to be different than the modules in that cache from the system.

This ensures that when said flag is enabled, the user transparently gets all
their code in OSSA form from transparent libraries.
2021-03-18 13:03:51 -07:00
Doug Gregor
3a357e9978 Add global actor-qualified function types.
Introduce the notion of global actor-qualified function types, e.g.,

    @MainActor () -> Void

to describe synchronous functions that must execute on a particular
global actor.
2021-03-17 00:51:16 -07:00
Evan Wilde
eaab395973 Remove ExplicitCompletionHandlerIndex flag
Assuming that we don't typecheck a deserialized module, we don't
actually need the ExplicitCompletionHandlerIndex. We used this flag to
determine whether we could trust the number stored in the handler index
or just take the last parameter of the function we're attached to.

Since we only typecheck it before serialization, we can safely check
that the completion handler location is valid before choosing whether we
should trust it or not.
2021-03-13 13:38:15 -08:00
Evan Wilde
9f412616d8 Remove AsyncFunctionName from serialization
We can get the DeclName of the async function from the resolved function
decl itself, so we don't actually need to serialize it. This patch pulls
the declNameRef from the serialization.
2021-03-13 13:38:15 -08:00
Evan Wilde
2b0def501f Replace @hasAsyncAlt with @completionHandlerAsync
This patch replaces the @hasAsyncAlternative attribute with
@completionHandlerAsync. The @completionHandlerAsync attribute takes the
function decl name of the async function and optionally the index of the
completion hander parameter in the function that it's attached to.
If the completion handler index is not provided, it's assumed to be the
last parameter in the parameter list.

We resolve the async function while typechecking the attribute. Before
resolving, we verify that the function the attribute is attached to
isn't async, that it has enough parameters to at least have the
indicated completion handler referenced by the index, that the
completion handler is an escaping non-auto-closure function that returns
Void.

The async function declaration resolution isn't perfect yet, but I want
to get this patch up and we can refine it later. It pulls all of the
delcs with the specified declname in the same context as the
function that the attribute is attached to. Going through that list, it
keeps any that are async functions. If there are none, we emit an error
saying that there are no viable functions, if there are multiple we emit
an error saying that the decl name is ambiguous, and if there is one
function, we keep that as the resolve async function declaration.

This does not take into account the data types of the completion handler
or the async function. There are some complexities to making this
mapping. Here are the pieces:
 - If the completion handler takes a single data type, the async
   function should return that type. (easy case)
 - If the completion handler takes a `Result<T, Error>`, the async
   function is a throwing function that returns a T. (Medium difficulty)
 - If the completion handler looks like `(T?, Error?) -> Void`, we have
   an ambiguous situation between the following async functions:
   - func foo() async throws -> T
   - func foo() async throws -> T?
   That is, we cannot tell whether the `T?` in the completion handler is
   optional because it will be nil on an error, or if it is intended to
   be optional.

This can be done later if it becomes a problem.
2021-03-13 13:38:15 -08:00
Doug Gregor
2a28fed34d Allow one to separately specify the ABI name of a module.
Introduce a new compiler flag `-module-abi-name <name>` that uses the
given name as the ABI name for the module (rather than the module's
name in source code). The ABI name impacts name mangling and metadata.
2021-03-12 07:42:07 -08:00
Slava Pestov
7ccc41a7b7 SIL: Preliminary support for 'apply [noasync]' calls
Refactor SILGen's ApplyOptions into an OptionSet, add a
DoesNotAwait flag to go with DoesNotThrow, and sink it
all down into SILInstruction.h.

Then, replace the isNonThrowing() flag in ApplyInst and
BeginApplyInst with getApplyOptions(), and plumb it
through to TryApplyInst as well.

Set the flag when SILGen emits a sync call to a reasync
function.

When set, this disables the SIL verifier check against
calling async functions from sync functions.

Finally, this allows us to add end-to-end tests for
rdar://problem/71098795.
2021-03-04 22:41:46 -05:00
Ben Barham
ce70727e6f [Deserialization] Output a diagnostic for invalid decls or types
47b068d445 output a diagnostic if a
deserialized decl was invalid (checking `Decl::isInvalid`). It had two
major issues:
  1. It incorrectly output diagnostics for valid modules
  2. It did not catch call invalid declarations

(1) is caused by `isInvalid` falling back to checking the storage for
accessors when the interface type hasn't already been computed (a
fallback to prevent a cycle due to `SimpleDidSetRequest` typechecking
the body). Since the `VarDecl` hasn't finished deserializing, it returns
`true` for its `isInvalid` check (even though it would later return
`false`).

For (2), only `ValueDecl`s would ever be invalid, since other
declarations use a bit in `Decl` to check for validity. As that's not
serialized, those would always be valid in deserialization.

To avoid both these issues, instead output a flag for each declaration
representing whether it is invalid (or not). Read that during
deserialization and output a diagnostic if it is invalid. To be extra
sure that a diagnostic is always output on an error, also output one
when deserializing any `ErrorType`. This ensures that SILGen does not
run when allowing errors (and an error is present), as it is likely to
crash when presented with an invalid AST.

Resolves rdar://74541834
2021-03-03 12:12:20 +10:00
Doug Gregor
451e6cc1f5 [Concurrency] Add support for @GlobalActor(unsafe).
Allow us to tag declarations that are meant to be in a global actor, but
for which we don't yet want to enforce everything. This will be used for
better staging-in of global actor annotations, but for now it's a fancy
way to document @actorIndependent(unsafe).

Stages in the syntax for rdar://74241687 without really implementing it.
2021-02-25 22:41:32 -08:00
Ben Barham
3a7bf2301b [AST] Add a new attribute @hasAsyncAlternative
This attribute marks a function has having an async alternative,
optionally providing the name of that function as a string. Intended to
be used to allow warnings when using a function with an async
alternative in an asynchronous context, to make the async refactorings
more accurate, and for documentation.
2021-02-18 16:18:38 +10:00
Richard Wei
e494df2ee6 [AutoDiff] Add differentiability kind to differentiability witnesses and mangle them.
Differentiability witnesses are now keyed by the original function name, the differentiability kind, and the autodiff config.

Updated SIL syntax:
```
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
sil-differentiability-witness ::=
    'sil_differentiability_witness'
    sil-linkage?
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
    sil-differentiability-witness-body?
sil-instruction ::=
    'differentiability_witness_function'
    '[' sil-differentiability-witness-function-kind ']'
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
```
```console
sil_differentiability_witness [reverse] [parameters 0 1] [results 0] <T where T: Differentiable> @foo : <T> $(T) -> T
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] <T where T: Differentiable> @foo : $(T) -> T
```

New mangling:
```swift
  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
```
```console
$s13test_mangling3fooyS2f_S2ftFWJrSpSr ---> reverse differentiability witness for test_mangling.foo(Swift.Float, Swift.Float, Swift.Float) -> Swift.Float with respect to parameters {0} and results {0}
```

Resolves rdar://74380324.
2021-02-17 18:27:42 -05:00
Slava Pestov
37b69fccca Serialization: Tweak record numbers to make room for 'reasync' 2021-02-15 22:18:54 -05:00
Joe Groff
3edaa70c28 Augment ForeignAsyncConvention with fields for completion handler flag params.
When the requisite support in Clang for `__attribute__((swift_async_error))` parameters
lands, this will let us represent APIs that take completion handlers in the general shape
of `void (^)(BOOL, id, NSError*)`, where the boolean argument indicates the presence of
an error rather than the nilness of the `NSError*` argument.
2021-02-11 13:38:30 -08:00
Evan Wilde
fc41826da9 Rename 'actor class' -> 'actor'
This patch softly updates the spelling of actors from `actor class` to
`actor`. We still accept using `actor` as a modifying attribute of
class, but emit a warning and fix-it to make the change.

One of the challenges that makes this messier is that the modifier list
can be in any order. e.g, `public actor class Foo {}` is the same as
`actor public class Foo {}`.

Classes have been updated to include whether they were explicitly
declared as an actor. This change updates the swiftmodule serialization
version number to 0.591. The additional bit only gets set of the class
declaration was declared as an actor, not if the actor was applied as an
attribute. This allows us to correctly emit `actor class` vs `actor`
emitting the code back out.
2021-02-10 08:05:57 -08:00
Richard Wei
18fe723543 Merge pull request #35811 from rxwei/69980056-differentiable-reverse
[AutoDiff] Add '@differentiable(reverse)'.
2021-02-08 04:32:27 -08:00