Skip to content

Commit a65ad79

Browse files
alexandersandbergtomerdal45tairhborladempseyatgithub
authored
Swift 5.9 release blog post (#372)
* add first blog post draft * include SE-0386 in evolutions appendix * add c++ interoperability section * Update 2023-08-29-swift-5.9-released.md (#374) * Update 2023-08-29-swift-5.9-released.md * indent code block in list --------- Co-authored-by: Alexander Sandberg <[email protected]> * update debugging section * update swift syntax section * add language and standard library section * fix casing of section title * Add a Crash Handling subsection. (#376) Add some words about the new crash handler, including a promise of a future blog post on the subject. * remove completed TODOs * add details element to hide code snippet * add SE-0395 to evolutions list * Remove promise of a future blog post. (#381) * Update _posts/2023-08-29-swift-5.9-released.md * Updates to the 5.9 blog post draft. (#398) * Add initial draft of the Windows section from Saleem. * Add a draft for the introduction to the 5.9 release blog post. * Minor editorial pass based on PR feedback. * Editorial pass for more consistent language and section structure. * Update _posts/2023-08-29-swift-5.9-released.md * Update _posts/2023-08-29-swift-5.9-released.md * Update _posts/2023-08-29-swift-5.9-released.md * use smart quotes Co-Authored-By: James Dempsey <[email protected]> * address PR feedback Co-Authored-By: James Dempsey <[email protected]> Co-Authored-By: finagolfin <[email protected]> * Improvements to the release blog post introduction. * Improve the framing of the language introduction, and flip the order of parameter packs a noncopyable types. * Minor editorial changes in the debugging section. * Minor editorial changes in the Swift Syntax section. * Editorial changes for the Swift Package Manager section. * Re-combine the bullet point about SE-0391 and signed packages. Signed packages are not applicable outside of a registry. * Lift up C++ interop to the top of the Ecosystem section. * Update _posts/2023-08-29-swift-5.9-released.md Co-authored-by: Doug Gregor <[email protected]> * Update _posts/2023-08-29-swift-5.9-released.md Co-authored-by: Adrian Prantl <[email protected]> * Add a code example of Swift-C++ interop. * Show an API before and after parameter packs to demonstrate reducing overloads. * Editorial changes to the Windows section. * More editorial changes for the language section. * Add subheaders to the Language section * Add macro examples * Minor tweaks to the C++ example flow * Use consistent formatting for code examples. * Add a Next Steps section at the end of the post. * Address the outstanding review feedback. * add @hborla to list of authors * Minor tweaks to Language and Standard Library * Minor edits to the remaining sections * Clarify macro composition refers to developers writing macros, not compiler engineers implementing the feature * Add example of a parameter pack call-site * Add macro examples * Update the download section * remove TODO * Minor tweaks for readability * Add a note about which platforms support macros. * Update post date and time --------- Co-authored-by: tomer doron <[email protected]> Co-authored-by: Alastair Houghton <[email protected]> Co-authored-by: Holly Borla <[email protected]> Co-authored-by: James Dempsey <[email protected]> Co-authored-by: finagolfin <[email protected]> Co-authored-by: Doug Gregor <[email protected]> Co-authored-by: Adrian Prantl <[email protected]> Co-authored-by: Christopher Thielen <[email protected]> Co-authored-by: Christopher Thielen <[email protected]> Co-authored-by: Mishal Shah <[email protected]>
1 parent 94c7485 commit a65ad79

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
---
2+
layout: post
3+
published: true
4+
date: 2023-09-18 11:00:00
5+
title: Swift 5.9 Released
6+
author: [alexandersandberg, hborla]
7+
---
8+
9+
Swift 5.9 is now available! 🎉
10+
11+
This is a major new release that **adds an expressive macro system to the language** and introduces **support for integrating Swift into C++ codebases** through bidirectional interoperability.
12+
13+
It also introduces parameter packs, an improved expression evaluator while debugging, enhanced crash handling, Windows platform improvements, and more.
14+
15+
Read on for a deep dive into changes to the language, standard library, tooling, platform support, and next steps for getting started with Swift 5.9.
16+
17+
Thank you to everyone in the Swift community who made this release possible. Your Swift Forums discussions, bug reports, pull requests, educational content, and other contributions are always appreciated!
18+
19+
## Language and Standard Library
20+
21+
Swift's fundamental goal is to encourage code that is clear and concise, while remaining safe and efficient.
22+
23+
This release adds three long-desired features that further that goal: a new macro system for more expressive libraries, parameter packs to make overloaded APIs natural to use, and new ownership features to offer more control over performance in low-level code.
24+
25+
### Macros
26+
27+
[Macros](https://github.com/apple/swift-evolution/blob/main/visions/macros.md) help developers reduce repetitive boilerplate and create more expressive libraries that can be distributed as a Swift package.
28+
29+
Using a macro is easy and natural. Macros can either be expanded with a function-like freestanding `#macroName` syntax:
30+
31+
```swift
32+
let _: Font = #fontLiteral(name: "SF Mono", size: 14, weight: .regular)
33+
```
34+
35+
or attached to declarations with an `@MacroName` attribute:
36+
37+
```swift
38+
// Move storage from the stored properties into a dictionary,
39+
// turning the stored properties into computed properties.
40+
@DictionaryStorage
41+
struct Point {
42+
var x: Int = 1
43+
var y: Int = 2
44+
}
45+
```
46+
47+
They work just like built-in language features but can’t be mistaken for normal code.
48+
49+
You write macros using a powerful and flexible approach: they are simply Swift functions that use the [SwiftSyntax](https://github.com/apple/swift-syntax) library to generate code to be inserted into the source file.
50+
51+
Macros make it easy for your library’s users to adopt powerful capabilities that adapt to the code they’re used in, like [the new `Observation` module](https://github.com/apple/swift-evolution/blob/main/proposals/0395-observability.md), which allows Swift classes to automatically notify other code when their properties change.
52+
53+
The Swift community has been hard at work creating tools and frameworks that build upon macros. For some examples, take a look at [swift-power-assert](https://github.com/kishikawakatsumi/swift-power-assert), [swift-spyable](https://github.com/Matejkob/swift-spyable), [swift-macro-testing](https://github.com/pointfreeco/swift-macro-testing), and [MetaCodable](https://github.com/SwiftyLab/MetaCodable).
54+
55+
Swift 5.9 supports macros on macOS and Linux, with Windows support coming soon.
56+
57+
### Parameter packs
58+
59+
[Parameter packs](https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md) let you write generic types and functions which work over an arbitrary number of types.
60+
61+
For example, without parameter packs, if you wanted to write a function called `all` to check whether any number of `Optional` values are `nil`, you would need to write a separate overload for each argument length you wanted to support, creating an arbitrary upper limit:
62+
63+
```swift
64+
func all<W1>(_ optional: W1?) -> W1?
65+
func all<W1, W2>(_ optional1: W1?, optional2: W2?) -> (W1, W2)?
66+
func all<W1, W2, W3>(_ optional1: W1?, optional2: W2?, optional3: W3?) -> (W1, W2, W2)?
67+
```
68+
69+
With parameter packs, you can express this API as a single function that has no upper limit, allowing you to pass any number of arguments:
70+
71+
```swift
72+
func all<each Wrapped>(_ optional: repeat (each Wrapped)?) -> (repeat each Wrapped)?
73+
```
74+
75+
Calling an API that uses parameter packs is intuitive and requires no extra work:
76+
77+
```swift
78+
let (int, double, string, bool) = all(optionalInt, optionalDouble, optionalString, optionalBool) {
79+
print(int, double, string, bool)
80+
}
81+
else {
82+
print("got a nil")
83+
}
84+
```
85+
86+
### Ownership
87+
88+
Ownership features can help developers fine-tune memory management behavior in performance-critical code.
89+
90+
The new [`consume` operator](https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md) tells Swift to deinitialize a variable and transfer its contents without copying it. The [`consuming` and `borrowing` parameter modifiers](https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md) provide hints that Swift can use to eliminate unnecessary copying and reference-counting operations when passing a parameter. Finally, [noncopyable structs and enums](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md) allow you to create types which, like a class, can’t be meaningfully copied when assigned, but like a struct or enum, do not need to be reference-counted because only one storage location can own the instance at a time.
91+
92+
These major features are the foundation for further evolution that puts more expressive power and performance control into your hands. We're excited about what you can do with Swift 5.9, and we're working toward a future with a larger suite of variadic generics and ownership features.
93+
94+
### Additional Features
95+
96+
Swift 5.9 also includes smaller, quality-of-life changes to the language, like the ability to use [`if` and `switch` as expressions](https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md) for variable assignment and return values:
97+
98+
```swift
99+
statusBar.text = if !hasConnection { "Disconnected" }
100+
else if let error = lastError { error.localizedDescription }
101+
else { "Ready" }
102+
```
103+
104+
A [new `package` access level](https://github.com/apple/swift-evolution/blob/main/proposals/0386-package-access-modifier.md) lets other modules in the same package access APIs, but hides them from code outside the package. It’s great for splitting up large modules into several smaller ones without exposing internals to the package’s clients.
105+
106+
Developers using Swift Concurrency may appreciate the [more convenient `DiscardingTaskGroup` types](https://github.com/apple/swift-evolution/blob/main/proposals/0381-task-group-discard-results.md) for task groups that don’t generate results and the advanced [custom actor executors](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md) feature for controlling the exact environment that an actor is run in.
107+
108+
A full list of Swift 5.9 Evolution proposals can be found at the end of this post.
109+
110+
## Developer Experience
111+
112+
### Crash Handling
113+
114+
On Linux, the Swift runtime will now catch program crashes and Swift runtime errors and display a backtrace on the program’s output. The backtracer is out-of-process and includes support for `async` functions.
115+
116+
This feature is also available on macOS but is disabled by default. To enable it, set `SWIFT_BACKTRACE=enable=yes` and sign your program with the `com.apple.security.get-task-allow` entitlement.
117+
118+
### Debugging
119+
120+
Swift 5.9 introduces new features to [LLDB](https://lldb.llvm.org/) and the Swift compiler aimed at making Swift debugging faster and more reliable.
121+
122+
The `p` and `po` commands now print local variables and properties as fast as the `frame variable` or `v` commands by bypassing the Swift compiler when evaluating simple expressions.
123+
124+
Swift expressions can now refer to generic type parameters. This allows setting a conditional breakpoint in a generic function that only triggers when a type parameter is instantiated with a specific concrete type.
125+
126+
The debug info produced by the Swift compiler now more precisely scopes local variables, making it less likely to display variables backed by uninitialized memory in the debugger.
127+
128+
## Ecosystem
129+
130+
### C++ Interoperability
131+
132+
Swift 5.9 supports bidirectional interoperability with C++ and Objective-C++ for certain kinds of APIs.
133+
134+
For example, if you have a C++ function like:
135+
136+
```cpp
137+
// Clang module 'PromptResponder'
138+
#pragma once
139+
#include <vector>
140+
141+
std::vector<std::string> generatePromptResponse(std::string prompt);
142+
```
143+
144+
you can call it directly from your Swift code:
145+
146+
```swift
147+
import PromptResponder
148+
149+
let codeLines = generatePromptResponse("Write Swift code that prints hello world")
150+
.map(String.init)
151+
.filter { !$0.isEmpty }
152+
153+
for line in codeLines {
154+
print(line)
155+
}
156+
```
157+
158+
C++ interoperability is actively evolving, with some aspects subject to change in future releases as the community gathers feedback from real world adoption in mixed Swift and C++ codebases.
159+
160+
For information on enabling C++ interoperability and the supported language subset, please refer to the [documentation](/documentation/cxx-interop/).
161+
162+
Special thanks to the [C++ Interoperability Workgroup](/cxx-interop-workgroup/) for their focus and dedication in supporting this feature.
163+
164+
### Swift Package Manager
165+
166+
The [Swift Package Manager](https://github.com/apple/swift-package-manager) has a number of improvements in Swift 5.9:
167+
168+
- Packages can use the new `package` access modifier, allowing access of symbols in another target / module within the same package without making them public. SwiftPM automatically sets the new compiler configuration to ensure this feature works out-of-the-box for packages.
169+
170+
- The `CompilerPluginSupport` module enables defining macro targets. Macro targets allow authoring and distributing custom Swift macros as APIs in a library.
171+
172+
- The new `.embedInCode` resource rule allows embedding the contents of a resource into an executable by generating a byte array.
173+
174+
- The `allowNetworkConnections(scope:reason:)` setting gives a command plugin permissions to access the network. Permissions can be scoped to Unix domain sockets as well as local or remote IP connections, with an option to limit by port. For non-interactive use cases, the `--allow-network-connections` command-line flag allows network connections for a particular scope.
175+
176+
- SwiftPM can now publish to a registry following the specification defined in [SE-0391](https://github.com/apple/swift-evolution/blob/main/proposals/0391-package-registry-publish.md), as well as support signed packages, which may be required by a registry. Trust-on-first-use (TOFU) validation checks can now use signing identities in addition to fingerprints, and are enforced for source archives as well as package manifests.
177+
178+
- SwiftPM now supports cross compilation based on [the Swift SDK bundle format](https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md#swift-sdk-bundles). While the feature is still considered experimental, we invite users to try it out and provide feedback.
179+
180+
See the [Swift Package Manager changelog](https://github.com/apple/swift-package-manager/blob/main/CHANGELOG.md#swift-59) for the complete list of changes.
181+
182+
### Swift Syntax
183+
184+
[swift-syntax](https://github.com/apple/swift-syntax) is an essential tool for parsing Swift code and helps power the new macro system. This year, swift-syntax received a number of improvements:
185+
186+
- Syntax node names are more consistent and accurately reflect the Swift language.
187+
188+
- Error recovery in the new SwiftParser is greatly improved, leading to more precise error messages for incorrect or missing syntax.
189+
190+
- Incremental parsing is now supported, allowing editors and other tools to only reparse those parts of a syntax tree that have changed.
191+
192+
- The [documentation](https://swiftpackageindex.com/apple/swift-syntax) of swift-syntax has been greatly expanded.
193+
194+
Over the last year, swift-syntax has been a huge success as an open source project. Since the release of Swift 5.8, more than 30 distinct contributors have worked on the package, accounting for more than 30% of the commits. In addition, the community tool [swift-ast-explorer.com](https://swift-ast-explorer.com) is invaluable in exploring and understanding the SwiftSyntax tree. Thank you to everyone who contributed!
195+
196+
### Server
197+
198+
Custom actor executors and other features from Swift 5.9 are making their way into the Swift on server ecosystem.
199+
200+
The server workgroup also recently published their [annual update for 2023](/blog/sswg-update-2023/), detailing plans to increase adoption of concurrency within key libraries, as well as other efforts.
201+
202+
### Windows Platform
203+
204+
Windows support for Swift 5.9 greatly improves stability and the developer experience.
205+
206+
The Windows installer now supports installation both before and after Visual Studio installation, and no longer requires repair after a Visual Studio upgrade. Initial work has also begun to enable multiple, parallel toolchains installed side-by-side on Windows.
207+
208+
The Swift toolchain also added new flags (`-windows-sdk-root`, `-windows-sdk-version`, `-visualc-tools-root`, `-visualc-tools-version`) to help control the Windows SDK and Visual C++ tools that it builds against.
209+
210+
The Windows SDK (`WinSDK`) module saw improvements in coverage, enabling access to a wider set of system APIs. The Visual C++ (`vcruntime`) module was greatly restructured to support C++ interoperability.
211+
212+
Structured Concurrency is now significantly more stable on Windows, eliminating stack overflows and other execution failures for common patterns such as iterating over an `AsyncStream` with many elements.
213+
214+
Improvements to path handling in the LSP and SPM makes both of these tools more robust on Windows. LLDB also saw initial work towards improving support for Windows, enabling fundamental debugging workflows in LLDB on Windows. While still a work in progress, this will significantly improve the developer experience on Windows.
215+
216+
Small improvements have also been made to reduce the size of the toolchain on Windows.
217+
218+
## Next Steps
219+
220+
### Downloads
221+
222+
Official binaries are [available for download](https://swift.org/download/) from [Swift.org](http://swift.org/) for macOS, Windows, and Linux. The Swift 5.9 compiler is also included in [Xcode 15](https://apps.apple.com/app/xcode/id497799835).
223+
224+
### Website
225+
226+
Swift.org has revamped certain key pages, including [a richer home page](/) and clearer [download and install instructions](/install/).
227+
228+
If you're new to Swift or looking to dive deeper, check out the updated [Getting Started guides](/getting-started/).
229+
230+
### Language Guide
231+
232+
[The Swift Programming Language](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/) book has been updated for Swift 5.9 and is now published with DocC.
233+
234+
This is the official Swift guide and a great entry point for learning Swift.
235+
236+
The Swift community also maintains a number of [translations](/documentation/#translations).
237+
238+
## Swift Evolution Appendix
239+
240+
The following language, standard library, and Swift Package Manager proposals were accepted through the [Swift Evolution](https://github.com/apple/swift-evolution) process and [implemented in Swift 5.9](https://apple.github.io/swift-evolution/#?version=5.9).
241+
242+
- SE-0366: [`consume` operator to end the lifetime of a variable binding](https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md)
243+
- SE-0374: [Add sleep(for:) to Clock](https://github.com/apple/swift-evolution/blob/main/proposals/0374-clock-sleep-for.md)
244+
- SE-0377: [`borrowing` and `consuming` parameter ownership modifiers](https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md)
245+
- SE-0380: [`if` and `switch` expressions](https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md)
246+
- SE-0381: [DiscardingTaskGroups](https://github.com/apple/swift-evolution/blob/main/proposals/0381-task-group-discard-results.md)
247+
- SE-0382: [Expression Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md)
248+
- SE-0384: [Importing Forward Declared Objective-C Interfaces and Protocols](https://github.com/apple/swift-evolution/blob/main/proposals/0384-importing-forward-declared-objc-interfaces-and-protocols.md)
249+
- SE-0386: [New access modifier: `package`](https://github.com/apple/swift-evolution/blob/main/proposals/0386-package-access-modifier.md)
250+
- SE-0388: [Convenience Async[Throwing]Stream.makeStream methods](https://github.com/apple/swift-evolution/blob/main/proposals/0388-async-stream-factory.md)
251+
- SE-0389: [Attached Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md)
252+
- SE-0390: [Noncopyable structs and enums](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md)
253+
- SE-0392: [Custom Actor Executors](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md)
254+
- SE-0393: [Value and Type Parameter Packs](https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md)
255+
- SE-0394: [Package Manager Support for Custom Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0394-swiftpm-expression-macros.md)
256+
- SE-0395: [Observation](https://github.com/apple/swift-evolution/blob/main/proposals/0395-observability.md)
257+
- SE-0396: [Conform `Never` to `Codable`](https://github.com/apple/swift-evolution/blob/main/proposals/0396-never-codable.md)
258+
- SE-0397: [Freestanding Declaration Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md)
259+
- SE-0398: [Allow Generic Types to Abstract Over Packs](https://github.com/apple/swift-evolution/blob/main/proposals/0398-variadic-types.md)
260+
- SE-0399: [Tuple of value pack expansion](https://github.com/apple/swift-evolution/blob/main/proposals/0399-tuple-of-value-pack-expansion.md)
261+
- SE-0400: [Init Accessors](https://github.com/apple/swift-evolution/blob/main/proposals/0400-init-accessors.md)
262+
- SE-0401: [Remove Actor Isolation Inference caused by Property Wrappers](https://github.com/apple/swift-evolution/blob/main/proposals/0401-remove-property-wrapper-isolation.md)
263+
- SE-0402: [Generalize `conformance` macros as `extension` macros](https://github.com/apple/swift-evolution/blob/main/proposals/0402-extension-macros.md)

0 commit comments

Comments
 (0)