Skip to content

Commit 69e97d2

Browse files
authored
Merge branch 'main' into patch-1
2 parents 27e0114 + cfe0b97 commit 69e97d2

File tree

2,985 files changed

+307821
-55687
lines changed

Some content is hidden

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

2,985 files changed

+307821
-55687
lines changed

CHANGELOG.md

Lines changed: 305 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,290 @@ CHANGELOG
33

44
_**Note:** This is in reverse chronological order, so newer entries are added to the top._
55

6+
## Swift 5.7
7+
8+
* [SE-0336][]:
9+
10+
It is now possible to declare `distributed actor` and `distributed func`s inside of them.
11+
12+
Distributed actors provide stronger isolation guarantees than "local" actors, and enable additional checks to be made on return types and parameters of distributed methods, e.g. checking if they conform to `Codable`. Distributed methods can be called on "remote" references of distributed actors, turning those invocations into remote procedure calls, by means of pluggable and user extensible distributed actor system implementations.
13+
14+
Swift does not provide any specific distributed actor system by itself, however, packages in the ecosystem fulfil the role of providing those implementations.
15+
16+
```swift
17+
distributed actor Greeter {
18+
var greetingsSent = 0
19+
20+
distributed func greet(name: String) -> String {
21+
greetingsSent += 1
22+
return "Hello, \(name)!"
23+
}
24+
}
25+
26+
func talkTo(greeter: Greeter) async throws {
27+
// isolation of distributed actors is stronger, it is impossible to refer to
28+
// any stored properties of distributed actors from outside of them:
29+
greeter.greetingsSent // distributed actor-isolated property 'name' can not be accessed from a non-isolated context
30+
31+
// remote calls are implicitly throwing and async,
32+
// to account for the potential networking involved:
33+
let greeting = try await greeter.greet(name: "Alice")
34+
print(greeting) // Hello, Alice!
35+
}
36+
```
37+
38+
* The compiler now emits a warning when a non-final class conforms to a protocol that imposes a same-type requirement between `Self` and an associated type. This is because such a requirement makes the conformance unsound for subclasses.
39+
40+
For example, Swift 5.6 would allow the following code, which at runtime would construct an instanec of `C` and not `SubC` as expected:
41+
42+
```swift
43+
protocol P {
44+
associatedtype A : Q where Self == Self.A.B
45+
}
46+
47+
protocol Q {
48+
associatedtype B
49+
50+
static func getB() -> B
51+
}
52+
53+
class C : P {
54+
typealias A = D
55+
}
56+
57+
class D : Q {
58+
typealias B = C
59+
60+
static func getB() -> C { return C() }
61+
}
62+
63+
extension P {
64+
static func getAB() -> Self {
65+
// This is well-typed, because `Self.A.getB()` returns
66+
// `Self.A.B`, which is equivalent to `Self`.
67+
return Self.A.getB()
68+
}
69+
}
70+
71+
class SubC : C {}
72+
73+
// P.getAB() declares a return type of `Self`, so it should
74+
// return `SubC`, but it actually returns a `C`.
75+
print(SubC.getAB())
76+
```
77+
78+
To make the above example correct, either the class `C` needs to become `final` (in which case `SubC` cannot be declared) or protocol `P` needs to be re-designed to not include the same-type requirement `Self == Self.A.B`.
79+
80+
* [SE-0341][]:
81+
82+
Opaque types can now be used in the parameters of functions and subscripts, wher they provide a shorthand syntax for the introduction of a generic parameter. For example, the following:
83+
84+
```swift
85+
func horizontal(_ v1: some View, _ v2: some View) -> some View {
86+
HStack {
87+
v1
88+
v2
89+
}
90+
}
91+
```
92+
93+
is equivalent to
94+
95+
```swift
96+
func horizontal<V1: View, V2: View>(_ v1: V1, _ v2: V2) -> some View {
97+
HStack {
98+
v1
99+
v2
100+
}
101+
}
102+
```
103+
104+
With this, `some` in a parameter type provides a generalization where the
105+
caller chooses the parameter's type as well as its value, whereas `some` in
106+
the result type provides a generalization where the callee chooses the
107+
resulting type and value.
108+
109+
* The compiler now correctly emits warnings for more kinds of expressions where a protocol conformance is used and may be unavailable at runtime. Previously, member reference expressions and type erasing expressions that used potentially unavailable conformances were not diagnosed, leading to potential crashes at runtime.
110+
111+
```swift
112+
struct Pancake {}
113+
protocol Food {}
114+
115+
extension Food {
116+
var isGlutenFree: Bool { false }
117+
}
118+
119+
@available(macOS 12.0, *)
120+
extension Pancake: Food {}
121+
122+
@available(macOS 11.0, *)
123+
func eatPancake(_ pancake: Pancake) {
124+
if (pancake.isGlutenFree) { // warning: conformance of 'Pancake' to 'Food' is only available in macOS 12.0 or newer
125+
eatFood(pancake) // warning: conformance of 'Pancake' to 'Food' is only available in macOS 12.0 or newer
126+
}
127+
}
128+
129+
func eatFood(_ food: Food) {}
130+
```
131+
132+
* [SE-0328][]:
133+
134+
Opaque types (expressed with 'some') can now be used in structural positions
135+
within a result type, including having multiple opaque types in the same
136+
result. For example:
137+
138+
```swift
139+
func getSomeDictionary() -> [some Hashable: some Codable] {
140+
return [ 1: "One", 2: "Two" ]
141+
}
142+
```
6143
Swift 5.6
7144
---------
8145

146+
* [SE-0327][]:
147+
148+
In Swift 5 mode, a warning is now emitted if the default-value expression of an
149+
instance-member property requires global-actor isolation. For example:
150+
151+
```swift
152+
@MainActor
153+
func partyGenerator() -> [PartyMember] { fatalError("todo") }
154+
155+
class Party {
156+
@MainActor var members: [PartyMember] = partyGenerator()
157+
// ^~~~~~~~~~~~~~~~
158+
// warning: expression requiring global actor 'MainActor' cannot
159+
// appear in default-value expression of property 'members'
160+
}
161+
```
162+
163+
Previously, the isolation granted by the type checker matched the isolation of
164+
the property itself, but at runtime that is not guaranteed. In Swift 6,
165+
such default-value expressions will become an error if they require isolation.
166+
167+
* Actor isolation checking now understands that `defer` bodies share the isolation of their enclosing function.
168+
169+
```swift
170+
// Works on global actors
171+
@MainActor
172+
func runAnimation(controller: MyViewController) async {
173+
controller.hasActiveAnimation = true
174+
defer { controller.hasActiveAnimation = false }
175+
176+
// do the animation here...
177+
}
178+
179+
// Works on actor instances
180+
actor OperationCounter {
181+
var activeOperationCount = 0
182+
183+
func operate() async {
184+
activeOperationCount += 1
185+
defer { activeOperationCount -= 1 }
186+
187+
// do work here...
188+
}
189+
}
190+
```
191+
192+
* [SE-0335][]:
193+
194+
Swift now allows existential types to be explicitly written with the `any`
195+
keyword, creating a syntactic distinction between existential types and
196+
protocol conformance constraints. For example:
197+
198+
```swift
199+
protocol P {}
200+
201+
func generic<T>(value: T) where T: P {
202+
...
203+
}
204+
205+
func existential(value: any P) {
206+
...
207+
}
208+
```
209+
210+
* [SE-0337][]:
211+
212+
Swift now provides an incremental migration path to data race safety, allowing
213+
APIs to adopt concurrency without breaking their clients that themselves have
214+
not adopted concurrency. An existing declaration can introduce
215+
concurrency-related annotations (such as making its closure parameters
216+
`@Sendable`) and use the `@preconcurrency` attribute to maintain its behavior
217+
for clients who have not themselves adopted concurrency:
218+
219+
```swift
220+
// module A
221+
@preconcurrency func runOnSeparateTask(_ workItem: @Sendable () -> Void)
222+
223+
// module B
224+
import A
225+
226+
class MyCounter {
227+
var value = 0
228+
}
229+
230+
func doesNotUseConcurrency(counter: MyCounter) {
231+
runOnSeparateTask {
232+
counter.value += 1 // no warning, because this code hasn't adopted concurrency
233+
}
234+
}
235+
236+
func usesConcurrency(counter: MyCounter) async {
237+
runOnSeparateTask {
238+
counter.value += 1 // warning: capture of non-Sendable type 'MyCounter'
239+
}
240+
}
241+
```
242+
243+
One can enable warnings about data race safety within a module with the
244+
`-warn-concurrency` compiler option. When using a module that does not yet
245+
provide `Sendable` annotations, one can suppress warnings for types from that
246+
module by marking the import with `@preconcurrency`:
247+
248+
```swift
249+
/// module C
250+
public struct Point {
251+
public var x, y: Double
252+
}
253+
254+
// module D
255+
@preconcurrency import C
256+
257+
func centerView(at location: Point) {
258+
Task {
259+
await mainView.center(at: location) // no warning about non-Sendable 'Point' because the @preconcurrency import suppresses it
260+
}
261+
}
262+
```
263+
264+
* [SE-0302][]:
265+
266+
Swift will now produce warnings to indicate potential data races when
267+
non-`Sendable` types are passed across actor or task boundaries. For
268+
example:
269+
270+
```swift
271+
class MyCounter {
272+
var value = 0
273+
}
274+
275+
func f() -> MyCounter {
276+
let counter = MyCounter()
277+
Task {
278+
counter.value += 1 // warning: capture of non-Sendable type 'MyCounter'
279+
}
280+
return counter
281+
}
282+
```
283+
284+
* [SE-0331][]:
285+
286+
The conformance of the unsafe pointer types (e.g., `UnsafePointer`,
287+
`UnsafeMutableBufferPointer`) to the `Sendable` protocols has been removed,
288+
because pointers cannot safely be transferred across task or actor boundaries.
289+
9290
* References to `Self` or so-called "`Self` requirements" in the type signatures
10291
of protocol members are now correctly detected in the parent of a nested type.
11292
As a result, protocol members that fall under this overlooked case are no longer
@@ -26,7 +307,7 @@ Swift 5.6
26307
// protocol type (use a generic constraint instead).
27308
_ = p.method
28309
}
29-
```
310+
```
30311

31312
* [SE-0324][]:
32313

@@ -53,6 +334,19 @@ Swift 5.6
53334
}
54335
```
55336

337+
* [SE-0322][]:
338+
339+
The standard library now provides a new operation
340+
`withUnsafeTemporaryAllocation` which provides an efficient temporarily
341+
allocation within a limited scope, which will be optimized to use stack
342+
allocation when possible.
343+
344+
* [SE-0320][]:
345+
346+
Dictionaries with keys of any type conforming to the new protocol
347+
`CodingKeyRepresentable` can now be encoded and decoded. Formerly, encoding
348+
and decoding was limited to keys of type `String` or `Int`.
349+
56350
* [SE-0315][]:
57351

58352
Type expressions and annotations can now include "type placeholders" which
@@ -8753,14 +9047,24 @@ Swift 1.0
87539047
[SE-0298]: <https://github.com/apple/swift-evolution/blob/main/proposals/0298-asyncsequence.md>
87549048
[SE-0299]: <https://github.com/apple/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md>
87559049
[SE-0300]: <https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
9050+
[SE-0302]: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
87569051
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
87579052
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
87589053
[SE-0311]: <https://github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
87599054
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
87609055
[SE-0315]: <https://github.com/apple/swift-evolution/blob/main/proposals/0315-placeholder-types.md>
87619056
[SE-0316]: <https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md>
9057+
[SE-0320]: <https://github.com/apple/swift-evolution/blob/main/proposals/0320-codingkeyrepresentable.md>
9058+
[SE-0322]: <https://github.com/apple/swift-evolution/blob/main/proposals/0322-temporary-buffers.md>
87629059
[SE-0324]: <https://github.com/apple/swift-evolution/blob/main/proposals/0324-c-lang-pointer-arg-conversion.md>
87639060
[SE-0323]: <https://github.com/apple/swift-evolution/blob/main/proposals/0323-async-main-semantics.md>
9061+
[SE-0327]: <https://github.com/apple/swift-evolution/blob/main/proposals/0327-actor-initializers.md>
9062+
[SE-0328]: <https://github.com/apple/swift-evolution/blob/main/proposals/0328-structural-opaque-result-types.md>
9063+
[SE-0331]: <https://github.com/apple/swift-evolution/blob/main/proposals/0331-remove-sendable-from-unsafepointer.md>
9064+
[SE-0337]: <https://github.com/apple/swift-evolution/blob/main/proposals/0337-support-incremental-migration-to-concurrency-checking.md>
9065+
[SE-0335]: <https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md>
9066+
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
9067+
[SE-0336]: <https://github.com/apple/swift-evolution/blob/main/proposals/0336-distributed-actor-isolation.md>
87649068

87659069
[SR-75]: <https://bugs.swift.org/browse/SR-75>
87669070
[SR-106]: <https://bugs.swift.org/browse/SR-106>

0 commit comments

Comments
 (0)