Skip to content

Commit b0b7bcd

Browse files
Merge pull request #3346 from swiftwasm/katei/merge-release-5.5-2021-08-07
2 parents 0317ee6 + 2ac13cb commit b0b7bcd

File tree

1,260 files changed

+46397
-59155
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,260 files changed

+46397
-59155
lines changed

CHANGELOG.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,204 @@ CHANGELOG
2929
Swift 5.5
3030
---------
3131

32+
* [SE-0313][]:
33+
34+
Parameters of actor type can be declared as `isolated`, which means that they
35+
represent the actor on which that code will be executed. `isolated` parameters
36+
extend the actor-isolated semantics of the `self` parameter of actor methods
37+
to arbitrary parameters. For example:
38+
39+
```swift
40+
actor MyActor {
41+
func f() { }
42+
}
43+
44+
func g(actor: isolated MyActor) {
45+
actor.f() // okay, this code is always executing on "actor"
46+
}
47+
48+
func h(actor: MyActor) async {
49+
g(actor: actor) // error, call must be asynchronous
50+
await g(actor: actor) // okay, hops to "actor" before calling g
51+
}
52+
```
53+
54+
The `self` parameter of actor methods are implicitly `isolated`. The
55+
`nonisolated` keyword makes the `self` parameter no longer `isolated`.
56+
57+
* [SR-14731][]:
58+
59+
The compiler now correctly rejects the application of generic arguments to the
60+
special `Self` type:
61+
62+
```swift
63+
struct Box<T> {
64+
// previously interpreted as a return type of Box<T>, ignoring the <Int> part;
65+
// now we diagnose an error with a fix-it suggesting replacing `Self` with `Box`
66+
static func makeBox() -> Self<Int> {...}
67+
}```
68+
69+
* [SR-14878][]:
70+
71+
The compiler now correctly rejects `@available` annotations on enum cases with
72+
associated values with an OS version newer than the current deployment target:
73+
74+
```swift
75+
@available(macOS 12, *)
76+
public struct Crayon {}
77+
78+
public enum Pen {
79+
case pencil
80+
81+
@available(macOS 12, *)
82+
case crayon(Crayon)
83+
}
84+
```
85+
86+
While this worked with some examples, there is no way for the Swift runtime to
87+
perform the requisite dynamic layout needed to support this in general, which
88+
could cause crashes at runtime.
89+
90+
Note that conditional availability on stored properties in structs and classes
91+
is not supported for similar reasons; it was already correctly detected and
92+
diagnosed.
93+
94+
* [SE-0316][]:
95+
96+
A type can be defined as a global actor. Global actors extend the notion
97+
of actor isolation outside of a single actor type, so that global state
98+
(and the functions that access it) can benefit from actor isolation,
99+
even if the state and functions are scattered across many different
100+
types, functions and modules. Global actors make it possible to safely
101+
work with global variables in a concurrent program, as well as modeling
102+
other global program constraints such as code that must only execute on
103+
the "main thread" or "UI thread". A new global actor can be defined with
104+
the `globalActor` attribute:
105+
106+
```swift
107+
@globalActor
108+
struct DatabaseActor {
109+
actor ActorType { }
110+
111+
static let shared: ActorType = ActorType()
112+
}
113+
```
114+
115+
Global actor types can be used as custom attributes on various declarations,
116+
which ensures that those declarations are only accessed on the actor described
117+
by the global actor's `shared` instance. For example:
118+
119+
```swift
120+
@DatabaseActor func queryDB(query: Query) throws -> QueryResult
121+
122+
func runQuery(queryString: String) async throws -> QueryResult {
123+
let query = try Query(parsing: queryString)
124+
return try await queryDB(query: query) // 'await' because this implicitly hops to DatabaseActor.shared
125+
}
126+
```
127+
128+
The concurrency library defines one global actor, `MainActor`, which
129+
represents the main thread of execution. It should be used for any code that
130+
must execute on the main thread, e.g., for updating UI.
131+
132+
* [SE-0313][]:
133+
134+
Declarations inside an actor that would normally be actor-isolated can
135+
explicitly become non-isolated using the `nonisolated` keyword. Non-isolated
136+
declarations can be used to conform to synchronous protocol requirements:
137+
138+
```swift
139+
actor Account: Hashable {
140+
let idNumber: Int
141+
let balance: Double
142+
143+
nonisolated func hash(into hasher: inout Hasher) { // okay, non-isolated satisfies synchronous requirement
144+
hasher.combine(idNumber) // okay, can reference idNumber from outside the let
145+
hasher.combine(balance) // error: cannot synchronously access actor-isolated property
146+
}
147+
}
148+
```
149+
150+
* Type names are no longer allowed as an argument to a subscript parameter that expects a metatype type
151+
152+
```swift
153+
struct MyValue {
154+
}
155+
156+
struct MyStruct {
157+
subscript(a: MyValue.Type) -> Int { get { ... } }
158+
}
159+
160+
func test(obj: MyStruct) {
161+
let _ = obj[MyValue]
162+
}
163+
```
164+
165+
Accepting subscripts with `MyValue` as an argument was an oversight because `MyValue` requires explicit `.self`
166+
to reference its metatype, so correct syntax would be to use `obj[MyValue.self]`.
167+
168+
* [SE-0310][]:
169+
170+
Read-only computed properties and subscripts can now define their `get` accessor to be `async` and/or `throws`, by writing one or both of those keywords between the `get` and `{`. Thus, these members can now make asynchronous calls or throw errors in the process of producing a value:
171+
```swift
172+
class BankAccount: FinancialAccount {
173+
var manager: AccountManager?
174+
175+
var lastTransaction: Transaction {
176+
get async throws {
177+
guard manager != nil else { throw BankError.notInYourFavor }
178+
return await manager!.getLastTransaction()
179+
}
180+
}
181+
182+
subscript(_ day: Date) -> [Transaction] {
183+
get async {
184+
return await manager?.getTransactions(onDay: day) ?? []
185+
}
186+
}
187+
}
188+
189+
protocol FinancialAccount {
190+
associatedtype T
191+
var lastTransaction: T { get async throws }
192+
subscript(_ day: Date) -> [T] { get async }
193+
}
194+
```
195+
Accesses to such members, like `lastTransaction` above, will require appropriate marking with `await` and/or `try`:
196+
```swift
197+
extension BankAccount {
198+
func meetsTransactionLimit(_ limit: Amount) async -> Bool {
199+
return try! await self.lastTransaction.amount < limit
200+
// ^~~~~~~~~~~~~~~~ this access is async & throws
201+
}
202+
}
203+
204+
205+
func hadWithdrawlOn(_ day: Date, from acct: BankAccount) async -> Bool {
206+
return await !acct[day].allSatisfy { $0.amount >= Amount.zero }
207+
// ^~~~~~~~~ this access is async
208+
}
209+
```
210+
211+
* [SE-0306][]:
212+
213+
Swift 5.5 includes support for actors, a new kind of type that isolates its instance data to protect it from concurrent access. Accesses to an actor's instance declarations from outside the must be asynchronous:
214+
215+
```swift
216+
actor Counter {
217+
var value = 0
218+
219+
func increment() {
220+
value = value + 1
221+
}
222+
}
223+
224+
func useCounter(counter: Counter) async {
225+
print(await counter.value) // interaction must be async
226+
await counter.increment() // interaction must be async
227+
}
228+
```
229+
32230
* The determination of whether a call to a `rethrows` function can throw now considers default arguments of `Optional` type.
33231

34232
In Swift 5.4, such default arguments were ignored entirely by `rethrows` checking. This meant that the following example was accepted:
@@ -8434,6 +8632,10 @@ Swift 1.0
84348632
[SE-0297]: <https://github.com/apple/swift-evolution/blob/main/proposals/0297-concurrency-objc.md>
84358633
[SE-0298]: <https://github.com/apple/swift-evolution/blob/main/proposals/0298-asyncsequence.md>
84368634
[SE-0299]: <https://github.com/apple/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md>
8635+
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
8636+
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
8637+
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
8638+
[SE-0316]: <https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md>
84378639

84388640
[SR-75]: <https://bugs.swift.org/browse/SR-75>
84398641
[SR-106]: <https://bugs.swift.org/browse/SR-106>
@@ -8472,3 +8674,5 @@ Swift 1.0
84728674
[SR-11429]: <https://bugs.swift.org/browse/SR-11429>
84738675
[SR-11700]: <https://bugs.swift.org/browse/SR-11700>
84748676
[SR-11841]: <https://bugs.swift.org/browse/SR-11841>
8677+
[SR-14731]: <https://bugs.swift.org/browse/SR-14731>
8678+
[SR-14878]: <https://bugs.swift.org/browse/SR-14878>

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,12 @@ option(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING
408408
"Enable experimental Swift differentiable programming features"
409409
FALSE)
410410

411+
option(SWIFT_IMPLICIT_CONCURRENCY_IMPORT
412+
"Implicitly import the Swift concurrency module"
413+
TRUE)
414+
411415
option(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY
412-
"Enable experimental Swift concurrency model"
416+
"Enable build of the Swift concurrency module"
413417
FALSE)
414418

415419
option(SWIFT_ENABLE_DISPATCH
@@ -462,7 +466,9 @@ endif()
462466

463467
set(SWIFT_BUILD_HOST_DISPATCH FALSE)
464468
if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
465-
if(SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT)
469+
# Only build libdispatch for the host if the host tools are being built and
470+
# specifically if these two libraries that depend on it are built.
471+
if(SWIFT_INCLUDE_TOOLS AND (SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT))
466472
set(SWIFT_BUILD_HOST_DISPATCH TRUE)
467473
endif()
468474

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @apple/swift5-branch-managers

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function(add_swift_unittest test_dirname)
4242
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
4343
# Add an @rpath to the swift library directory.
4444
set_target_properties(${test_dirname} PROPERTIES
45-
BUILD_RPATH ${SWIFT_LIBRARY_OUTPUT_INTDIR}/swift/macosx)
45+
BUILD_RPATH "${SWIFT_LIBRARY_OUTPUT_INTDIR}/swift/macosx;${SWIFT_DARWIN_STDLIB_INSTALL_NAME_DIR}")
4646
# Force all the swift libraries to be found via rpath.
4747
add_custom_command(TARGET "${test_dirname}" POST_BUILD
4848
COMMAND "${SWIFT_SOURCE_DIR}/utils/swift-rpathize.py"

docs/ABI/Mangling.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ types where the metadata itself has unknown layout.)
251251
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk
252252
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk (obsolete)
253253

254+
global ::= reabstraction-thunk type 'TU' // reabstraction thunk with global actor constraint
255+
254256
The `from-type` and `to-type` in a reabstraction thunk helper function
255257
are always non-polymorphic ``<impl-function-type>`` types.
256258

@@ -464,6 +466,7 @@ Types
464466
KNOWN-TYPE-KIND ::= 'a' // Swift.Array
465467
KNOWN-TYPE-KIND ::= 'B' // Swift.BinaryFloatingPoint
466468
KNOWN-TYPE-KIND ::= 'b' // Swift.Bool
469+
KNOWN-TYPE-KIND ::= 'c' KNOWN-TYPE-KIND-2 // Second set of standard types
467470
KNOWN-TYPE-KIND ::= 'D' // Swift.Dictionary
468471
KNOWN-TYPE-KIND ::= 'd' // Swift.Float64
469472
KNOWN-TYPE-KIND ::= 'E' // Swift.Encodable
@@ -509,6 +512,25 @@ Types
509512
KNOWN-TYPE-KIND ::= 'Z' // Swift.SignedInteger
510513
KNOWN-TYPE-KIND ::= 'z' // Swift.BinaryInteger
511514

515+
KNOWN-TYPE-KIND-2 ::= 'A' // Swift.Actor
516+
KNOWN-TYPE-KIND-2 ::= 'C' // Swift.CheckedContinuation
517+
KNOWN-TYPE-KIND-2 ::= 'c' // Swift.UnsafeContinuation
518+
KNOWN-TYPE-KIND-2 ::= 'E' // Swift.CancellationError
519+
KNOWN-TYPE-KIND-2 ::= 'e' // Swift.UnownedSerialExecutor
520+
KNOWN-TYPE-KIND-2 ::= 'F' // Swift.Executor
521+
KNOWN-TYPE-KIND-2 ::= 'f' // Swift.SerialExecutor
522+
KNOWN-TYPE-KIND-2 ::= 'G' // Swift.TaskGroup
523+
KNOWN-TYPE-KIND-2 ::= 'g' // Swift.ThrowingTaskGroup
524+
KNOWN-TYPE-KIND-2 ::= 'I' // Swift.AsyncIteratorProtocol
525+
KNOWN-TYPE-KIND-2 ::= 'i' // Swift.AsyncSequence
526+
KNOWN-TYPE-KIND-2 ::= 'J' // Swift.UnownedJob
527+
KNOWN-TYPE-KIND-2 ::= 'M' // Swift.MainActor
528+
KNOWN-TYPE-KIND-2 ::= 'P' // Swift.TaskPriority
529+
KNOWN-TYPE-KIND-2 ::= 'S' // Swift.AsyncStream
530+
KNOWN-TYPE-KIND-2 ::= 's' // Swift.AsyncThrowingStream
531+
KNOWN-TYPE-KIND-2 ::= 'T' // Swift.Task
532+
KNOWN-TYPE-KIND-2 ::= 't' // Swift.UnsafeCurrentTask
533+
512534
protocol ::= context decl-name
513535
protocol ::= standard-substitutions
514536

@@ -567,7 +589,7 @@ Types
567589
C-TYPE is mangled according to the Itanium ABI, and prefixed with the length.
568590
Non-ASCII identifiers are preserved as-is; we do not use Punycode.
569591

570-
function-signature ::= params-type params-type async? sendable? throws? differentiable? // results and parameters
592+
function-signature ::= params-type params-type async? sendable? throws? differentiable? global-actor? // results and parameters
571593

572594
params-type ::= type 'z'? 'h'? // tuple in case of multiple parameters or a single parameter with a single tuple type
573595
// with optional inout convention, shared convention. parameters don't have labels,
@@ -577,6 +599,7 @@ Types
577599
#if SWIFT_RUNTIME_VERSION >= 5.5
578600
async ::= 'Ya' // 'async' annotation on function types
579601
sendable ::= 'Yb' // @Sendable on function types
602+
global-actor :: = type 'Yc' // Global actor on function type
580603
#endif
581604
throws ::= 'K' // 'throws' annotation on function types
582605
differentiable ::= 'Yjf' // @differentiable(_forward) on function type
@@ -588,7 +611,7 @@ Types
588611
type-list ::= empty-list
589612

590613
// FIXME: Consider replacing 'h' with a two-char code
591-
list-type ::= type identifier? 'Yk'? 'z'? 'h'? 'n'? 'd'? // type with optional label, '@noDerivative', inout convention, shared convention, owned convention, and variadic specifier
614+
list-type ::= type identifier? 'Yk'? 'z'? 'h'? 'n'? 'Yi'? 'd'? // type with optional label, '@noDerivative', inout convention, shared convention, owned convention, actor 'isolated', and variadic specifier
592615

593616
METATYPE-REPR ::= 't' // Thin metatype representation
594617
METATYPE-REPR ::= 'T' // Thick metatype representation

docs/Diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Clang also has a kind of diagnostic called a "remark", which represents informat
5151
- "...to silence this warning"
5252
- "...here" (for a purely locational note)
5353

54+
- If possible, it is best to include the name of the type or function that has the error, e.g. "non-actor type 'Nope' cannot ..." is better than "non-actor type cannot ...". It helps developers relate the error message to the specific type the error is about, even if the error would highlight the appropriate line / function in other ways.
5455

5556
### Locations and Highlights ###
5657

0 commit comments

Comments
 (0)