-
Notifications
You must be signed in to change notification settings - Fork 255
Swift 5.9 release blog post #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 31 commits
113c1bf
2f8aa20
1f9e12f
40d1057
cf20b9a
7518f22
e1bc48b
9bf6871
d147e4a
1171ead
dd2d3a1
8e492da
0fa3a86
79798f7
06e45f4
c2af136
9fe1cc8
a183a95
019cfc3
0121079
da1c493
475a1d7
2d316a2
3e08eb3
2eb95ab
902fa61
03ae353
4bb86ae
b038546
dc5f3f2
7d2f70a
667bbf1
5f81630
4f13f19
ccc6457
d894100
5838920
6ef669d
57a95c8
2aa7d5d
5d9300e
b7d6c50
a7ec23f
52a6395
ff49b01
4eb287a
acd33bb
e709a82
210665e
a9e7450
ad1dbda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| --- | ||
| layout: post | ||
| published: true | ||
| date: 2023-08-29 15:00:00 | ||
| title: Swift 5.9 Released | ||
| author: [alexandersandberg] | ||
| --- | ||
|
|
||
| Swift 5.9 is now available! 🎉 | ||
|
|
||
| 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. | ||
|
|
||
| It also introduces parameter packs, an improved expression evaluator while debugging, enhanced crash handling, Windows platform improvements, and more. | ||
|
|
||
| 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. | ||
|
|
||
| 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! | ||
|
|
||
| <!-- TODO: Link to Swift 5.9 showcase resource --> | ||
|
|
||
| ## Language and Standard Library | ||
|
|
||
| Swift 5.9 tackles three long-desired features that build upon the fundamental goals of clear and concise code while maintaining safety and efficiency. A new macro system unlocks more expressive power for libraries, parameter packs make overloaded APIs more natural to use, and new ownership features offer more performance control in low-level code. | ||
|
|
||
| Swift now supports [macros](https://github.com/apple/swift-evolution/blob/main/visions/macros.md), which allow developers to reduce repetitive boilerplate and create more expressive libraries that can be distributed as a Swift package. Using a macro is easy and natural; macros can either be expanded with the function-like freestanding `#macroName` syntax or attached to declarations with an `@MacroName` attribute, so they work just like built-in language features but can’t be mistaken for normal code. Implementing a macro uses a powerful and flexible approach: Macros are implemented by writing Swift functions that use the SwiftSyntax library to generate code to insert into the source file. 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. | ||
|
|
||
| [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 not just any type, but any _number_ of types. For example, this `all(_:)` function lets you check whether any number of `Optional` values are `nil`, even if the optionals wrap different types: | ||
|
|
||
| ```swift | ||
| func all<each Wrapped>(_ optional: repeat (each Wrapped)?) -> (repeat each Wrapped)? { | ||
| // Implementation provided below | ||
| } | ||
|
|
||
| let optionalInt: Int? = 1 | ||
| let optionalDouble: Double? = 2.5 | ||
| let optionalString: String? = nil | ||
|
|
||
| if let (int, double, string) = all(optionalInt, optionalDouble, optionalString) { | ||
| print(int, double, string) | ||
| } | ||
| else { | ||
| print("got a nil") | ||
| } | ||
| ``` | ||
|
|
||
| <details markdown="1" class="download"> | ||
| <summary>Implementation of <code>all</code></summary> | ||
|
|
||
| ```swift | ||
| /// Takes any number of `Optional` arguments and returns `nil` if any of them | ||
| /// are `nil`, or a tuple of their values if all of them are non-`nil`. | ||
| func all<each Wrapped>(_ optional: repeat (each Wrapped)?) -> (repeat each Wrapped)? { | ||
| /// Accumulates the results from the `checkNilness(of:)` helper function. | ||
| var anyAreNil = false | ||
|
|
||
| // Check if any of the arguments in `each optional` is nil by running this | ||
| // `checkNilness(of:)` function on each argument. | ||
| func checkNilness<OneWrapped>(of oneOptional: OneWrapped?) { | ||
| if oneOptional == nil { | ||
| anyAreNil = true | ||
| } | ||
| } | ||
| repeat checkNilness(of: each optional) | ||
|
|
||
| // Did `checkNilness(of:)` find any nils? If so, return nil early. | ||
| if anyAreNil { | ||
| return nil | ||
| } | ||
|
|
||
| // Otherwise, we can safely force-unwrap all of the arguments and form a tuple | ||
| // from the unwrapped values. | ||
| return (repeat (each optional)!) | ||
alexandersandberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| The last of these major features helps developers fine-tune memory management behavior in performance-critical code. The [`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, represent a specific resource that can’t be meaningfully copied when it’s 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. | ||
|
|
||
| What’s more, these features are all usable today in Swift 5.9, but they are not finished evolving. Each one is too large to fully implement in a single release; we’re excited about what you can already do with them in Swift 5.9, but we’re even more excited to have laid the foundations for better things to come. | ||
alexandersandberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| In addition to these major features, Swift 5.9 also includes many smaller refinements to the language. The one developers will probably use most often is [`if` and `switch` expressions](https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md), which lets you assign the result of a single-expression `if` or `switch` statement to a variable: | ||
hborla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```swift | ||
| statusBar.text = if !hasConnection { "Disconnected" } | ||
| else if let error = lastError { error.localizedDescription } | ||
| else { "Ready" } | ||
| ``` | ||
|
|
||
| Swift 5.9 also adds a [new `package` access level](https://github.com/apple/swift-evolution/blob/main/proposals/0386-package-access-modifier.md) that 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. | ||
|
|
||
| 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. | ||
|
|
||
| A full list of Swift 5.9 Evolution proposals can be found at the end of this post. | ||
|
|
||
| ## Developer Experience | ||
|
|
||
| ### Crash Handling | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ### Debugging | ||
alexandersandberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| The debug info produced by the Swift compiler is now more precisely scoping local variables, which makes it less likely to see variables backed by uninitialized memory in the debugger. | ||
|
||
|
|
||
| ## Ecosystem | ||
|
|
||
| ### C++ Interoperability | ||
|
|
||
| Swift 5.9 supports bidirectional interoperability with C++ and Objective-C++. You can now use a subset of C++ APIs in Swift and Swift APIs from C++: | ||
|
|
||
| ```cpp | ||
alexandersandberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Clang module 'PromptResponder' | ||
| #pragma once | ||
| #include <vector> | ||
|
|
||
| std::vector<std::string> generatePromptResponse(std::string prompt); | ||
| ``` | ||
|
|
||
| ```swift | ||
| import PromptResponder | ||
|
|
||
| let codeLines = generatePromptResponse("Write Swift code that prints hello world") | ||
| .map(String.init) | ||
| .filter { !$0.isEmpty } | ||
|
|
||
| for line in codeLines { | ||
| print(line) | ||
| } | ||
| ``` | ||
|
|
||
| C++ interoperability is an actively evolving feature of Swift and developed by the focused [C++ Interoperability Workgroup](/cxx-interop-workgroup/). Certain aspects of the design and functionality might change in future releases of Swift, as the Swift community gathers feedback from real world adoption in mixed Swift and C++ codebases. | ||
|
|
||
| For information on enabling C++ interoperability and the supported language subset, please refer to the [documentation](/documentation/cxx-interop/). | ||
|
|
||
| ### Swift Package Manager | ||
alexandersandberg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The following are some highlights from the changes introduced to the [Swift Package Manager](https://github.com/apple/swift-package-manager) in Swift 5.9: | ||
|
|
||
| - Packages can make use of 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. | ||
|
|
||
| - The `CompilerPluginSupport` module enables defining macro targets. Macro targets allow authoring and distributing custom Swift macros as API in a library. | ||
|
||
|
|
||
| - The new `.embedInCode` resource rule allows embedding the contents of the resource into the executable by generating a byte array. | ||
|
|
||
| - The `allowNetworkConnections(scope:reason:)` setting gives a command plugin permissions to access the network, which allows Linux-oriented packaging and deployment plugins to communicate with Docker within the SwiftPM sandbox. Permissions can be scoped to Unix domain sockets in general or specifically for Docker, as well as local or remote IP connections which can be limited by port. For non-interactive use cases, the `--allow-network-connections` command-line flag allows network connections for a particular scope. | ||
|
|
||
| - SwiftPM can now publish to a registry following the publishing spec as defined in [SE-0391](https://github.com/apple/swift-evolution/blob/main/proposals/0391-package-registry-publish.md). SwiftPM now supports signed packages, which a registry may require. Trust-on-first-use (TOFU) validation checks which previously only included fingerprints (e.g., checksums) has been extended to include signing identities, and it is enforced for source archives as well as package manifests. | ||
|
|
||
| - A new `swift sdk` command is now available for installing and removing [Swift SDK bundles](https://github.com/apple/swift-evolution/blob/main/proposals/0387-cross-compilation-destinations.md#swift-sdk-bundles) on the local filesystem. | ||
|
|
||
| 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. | ||
|
|
||
| ### Swift Syntax | ||
|
|
||
| [swift-syntax](https://github.com/apple/swift-syntax) is an essential tool for parsing Swift code, and it helps to power new macro libraries. This year, swift-syntax received a number of major improvements: | ||
|
|
||
| - Syntax nodes names are more consistent and accurately reflect the Swift language. | ||
|
||
|
|
||
| - Error recovery in the new SwiftParser is greatly improved, leading to more precise error messages for incorrect or missing syntax. | ||
|
|
||
| - Incremental parsing is now supported, allowing tools such as an editor to only reparse those parts of a syntax tree that have changed. | ||
|
|
||
| - The documentation of swift-syntax has been greatly expanded and can be viewed at [swiftpackageindex.com](https://swiftpackageindex.com/apple/swift-syntax). | ||
|
||
|
|
||
| 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 open source contributors have contributed to the package accounting for more than 30% of the commits. In addition, community tools like [swift-ast-explorer.com](https://swift-ast-explorer.com) are an invaluable tool to explore and understand the SwiftSyntax tree. Thanks to everyone who contributed! | ||
|
|
||
| ### Server | ||
hborla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The server workgroup has been busy driving the adoption of Swift concurrency in the server ecosystem, including making use of new 5.9 features such as [custom actor executors](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md). The group recently published their [annual update for 2023](/blog/sswg-update-2023/) which goes into details of this work. | ||
|
|
||
| ### Website | ||
hborla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Along with the 5.9 release, the website workgroup has revamped the content on key pages of [Swift.org](/), including a richer home page, new and detailed getting-started guides, and clearer download and install instructions. | ||
|
|
||
tomerd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
tomerd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ### Windows Platform | ||
|
|
||
| The Windows installation received new functionality to inject the module maps via a VFS overlay dynamically rather than needing to modify the Visual Studio installation. This change breaks the dependency order for installation; it is now possible to install either Visual Studio or the Swift toolchain first. This also eliminates the need to repair the installation after an upgrade to Visual Studio. As part of the continued refinement to the installer, initial work towards versioning the toolchain installation lays the foundation for the ability to have multiple parallel toolchain versions installed side-by-side on Windows. | ||
hborla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Alongside this improved handling of Visual Studio, the Swift toolchain 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. The Windows SDK (`WinSDK`) module also saw improvements in the coverage of the SDK, enabling access to a wider set of system APIs. The Visual C++ (`vcruntime`) module was greatly restructured to support C++ interoperability. | ||
|
|
||
| Structured Concurrency is now significantly more stable on Windows. Backtraces are still a work in progress, as the Windows ABI currently prohibits the necessary optimizations required for the proper implementation of the feature. | ||
hborla marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 significantly improves the developer experience on Windows. | ||
|
|
||
| Small improvements continue to be made to reduce the size of the toolchain. Although the impact of this is minimal, some of the tools are now smaller in size due to more careful curation of linking. This also reduces the number of artifacts that are contained in a toolchain installation. | ||
|
|
||
| ## Downloads | ||
|
|
||
| <!-- TODO: Add section content --> | ||
|
|
||
| ## Language Guide | ||
|
|
||
| [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. This is the official Swift guide and a great entry point for those new to Swift. The Swift community also maintains a number of [translations](/documentation/#translations). | ||
|
|
||
| ## Swift Evolution Appendix | ||
|
|
||
| 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). | ||
|
|
||
| - 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) | ||
| - SE-0374: [Add sleep(for:) to Clock](https://github.com/apple/swift-evolution/blob/main/proposals/0374-clock-sleep-for.md) | ||
| - SE-0377: [`borrowing` and `consuming` parameter ownership modifiers](https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md) | ||
| - SE-0380: [`if` and `switch` expressions](https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md) | ||
| - SE-0381: [DiscardingTaskGroups](https://github.com/apple/swift-evolution/blob/main/proposals/0381-task-group-discard-results.md) | ||
| - SE-0382: [Expression Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0382-expression-macros.md) | ||
| - 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) | ||
| - SE-0386: [New access modifier: `package`](https://github.com/apple/swift-evolution/blob/main/proposals/0386-package-access-modifier.md) | ||
| - SE-0388: [Convenience Async[Throwing]Stream.makeStream methods](https://github.com/apple/swift-evolution/blob/main/proposals/0388-async-stream-factory.md) | ||
| - SE-0389: [Attached Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0389-attached-macros.md) | ||
| - SE-0390: [Noncopyable structs and enums](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md) | ||
| - SE-0392: [Custom Actor Executors](https://github.com/apple/swift-evolution/blob/main/proposals/0392-custom-actor-executors.md) | ||
| - SE-0393: [Value and Type Parameter Packs](https://github.com/apple/swift-evolution/blob/main/proposals/0393-parameter-packs.md) | ||
| - SE-0394: [Package Manager Support for Custom Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0394-swiftpm-expression-macros.md) | ||
| - SE-0395: [Observation](https://github.com/apple/swift-evolution/blob/main/proposals/0395-observability.md) | ||
| - SE-0396: [Conform `Never` to `Codable`](https://github.com/apple/swift-evolution/blob/main/proposals/0396-never-codable.md) | ||
| - SE-0397: [Freestanding Declaration Macros](https://github.com/apple/swift-evolution/blob/main/proposals/0397-freestanding-declaration-macros.md) | ||
| - SE-0398: [Allow Generic Types to Abstract Over Packs](https://github.com/apple/swift-evolution/blob/main/proposals/0398-variadic-types.md) | ||
| - SE-0399: [Tuple of value pack expansion](https://github.com/apple/swift-evolution/blob/main/proposals/0399-tuple-of-value-pack-expansion.md) | ||
| - SE-0400: [Init Accessors](https://github.com/apple/swift-evolution/blob/main/proposals/0400-init-accessors.md) | ||
| - 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) | ||
| - SE-0402: [Generalize `conformance` macros as `extension` macros](https://github.com/apple/swift-evolution/blob/main/proposals/0402-extension-macros.md) | ||
Uh oh!
There was an error while loading. Please reload this page.