Skip to content

Commit b2fd75d

Browse files
committed
Document a couple of miscallaneous 5.4 changes in CHANGELOG.md
1 parent f73a6b3 commit b2fd75d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,88 @@ CHANGELOG
2828
Swift 5.4
2929
---------
3030

31+
* Protocol conformance checking now considers `where` clauses when evaluating if a `typealias` is a suitable witness for an associated type requirement. The following code is now rejected:
32+
33+
```swift
34+
protocol Holder {
35+
associatedtype Contents
36+
}
37+
38+
struct Box<T> : Holder {}
39+
// error: type 'Box<T>' does not conform to protocol 'Holder'
40+
41+
extension Box where T : Hashable {
42+
typealias Contents = T
43+
}
44+
```
45+
46+
In most cases, the compiler would either crash or produce surprising results when making use of a `typealias` with an unsatisfied `where` clause, but it is possible that some previously-working code is now rejected. In the above example, the conformance can be fixed in one of various ways:
47+
48+
1) making it conditional (moving the `: Holder` from the definition of `Box` to the extension)
49+
2) moving the `typealias` from the extension to the type itself
50+
3) relaxing the `where` clause on the extension
51+
52+
* Availability checking now rejects protocols that refine less available protocols. Previously, this was accepted by the compiler but could result in linker errors or runtime crashes:
53+
54+
```swift
55+
@available(macOS 11, *)
56+
protocol Base {}
57+
58+
protocol Bad : Base {}
59+
// error: 'Base' is only available in macOS 11 or newer
60+
61+
@available(macOS 11, *)
62+
protocol Good : Base {} // OK
63+
```
64+
65+
* The `@available` attribute is no longer permitted on generic parameters, where it had no effect:
66+
67+
```swift
68+
struct Bad<@available(macOS 11, *) T> {}
69+
// error: '@available' attribute cannot be applied to this declaration
70+
71+
struct Good<T> {} // equivalent
72+
```
73+
74+
* If a type is made to conform to a protocol via an extension, the availability of the extension is now taken into account when forming generic types that use this protocol conformance. For example, consider a `Box` type whose conformance to `Hashable` uses features only available on macOS 11:
75+
76+
```swift
77+
public struct Box {}
78+
79+
@available(macOS 11, *)
80+
extension Box : Hashable {
81+
func hash(into: inout Hasher) {
82+
// call some new API to hash the value...
83+
}
84+
}
85+
86+
public func findBad(_: Set<Box>) -> Box {}
87+
// warning: conformance of 'Box' to 'Hashable' is only available in macOS 11 or newer
88+
89+
@available(macOS 11, *)
90+
public func findGood(_: Set<Box>) -> Box {} // OK
91+
```
92+
93+
In the above code, it is not valid for `findBad()` to take a `Set<Box>`, since `Set` requires that its element type conform to `Hashable`; however the conformance of `Box` to `Hashable` is not available prior to macOS 11.
94+
95+
Note that using an unavailable protocol conformance is a warning, not an error, to avoid potential source compatibility issues. This is because it was technically possible to write code in the past that made use of unavailable protocol conformances but worked anyway, if the optimizer had serendipitously eliminated all runtime dispatch through this conformance, or the code in question was entirely unreachable at runtime.
96+
97+
Protocol conformances can also be marked as completely unavailable or deprecated, by placing an appropriate `@available` attribute on the extension:
98+
99+
```swift
100+
@available(*, unavailable, message: "Not supported anymore")
101+
extension Box : Hashable {}
102+
103+
@available(*, deprecated, message: "Suggest using something else")
104+
extension Box : Hashable {}
105+
```
106+
107+
If a protocol conformance is defined on the type itself, it inherits availability from the type. You can move the protocol conformance to an extension if you need it to have narrower availability than the type.
108+
109+
* When `swift` is run with no arguments, it starts a REPL (read eval print loop) that uses LLDB. The compiler also had a second REPL implementation, known as the "integrated REPL", formerly accessible by running `swift -frontend -repl`. The "integrated REPL" was only intended for use by compiler developers, and has now been removed.
110+
111+
Note that this does not take away the ability to put Swift code in a script and run it with `swift myScript.swift`. This so-called "script mode" is distinct from the integrated REPL, and continues to be supported.
112+
31113
* Property wrappers now work in local contexts, making the following valid:
32114

33115
```swift

0 commit comments

Comments
 (0)