Swift 5.3 released!

Holly Borla is an engineer on the Swift compiler team at Apple.

Swift 5.3 is now officially released! 🎉

Swift 5.3 continues to focus on language refinements, the developer experience, and expanding the Swift ecosystem to enable more opportunities to write Swift. These sketch notes, created by Amy Tsai, illustrate the highlights of the Swift 5.3 release:

Sketch notes of What's New in Swift 5.3

Full resolution version available on Amy’s tweet

You can also experiment with many of these updates in a playground put together by Paul Hudson.

Language and Standard Library

New Features and Refinements

Swift 5.3 brings many language refinements that improve the ergonomics of writing Swift code. These updates can help you be a more productive Swift programmer by reducing boilerplate and redundant code, and enabling more functionality to be defined in libraries that you may use.

Swift 5.3 implements the following proposals from the Swift Evolution process:

Many of these features were proposed and implemented by active community members: Kelvin Ma, Anthony Latsis, Suyash Srijan, Frederick Kellison-Linn and Owen Voorhees. Thank you for your contributions!

Swift 5.3 also includes fixes for several commonly-reported compiler limitations:

Runtime Performance Improvements

Swift 5.3 significantly improves both binary code size and runtime memory usage. Measurements of these improvements have yielded exciting results across various projects:

These measurements were reported in the ‘What’s New in Swift’ talk at WWDC 2020.

Binary size improvements will vary by patterns of use. The biggest improvement is in projects that declare a large number of types, through reduction in the size of “value functions” – the invisible functions that the compiler generates to create, copy, and destroy value types. This is especially beneficial to SwiftUI apps.

Additionally, Swift applications now have lower heap memory overhead at runtime. The Swift runtime caches less information on startup to track things like protocol conformances, due to improvements in the runtime that made this caching less necessary. An application written in Swift should now use less heap memory than an otherwise-identical program written in Objective-C.

Developer Experience

Indentation Improvements while Editing Code

The automatic indentation implementation in SourceKit was overhauled in this release, fixing ~50 feedback reports in the process. In particular, the automatic indentation of the following cases are much improved in this release:

Code Completion

Swift 5.3 further improves code completion performance and quality:

Build Time Improvements

Swift 5.3 incorporates a new strategy for how the compiler handles declarations in your Swift code. These changes bring several concrete improvements:

These improvements were achieved by expanding the adoption of a new centralized framework in the compiler that records fine-grained dependency information, caches the results of expensive computation that may need to be repeated, and automatically detects dependency cycles within your Swift code.

Compiler Diagnostics

Swift 5.3 builds upon the diagnostics improvements in Swift 5.2 to further enhance the quality and precision of error messages, especially in SwiftUI code. More specifically, the transition to the New Diagnostics Architecture is now complete in Swift 5.3!

Many of the diagnostics improvements in 5.3 involve complex generic code where a generic argument has a failed requirement, such as a missing conformance. For example, consider the following code:

struct FormList<FieldID> {
  init<Data: Collection>(_ data: Data) where Data.Element: Identifiable, 
                                             FieldID == Data.Element.ID { ... }
}

struct Field {
  let id: String
}

func createForm(fields: [Field]) {
  let form = FormList(fields)
}

In Swift 5.2, this compiler reported a very cryptic error message:

error: expression type 'FormList<_>' is ambiguous without more context
  let form = FormList(fields)
             ^~~~~~~~~~~~~~~~

In Swift 5.3, the compiler correctly reports the missing conformance, along with a helpful note showing the source of the requirement:

error: initializer 'init(_:)' requires that 'Field' conform to 'Identifiable'
  let form = FormList(fields)
             ^
             
note: where 'Data.Element' = 'Field'
  init<Data: Collection>(_ data: Data) where Data.Element: Identifiable,
  ^

Debugging

Swift 5.3 supports better error messages for runtime failures. When debug info is available, the debugger will now display the reason for traps in the standard library instead of just showing an opaque invalid instruction crash.

LLDB is now more robust when debugging binaries that were compiled on a different machine:

Ecosystem

Swift Package Manager

Resources (SE-0271)

Packages can now contain resources such as images and other data files needed at runtime. Resources are scoped by target, and are processed and embedded into client apps when the package is built. Resources can be accessed from source code using Foundation’s Bundle API.

Resources that are specific to Apple platforms (such as asset catalogs, storyboards, and CoreData models) can only be built in Xcode, but generic resources are supported on all platforms. New API in the package manifest provides control over which source files to treat as resources.

Localization (SE-0278)

Packages can now contain localizable content such as .strings files and localized variants of resources. Localizable content can be added to a package using .lproj directories, and can be accessed using Foundation APIs.

Binary Dependencies (SE-0272)

Packages can now vend prebuilt libraries distributed as XCFrameworks, allowing dependencies on libraries that can’t be distributed as source code. This feature is currently only available when building for Apple platforms. New API in the package manifest lets XCFrameworks be referenced as binary targets.

Conditional Target Dependencies (SE-0273)

A package target’s dependencies can now be declared as conditional, which can be used to limit dependencies by platform. This provides more flexibility to describe complex target dependencies that support multiple platforms.

Note that conditions based on build configuration were also a part of the Swift evolution proposal, but are yet to be implemented, and are therefore not part of Swift 5.3.

Downloads

Official binaries are available for download from Swift.org as toolchains to work with Xcode, as well as Linux toolchains and Docker images. Swift 5.3 is also included in Xcode 12. An official toolchain for Windows will be available in the coming weeks.

Sources

Development on Swift 5.3 was tracked in the release/5.3 branch on the following repositories:

The tag swift-5.3-RELEASE designates the specific revisions in those repositories that make up the final version of Swift 5.3.

The release/5.3 branch will remain open, but under the same release management process, to accumulate changes for the next release.