Swift 5.6 is now officially released!
Thank you to everyone in the Swift community for your discussion, proposals, bug reports, pull requests, and more.
Swift 5.6 includes a number of enhancements to the type system, improved interaction with pointers, and adds the ability to run new plugin commands using the package manager.
For a quick dive into some of what’s new in Swift 5.6, check out this playground put together by Paul Hudson.
If you’re new to Swift, The Swift Programming Language is the definitive guide on the Swift programming language and has been updated for version 5.6. The Swift community also maintains a number of translations. It is also available for free on the Apple Books store.
Table of Contents
Language and Standard Library
New Features and Refinements
Swift 5.6 enhances the language through a number of proposals from the Swift Evolution process, including:
- SE-0290 – Unavailability Condition
- SE-0302 –
Sendable
and@Sendable
closures - SE-0315 – Type placeholders (formerly, “Placeholder types”)
- SE-0320 – Allow coding of non
String
/Int
keyedDictionary
into aKeyedContainer
- SE-0322 – Temporary uninitialized buffers
- SE-0324 – Relax diagnostics for pointer arguments to C functions
- SE-0331 – Remove Sendable conformance from unsafe pointer types
- SE-0335 – Introduces existential
any
- SE-0337 – Incremental migration to concurrency checking
Let’s take a closer look at some of these below.
Enhancements to the Type System
Type Placeholders (SE-0315)
Swift allows you to omit verbose, incidental details from your code using type inference. However, writing explicit types when needed can feel excessive because you have to specify a complete type, even when your code only needed a specific part of the type to provide clarity:
enum Either<Left, Right> {
case left(Left)
case right(Right)
}
let either: Either<ClosedRange<Int>, Range<Int>> = .left(0...10)
With type placeholders, you can now write partial type annotations in your code to provide only the details that were necessary. A type placeholder is written with _, and it directs the compiler to infer the missing type:
enum Either<Left, Right> {
case left(Left)
case right(Right)
}
// Inferred as 'Either<ClosedRange<Int>, Range<Int>>'
let either: Either<_, Range<Int>> = .left(0...10)
Existential any
(SE-0335)
Existential types in Swift are used to store a value of any type conforming to a specific protocol. Today, existential types are spelled using a plain protocol name or protocol composition:
protocol DataSourceObserver { ... }
struct DataSource {
var observers: [DataSourceObserver] { ... }
}
An existential type erases its underlying type information, which is useful when you need to dynamically change the underlying type, but it prohibits existential types from other useful capabilities such as conforming to protocols. The existing syntax is confusing because an existential type looks just like a generic conformance requirement, which doesn’t have these fundamental limitations.
In Swift 5.6, existential types can be explicitly marked with the any keyword:
protocol DataSourceObserver { ... }
struct DataSource {
var observers: [any DataSourceObserver] { ... }
}
Improved Interaction with Pointers
Swift 5.6 introduces three significant improvements when working with unsafe pointers:
Temporary uninitialized buffers (SE-0322)
This introduces a new way to create temporary uninitialized memory space, which is particularly useful when interacting with C APIs that need to be supplied with memory into which to store results of a computation.
Relax diagnostics for pointer arguments to C functions (SE-0324)
This change allows the passing of mutable variants of unsafe pointers (e.g. UnsafeMutablePointer
) to APIs that take the immutable version (e.g. UnsafePointer
) without an explicit conversion.
Remove Sendable conformance from unsafe pointer types (SE-0331)
Feedback from early adoption of Sendable shows that pointer conformance has unexpected negative consequences, especially for implicit conformance, since these types behave like references.
Improved Concurrency Safety Model
Swift 5.6 also includes several improvements to the concurrency safety model:
Sendable
and @Sendable
closures (SE-0302)
Structural types such as @Sendable
function types, tuples consisting of Sendable
type (including Void
), and metatypes (like Any.Type
) now conform to the Sendable
protocol. The Task and task-local APIs now define Sendable
constraints as necessary.
Incremental migration to concurrency checking (SE-0337)
Diagnostics about Sendable
are suppressed by default in Swift 5.6, but can be enabled by explicitly defining conformances to Sendable
or using the -warn-concurrency
compiler flag, enabling an incremental migration path to concurrency checking.
Ecosystem
Swift Package Manager
The Swift Package Manager gained extensibility features in Swift 5.6, alongside several important security, performance and reliability updates.
Extensible Build Tools (SE-0303)
Introduces the ability to define build tool plugins in SwiftPM, allowing custom tools to be automatically invoked during a build. Build tool plugins are focused on code generation during the build of a package, for such purposes as generating Swift source files from .proto files or from other inputs, in order to allow build tools to be incorporated into the build graph and to run automatically in a safe manner.
Command Plugins (SE-0332)
Extends SwiftPM plugin support first introduced with SE-0303 to allow the definition of custom command plugins — plugins that users can invoke directly from the SwiftPM CLI, or from an IDE that supports Swift Packages, in order to perform custom actions on their packages. A command plugin specifies the semantic intent of the command — this might be one of the predefined intents such “documentation generation” or “source code formatting”, or it might be a custom intent with a specialized verb that can be passed to the swift package command.
Other updates include:
- SE-0305 – Package Manager Binary Target Improvements
- Semantic version dependencies can now be resolved against Git tag names that contain only major and minor version identifiers. A tag with the form X.Y will be treated as X.Y.0. This improves compatibility with existing repositories.
- To increase the security of packages, SwiftPM performs trust on first use (TOFU) validation. The fingerprint of a package is now being recorded when the package is first downloaded from a Git repository. Subsequent downloads must have fingerpints matching previous recorded values, otherwise it would result in build warnings or failures depending on settings.
- Multiple improvements to dependencies resolution infrastructure, leading to improved performance and reliability of dependency resolution
Swift-DocC Updates
Swift-DocC is now available as a SwiftPM plugin using the new plugin command support. See the documentation to learn how to get started.
In addition, you can now use Swift-DocC to publish static content to GitHub Pages.
Other enhancements include:
- The
docc
command-line tool is now a part of the open-source, release Swift toolchain for macOS and Linux platforms. - Swift-DocC can now build documentation that is compatible with static hosting environments, like GitHub Pages.
- Swift-DocC can now produce documentation for executable targets like command-line tools and apps.
Be sure to check out Joseph Heck’s great blog post covering this is in more detail.
Downloads
Official binaries are available for download from Swift.org for Xcode, Windows, and Linux. Swift 5.6 is also included in Xcode 13.3.
We also provide RPMs for Amazon Linux 2 and CentOS 7 for experimental use only. Please provide your feedback.
Use the instructions below for RPM installation:
Amazon Linux 2
$ curl https://download.swift.org/experimental-use-only/repo/amazonlinux/releases/2/swiftlang.repo > /etc/yum.repos.d/swiftlang.repo
$ amazon-linux-extras install epel
$ yum install swiftlang
CentOS 7
$ curl https://download.swift.org/experimental-use-only/repo/centos/releases/7/swiftlang.repo > /etc/yum.repos.d/swiftlang.repo
$ yum install epel-release
$ yum install swiftlang
Leave a Reply