Swift compiler crash with Xcode 16 beta 5

This simple project just makes the Swift compiler crash in init(comparator: KeyPathComparator<RemoteComputer>):

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

enum CSItemOrderType: Int, Codable, CaseIterable, CustomStringConvertible {
    case readableName
    case lastConnectionDate
    case numberOfConnections
    
    var description: String {
        switch self {
        case .readableName: return "STR_NAME"
        case .lastConnectionDate: return "STR_LAST_CONN_DATE"
        case .numberOfConnections: return "STR_ORDER_MOST_CONNECTED"
        }
    }
}

struct CSItemOrder: Codable {
    static let allCases = [CSItemOrder(type: .readableName),
                           CSItemOrder(type: .lastConnectionDate, order: .reverse),
                           CSItemOrder(type: .numberOfConnections, order: .reverse)]
    
    static let allSortOrders = [KeyPathComparator(\RemoteComputer.readableName),
                                KeyPathComparator(\RemoteComputer.lastConnectionDate),
                                KeyPathComparator(\RemoteComputer.numberOfConnections)]
    
    let type: CSItemOrderType
    var order: SortOrder

    var comparator: KeyPathComparator<RemoteComputer> {
        switch type {
        case .readableName: return KeyPathComparator(\RemoteComputer.readableName, order: order)
        case .lastConnectionDate: return KeyPathComparator(\RemoteComputer.lastConnectionDate, order: order)
        case .numberOfConnections: return KeyPathComparator(\RemoteComputer.numberOfConnections, order: order)
        }
    }
    
    init(type: CSItemOrderType, order: SortOrder = .forward) {
        self.type = type
        self.order = order
    }
    
    init(comparator: KeyPathComparator<RemoteComputer>) throws {
        switch comparator.keyPath {
        case \RemoteComputer.readableName: self.init(type: .readableName, order: comparator.order)
        case \RemoteComputer.lastConnectionDate: self.init(type: .lastConnectionDate, order: comparator.order)
        case \RemoteComputer.numberOfConnections: self.init(type: .numberOfConnections, order: comparator.order)
        default:
            print("Unsupported keyPath: \(comparator.keyPath)")
            throw ItemOrderError.unsupportedKeyPath
        }
    }
}


struct RemoteComputer: Codable, Hashable, Identifiable {
    let id: Int
    let name: String
    let hostname: String?
    let localIpAddress: String?
    let vncPort: Int
    let sshPort: Int
    let publicIpAddress: String?
    let publicPort: Int
    let macAddress: String
    let scVersion: String
    let manualMode: Int
    let uuid: String
    let lastMappingStatus: String?
    let isAwake: Bool
    let unregistered: Bool
    let osVersion: String?
    let lastUpdate: Date
    let remoteAddress: String?
    let lastKeyboardLocale: String?
    let macSystemShortcuts: [String: [Int32]]?
    
    var readableName: String {
        return name.removingPercentEncoding ?? name
    }
    
    var lastConnectionDate: Date {
        return Date.now
    }
    
    var unwrappedPublicIpAddress: String {
        return publicIpAddress ?? NSLocalizedString("STR_NOT_AVAILABLE", comment: "")
    }
    
    var numberOfConnections: Int {
        return 0
    }
}

enum ItemOrderError: Error {
    case unsupportedKeyPath
}

This code works fine in previous betas or Xcode 15.

Here's an excerpt of the crash:

Unhandled coercion:
(existential_type
  (protocol_composition_type
    (bound_generic_class_type decl="Swift.(file).PartialKeyPath"
      (struct_type decl="PartialKeyPath.(file).RemoteComputer@/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:74:8"))
    (protocol_type decl="Swift.(file).Sendable")))
(bound_generic_class_type decl="Swift.(file).PartialKeyPath"
  (struct_type decl="PartialKeyPath.(file).RemoteComputer@/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:74:8"))
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
...
1.	Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(PartialKeyPath.(file).CSItemOrder.init(comparator:)@/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:61:5)
5.	While type-checking statement at [/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:61:64 - line:70:5] RangeText="{
        switch comparator.keyPath {
        case \RemoteComputer.readableName: self.init(type: .readableName, order: comparator.order)
        case \RemoteComputer.lastConnectionDate: self.init(type: .lastConnectionDate, order: comparator.order)
        case \RemoteComputer.numberOfConnections: self.init(type: .numberOfConnections, order: comparator.order)
        default:
            print("Unsupported keyPath: \(comparator.keyPath)")
            throw ItemOrderError.unsupportedKeyPath
        }
    "
6.	While type-checking statement at [/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:62:9 - line:69:9] RangeText="switch comparator.keyPath {
        case \RemoteComputer.readableName: self.init(type: .readableName, order: comparator.order)
        case \RemoteComputer.lastConnectionDate: self.init(type: .lastConnectionDate, order: comparator.order)
        case \RemoteComputer.numberOfConnections: self.init(type: .numberOfConnections, order: comparator.order)
        default:
            print("Unsupported keyPath: \(comparator.keyPath)")
            throw ItemOrderError.unsupportedKeyPath
        "
7.	While type-checking expression at [/Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:62:16 - line:62:27] RangeText="comparator."
8.	While type-checking-target starting at /Users/me/Downloads/PartialKeyPath/PartialKeyPath/ContentView.swift:62:27

FB14697971

For me Xcode 16 beta 5 crashes when I open it on macOS 15 beta 5

  • Xcode: Xcode 16 beta 5
  • macOS: macOS 15 beta 5

Thanks for filing the feedback, I hope this gets fixed as Xcode is unusable.

We met this issue too. The compiler will crash once I access KeyPathComparator.keyPath. Maybe we can use keyPath to access keyPath:

init(comparator: KeyPathComparator<RemoteComputer>) throws {
        let path = \KeyPathComparator<RemoteComputer>.keyPath
        let keyPath = comparator[keyPath: path]
        switch keyPath {
        case \RemoteComputer.readableName: self.init(type: .readableName, order: comparator.order)
        case \RemoteComputer.lastConnectionDate: self.init(type: .lastConnectionDate, order: comparator.order)
        case \RemoteComputer.numberOfConnections: self.init(type: .numberOfConnections, order: comparator.order)
        default:
            print("Unsupported keyPath: \(keyPath)")
            throw ItemOrderError.unsupportedKeyPath
        }
    }

After updating to macOS 15 Beta 6, I am able to open Xcode 15 Beta 5 and it doesn't crash.

You could it try after updating the OS to macOS 15 Beta 6.

Swift compiler crash with Xcode 16 beta 5
 
 
Q