Skip to content

Commit d26aa55

Browse files
Merge remote-tracking branch 'apple/release/5.7' into swiftwasm-release/5.7
2 parents a034333 + fc4f64c commit d26aa55

File tree

184 files changed

+6610
-1081
lines changed

Some content is hidden

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

184 files changed

+6610
-1081
lines changed

CHANGELOG.md

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* The Swift compiler no longer warns about redundant requirements in generic declarations. For example,
9+
the following code diagnosed a warning in Swift 5.6 about the `T.Iterator : IteratorProtocol`
10+
requirement being redundant, because it is implied by `T : Sequence`:
11+
12+
```swift
13+
func firstElement<T: Sequence>(_: T) -> T.Element where T.Iterator: IteratorProtocol {...}
14+
```
15+
16+
A redundant requirement does not indicate a coding error, and sometimes it is desirable to spell them
17+
out for documentation purposes. For this reason these warnings are now disabled by default.
18+
19+
To restore the previous behavior, pass the `-Xfrontend -warn-redundant-requirements`
20+
compiler flag.
21+
822
* [SE-0338][]:
923

1024
Non-isolated async functions now always execute on the global concurrent pool,
@@ -38,33 +52,54 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
3852
pool, `Sendable` checking will be performed, so the compiler will emit a
3953
diagnostic in the call to `f` if `c` is not of `Sendable` type.
4054

41-
* [SE-0353][]:
55+
* [SE-0350][]:
4256

43-
Protocols with primary associated types can now be used in existential types,
44-
enabling same-type constraints on those associated types.
57+
The standard library has a new `Regex<Output>` type.
4558

46-
```
47-
let strings: any Collection<String> = [ "Hello" ]
59+
This type represents an _extended regular expression_, allowing more fluent
60+
string processing operations. A `Regex` may be created by
61+
[initialization from a string][SE-0355]:
62+
63+
```swift
64+
let pattern = "a[bc]+" // matches "a" followed by one or more instances
65+
// of either "b" or "c"
66+
let regex = try! Regex(pattern)
4867
```
4968

50-
Note that language features requiring runtime support like dynamic casts
51-
(`is`, `as?`, `as!`), as well as generic usages of parameterized existentials
52-
in generic types (e.g. `Array<any Collection<Int>>`) involve additional
53-
availability checks to use. Back-deploying usages in generic position can be
54-
worked around with a generic type-erasing wrapper struct, which is now much
55-
simpler to implement:
69+
Or via a [regex literal][SE-0354]:
5670

5771
```swift
58-
struct AnyCollection<T> {
59-
var wrapped: any Collection<T>
60-
}
61-
62-
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
72+
let regex = #/a[bc]+/#
6373
```
64-
74+
75+
In Swift 6, `/` will also be supported as a delimiter for `Regex` literals.
76+
You can enable this mode in Swift 5.7 with the `-enable-bare-slash-regex`
77+
flag. Doing so will cause some existing expressions that use `/` as an
78+
operator to no longer compile; you can add parentheses or line breaks as a
79+
workaround.
80+
81+
There are [new string-processing algorithms][SE-0357] that support
82+
`String`, `Regex` and arbitrary `Collection` types.
83+
6584
* [SE-0329][]:
6685
New types representing time and clocks were introduced. This includes a protocol `Clock` defining clocks which allow for defining a concept of now and a way to wake up after a given instant. Additionally a new protocol `InstantProtocol` for defining instants in time was added. Furthermore a new protocol `DurationProtocol` was added to define an elapsed duration between two given `InstantProtocol` types. Most commonly the `Clock` types for general use are the `SuspendingClock` and `ContinuousClock` which represent the most fundamental clocks for the system. The `SuspendingClock` type does not progress while the machine is suspended whereas the `ContinuousClock` progresses no matter the state of the machine.
6786

87+
```swift
88+
func delayedHello() async throws {
89+
try await Task.sleep(until: .now + .milliseconds(123), clock: .continuous)
90+
print("hello delayed world")
91+
}
92+
```
93+
94+
`Clock` also has methods to measure the elapsed duration of the execution of work. In the case of the `SuspendingClock` and `ContinuousClock` this measures with high resolution and is suitable for benchmarks.
95+
96+
```swift
97+
let clock = ContinuousClock()
98+
let elapsed = clock.measure {
99+
someLongRunningWork()
100+
}
101+
```
102+
68103
* [SE-0309][]:
69104

70105
Protocols with associated types and `Self` requirements can now be used as the
@@ -75,21 +110,91 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
75110
`any` type having the same constraints as the associated type. For example:
76111

77112
```swift
78-
func delayedHello() async throws {
79-
try await Task.sleep(until: .now + .milliseconds(123), clock: .continuous)
80-
print("hello delayed world")
113+
protocol Surface {...}
114+
115+
protocol Solid {
116+
associatedtype SurfaceType: Surface
117+
func boundary() -> SurfaceType
118+
}
119+
120+
let solid: any Solid = ...
121+
122+
// Type of 'boundary' is 'any Surface'
123+
let boundary = solid.boundary()
124+
```
125+
126+
Protocol methods that take an associated type or `Self` cannot be used with `any`,
127+
however in conjunction with [SE-0352][], you can pass the `any` type to a function
128+
taking a generic parameter constrained to the protocol. Within the generic context,
129+
type relationships are explicit and all protocol methods can be used.
130+
131+
* [SE-0346][]:
132+
133+
Protocols can now declare a list of one or more _primary associated types_, which enable writing same-type requirements on those associated types using angle bracket syntax:
134+
135+
```swift
136+
protocol Graph<Vertex, Edge> {
137+
associatedtype Vertex
138+
associatedtype Edge
81139
}
82140
```
83141

84-
`Clock` also has methods to measure the elapsed duration of the execution of work. In the case of the `SuspendingClock` and `ContinuousClock` this measures with high resolution and is suitable for benchmarks.
142+
You can now write a protocol name followed by type arguments in angle brackets, like
143+
`Graph<Int, String>`, anywhere that a protocol conformance requirement may appear:
85144

86145
```swift
87-
let clock = ContinuousClock()
88-
let elapsed = clock.measure {
89-
someLongRunningWork()
146+
func shortestPath<V, E>(_: some Graph<V, E>, from: V, to: V) -> [E]
147+
148+
extension Graph<Int, String> {...}
149+
150+
func build() -> some Graph<Int, String> {}
151+
```
152+
153+
A protocol name followed by angle brackets is shorthand for a conformance requirement,
154+
together with a same-type requirement for the protocol's primary associated types.
155+
The first two examples above are equivalent to the following:
156+
157+
```swift
158+
func shortestPath<V, E, G>(_: G, from: V, to: V) -> [E]
159+
where G: Graph, G.Vertex == V, G.Edge == E
160+
161+
extension Graph where Vertex == Int, Edge == String {...}
162+
```
163+
164+
The `build()` function returning `some Graph<Int, String>` can't be written using a
165+
`where` clause; this is an example of a constrained opaque result type, which is new expressivity in Swift 5.7.
166+
167+
* [SE-0353][]:
168+
169+
Protocols with primary associated types can now be used in existential types,
170+
enabling same-type constraints on those associated types.
171+
172+
```swift
173+
let strings: any Collection<String> = [ "Hello" ]
174+
```
175+
176+
Note that language features requiring runtime support like dynamic casts
177+
(`is`, `as?`, `as!`), as well as generic usages of parameterized existentials
178+
in generic types (e.g. `Array<any Collection<Int>>`) involve additional
179+
availability checks to use. Back-deploying usages in generic position can be
180+
worked around with a generic type-erasing wrapper struct, which is now much
181+
simpler to implement:
182+
183+
```swift
184+
struct AnyCollection<T> {
185+
var wrapped: any Collection<T>
90186
}
187+
188+
let arrayOfCollections: [AnyCollection<T>] = [ /**/ ]
91189
```
92190

191+
* [SE-0358][]:
192+
193+
Various protocols in the standard library now declare primary associated types, for
194+
example `Sequence` and `Collection` declare a single primary associated type `Element`.
195+
For example, this allows writing down the types `some Collection<Int>` and
196+
`any Collection<Int>`.
197+
93198
* References to `optional` methods on a protocol metatype, as well as references to dynamically looked up methods on `AnyObject` are now supported on par with other function references. The type of such a reference (formerly an immediate optional by mistake) has been altered to that of a function that takes a single argument and returns an optional value of function type:
94199

95200
```swift
@@ -189,7 +294,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
189294
in places that would previously fail because `any` types do not conform
190295
to their protocols. For example:
191296

192-
```
297+
```swift
193298
protocol P {
194299
associatedtype A
195300
func getA() -> A
@@ -407,12 +512,12 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
407512
struct S {
408513
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
409514
lazy var a: Int = 42
410-
515+
411516
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
412517
@Wrapper var b: Int
413518
}
414519
```
415-
520+
416521
* 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.
417522

418523
```swift
@@ -438,7 +543,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
438543

439544
* [SE-0328][]:
440545

441-
Opaque types (expressed with 'some') can now be used in structural positions
546+
Opaque types (expressed with `some`) can now be used in structural positions
442547
within a result type, including having multiple opaque types in the same
443548
result. For example:
444549

@@ -9353,6 +9458,7 @@ Swift 1.0
93539458
[SE-0300]: <https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md>
93549459
[SE-0302]: <https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md>
93559460
[SE-0306]: <https://github.com/apple/swift-evolution/blob/main/proposals/0306-actors.md>
9461+
[SE-0309]: <https://github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md>
93569462
[SE-0310]: <https://github.com/apple/swift-evolution/blob/main/proposals/0310-effectful-readonly-properties.md>
93579463
[SE-0311]: <https://github.com/apple/swift-evolution/blob/main/proposals/0311-task-locals.md>
93589464
[SE-0313]: <https://github.com/apple/swift-evolution/blob/main/proposals/0313-actor-isolation-control.md>
@@ -9377,9 +9483,16 @@ Swift 1.0
93779483
[SE-0341]: <https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md>
93789484
[SE-0343]: <https://github.com/apple/swift-evolution/blob/main/proposals/0343-top-level-concurrency.md>
93799485
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
9486+
[SE-0346]: <https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md>
93809487
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
93819488
[SE-0349]: <https://github.com/apple/swift-evolution/blob/main/proposals/0349-unaligned-loads-and-stores.md>
9489+
[SE-0350]: <https://github.com/apple/swift-evolution/blob/main/proposals/0350-regex-type-overview.md>
93829490
[SE-0352]: <https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md>
9491+
[SE-0353]: <https://github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md>
9492+
[SE-0354]: <https://github.com/apple/swift-evolution/blob/main/proposals/0354-regex-literals.md>
9493+
[SE-0355]: <https://github.com/apple/swift-evolution/blob/main/proposals/0355-regex-syntax-run-time-construction.md>
9494+
[SE-0357]: <https://github.com/apple/swift-evolution/blob/main/proposals/0357-regex-string-processing-algorithms.md>
9495+
[SE-0358]: <https://github.com/apple/swift-evolution/blob/main/proposals/0358-primary-associated-types-in-stdlib.md>
93839496

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

0 commit comments

Comments
 (0)