Swift 6 Migration (#5733)

* Enable Swift 6.2 with default main actor isolation for the app target

Turn on SWIFT_VERSION 6.2, SWIFT_APPROACHABLE_CONCURRENCY and SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor for the ElementX target, matching the NSE and Share Extension (which already use 6.2 without the main actor default).

# Conflicts:
#	ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

* Update the code generation templates for Swift 6

Emit nonisolated/@unchecked Sendable mocks and nonisolated string enums, and mark async mock methods @concurrent so they keep suspending like the real implementations (otherwise intermediate states are unobservable in tests). Includes the regenerated mocks and strings.

# Conflicts:
#	ElementX/Sources/Mocks/Generated/GeneratedMocks.swift

* Make the data layer and services nonisolated and Sendable

Annotate the value-type models, Rust SDK proxy wrappers, timeline item graph, settings, logging and NSE-shared types as nonisolated, and conform the service protocols to Sendable so their existentials can cross isolation boundaries. These types run off the main actor and no longer inherit the target's default main actor isolation.

* Adopt the new isolation across call sites, mocks and signatures

Update call sites, closure signatures and mock configuration helpers to match the nonisolated/Sendable data layer, including the main actor isolated mock inits and the @Sendable filter on recentlyVisitedRooms.

^ Conflicts:
^	ElementX/Sources/Services/SecureBackup/SecureBackupController.swift

* Remove redundant @MainActor annotations

With the app target defaulting to main actor isolation, the explicit @MainActor on view models, coordinators, views and their protocols is no longer required.

* Fix concurrency hotspots surfaced by the migration

Resolve the room summary main-thread deadlock by moving diff processing onto AsyncStreams, add the SDKListener.onMainActor FIFO bridge for SDK callbacks, hop KVO/timer/UIKit callbacks to the main actor via assumeIsolated, add isolated deinits, and apply the few justified nonisolated(unsafe) escapes for queue-confined Apple APIs.

* Fix the unit tests for Swift 6 concurrency

Hop the waitForConfirmation body onto the caller's actor (Swift Testing runs confirmation closures off-actor), and adjust the affected suites and async signatures.

* offload diffs on concurrent threads instead of main actor

* make room info protocols sendable

* use a task group for fetching data so that everything runs concurrently in the TextRoomTimelineView when getting the preview.

* use the generic onMainActor implementation, which already makes a stream and send the continuation on the main actor.

* small pr comments issues being addressed

* update enterprise

* Update StaticCode to 1.1.0 and specify the required Swift isolation.

* fix ElementCallServiceTests

* update enterprise

* re-generate mocks

* update enterprise

* enterprise new code conformed to swift 6

---------

Co-authored-by: Doug <douglase@element.io>
This commit is contained in:
Mauro
2026-06-18 17:55:31 +02:00
committed by GitHub
parent 7edca8b5a1
commit a520d94760
444 changed files with 3677 additions and 3361 deletions
@@ -11,7 +11,7 @@ import MatrixRustSDK
import MatrixRustSDKMocks
/// A light wrapper around timeline items returned from Rust.
enum TimelineItemProxy {
nonisolated enum TimelineItemProxy {
case event(EventTimelineItemProxy)
case virtual(MatrixRustSDK.VirtualTimelineItem, uniqueID: TimelineItemIdentifier.UniqueID)
case unknown(MatrixRustSDK.TimelineItem)
@@ -37,7 +37,7 @@ enum TimelineItemProxy {
}
/// The delivery status for the item.
enum TimelineItemDeliveryStatus: Hashable {
nonisolated enum TimelineItemDeliveryStatus: Hashable {
case sending
case sent
case sendingFailed(TimelineItemSendFailure)
@@ -51,7 +51,7 @@ enum TimelineItemDeliveryStatus: Hashable {
}
/// The reason a timeline item failed to send.
enum TimelineItemSendFailure: Hashable {
nonisolated enum TimelineItemSendFailure: Hashable {
enum VerifiedUser: Hashable {
case hasUnsignedDevice(devices: [String: [String]])
case changedIdentity(users: [String])
@@ -69,7 +69,11 @@ enum TimelineItemSendFailure: Hashable {
}
/// A light wrapper around event timeline items returned from Rust.
class EventTimelineItemProxy {
///
/// The proxy is shared between the main actor and the background timeline item
/// building, so it's stateless and forwards everything to the underlying
/// (internally synchronised) Rust object.
final nonisolated class EventTimelineItemProxy: Sendable {
let item: MatrixRustSDK.EventTimelineItem
let id: TimelineItemIdentifier
@@ -79,13 +83,13 @@ class EventTimelineItemProxy {
id = .event(uniqueID: uniqueID, eventOrTransactionID: .init(rustValue: item.eventOrTransactionId))
}
lazy var deliveryStatus: TimelineItemDeliveryStatus? = {
var deliveryStatus: TimelineItemDeliveryStatus? {
guard let localSendState = item.localSendState else {
return nil
}
switch localSendState {
case .sendingFailed(let error, let isRecoverable):
case .sendingFailed(let error, _):
switch error {
case .identityViolations(let users):
return .sendingFailed(.verifiedUser(.changedIdentity(users: users)))
@@ -99,42 +103,62 @@ class EventTimelineItemProxy {
case .sent:
return .sent
}
}()
}
lazy var canBeRepliedTo = item.canBeRepliedTo
var canBeRepliedTo: Bool {
item.canBeRepliedTo
}
lazy var content = item.content
var content: TimelineItemContent {
item.content
}
lazy var isOwn = item.isOwn
var isOwn: Bool {
item.isOwn
}
lazy var isEditable = item.isEditable
var isEditable: Bool {
item.isEditable
}
lazy var sender = TimelineItemSender(senderID: item.sender, senderProfile: item.senderProfile)
var sender: TimelineItemSender {
TimelineItemSender(senderID: item.sender, senderProfile: item.senderProfile)
}
lazy var forwarder: TimelineItemKeyForwarder? = {
var forwarder: TimelineItemKeyForwarder? {
guard let forwarderID = item.forwarder, let forwarderProfile = item.forwarderProfile else {
return nil
}
return TimelineItemKeyForwarder(forwarderID: forwarderID, forwarderProfile: forwarderProfile)
}()
}
lazy var timestamp = Date(timeIntervalSince1970: TimeInterval(item.timestamp / 1000))
var timestamp: Date {
Date(timeIntervalSince1970: TimeInterval(item.timestamp / 1000))
}
lazy var debugInfo: TimelineItemDebugInfo = {
var debugInfo: TimelineItemDebugInfo {
let debugInfo = item.lazyProvider.debugInfo()
return TimelineItemDebugInfo(model: debugInfo.model, originalJSON: debugInfo.originalJson, latestEditJSON: debugInfo.latestEditJson)
}()
}
lazy var shieldState = item.lazyProvider.getShields(strict: false)
var shieldState: ShieldState? {
item.lazyProvider.getShields(strict: false)
}
lazy var sendHandle = item.lazyProvider.getSendHandle()
var sendHandle: SendHandle? {
item.lazyProvider.getSendHandle()
}
lazy var shouldBoost = item.lazyProvider.containsOnlyEmojis()
var shouldBoost: Bool {
item.lazyProvider.containsOnlyEmojis()
}
lazy var readReceipts = item.readReceipts
var readReceipts: [String: Receipt] {
item.readReceipts
}
}
struct TimelineItemDebugInfo: Identifiable, CustomStringConvertible {
nonisolated struct TimelineItemDebugInfo: Identifiable, CustomStringConvertible {
let id = UUID()
let model: String
let originalJSON: String?
@@ -175,7 +199,7 @@ struct TimelineItemDebugInfo: Identifiable, CustomStringConvertible {
}
}
struct SendHandleProxy: Hashable {
nonisolated struct SendHandleProxy: Hashable {
enum Error: Swift.Error {
case sdkError(Swift.Error)
}
@@ -209,7 +233,7 @@ struct SendHandleProxy: Hashable {
}
}
struct VideoInfoProxy: Hashable {
nonisolated struct VideoInfoProxy: Hashable {
let source: MediaSourceProxy
private(set) var duration: TimeInterval
private(set) var size: CGSize?
@@ -253,7 +277,7 @@ struct VideoInfoProxy: Hashable {
}
}
struct ImageInfoProxy: Hashable {
nonisolated struct ImageInfoProxy: Hashable {
let source: MediaSourceProxy
private(set) var size: CGSize?
private(set) var aspectRatio: CGFloat?
@@ -317,7 +341,7 @@ struct ImageInfoProxy: Hashable {
}
}
private struct MediaInfoProxy: Hashable {
private nonisolated struct MediaInfoProxy: Hashable {
private(set) var size: CGSize?
private(set) var mimeType: String?
private(set) var aspectRatio: CGFloat?