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
+18 -17
View File
@@ -21,7 +21,7 @@ import {{ import }}
{% macro accessLevel level %}{% if level != 'internal' %}{{ level }} {% endif %}{% endmacro %}
{% macro staticSpecifier method %}{% if method.isStatic and not method.isInitializer %}static {% endif %}{% endmacro %}
{% macro methodThrowableErrorDeclaration method %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ThrowableError: Error?
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}ThrowableError: Error?
{% endmacro %}
{% macro methodThrowableErrorUsage method %}
if let error = {% call swiftifyMethodName method.selectorName %}ThrowableError {
@@ -47,7 +47,7 @@ import {{ import }}
{% macro methodClosureName method %}{% call swiftifyMethodName method.selectorName %}Closure{% endmacro %}
{% macro closureReturnTypeName method %}{% if method.isOptionalReturnType %}{{ method.unwrappedReturnTypeName }}?{% else %}{{ method.returnTypeName }}{% endif %}{% endmacro %}
{% macro methodClosureDeclaration method %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call methodClosureName method %}: (({% for param in method.parameters %}{{ param.typeName }}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.isAsync %}async {% endif %}{% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call closureReturnTypeName method %}{% endif %})?
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}nonisolated(unsafe) var {% call methodClosureName method %}: (({% for param in method.parameters %}{{ param.typeName }}{% if not forloop.last %}, {% endif %}{% endfor %}) {% if method.isAsync %}async {% endif %}{% if method.throws %}throws {% endif %}-> {% if method.isInitializer %}Void{% else %}{% call closureReturnTypeName method %}{% endif %})?
{% endmacro %}
{% macro methodClosureCallParameters method %}{% for param in method.parameters %}{{ param.name }}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
{% macro mockMethod method %}
@@ -58,7 +58,7 @@ import {{ import }}
{% endif %}
{% if not method.isInitializer %}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}CallsCountLock = NSLock()
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}CallsCount: Int {
get { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount } }
set { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue } }
@@ -74,26 +74,26 @@ import {{ import }}
{% endset %}
{% if method.parameters.count == 1 and not hasNonEscapingClosures %}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}Lock = NSLock(){% endfor %}
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceived{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %}
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingReceived{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} {
get { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} } }
set { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} = newValue } }
}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock()
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = []
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = []
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} {
get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } }
set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } }
}
{% elif not method.parameters.count == 0 and not hasNonEscapingClosures %}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock = NSLock()
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})?
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})?
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? {
get { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments } }
set { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments = newValue } }
}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock()
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = []
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = []
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] {
get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } }
set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } }
@@ -102,7 +102,7 @@ import {{ import }}
{% if not method.returnTypeName.isVoid and not method.isInitializer %}
private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReturnValueLock = NSLock()
private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }}
private {% call staticSpecifier method %}nonisolated(unsafe) var {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} {
get { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue } }
set { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue } }
@@ -121,7 +121,8 @@ import {{ import }}
{{ value }}
{% endfor %}
{% endfor %}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}func {{ method.name }}{{ ' async' if method.isAsync }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} {
{# @concurrent keeps async mocks suspending like the real implementations would, otherwise with caller isolation inherited by default, intermediate states are never observable. #}
{% call accessLevel method.accessLevel %}{% call staticSpecifier method %}{{ '@concurrent ' if method.isAsync }}func {{ method.name }}{{ ' async' if method.isAsync }}{{ ' throws' if method.throws }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} {
{% if method.throws %}
{% call methodThrowableErrorUsage method %}
{% endif %}
@@ -160,10 +161,10 @@ import {{ import }}
{% endif %}
{% endmacro %}
{% macro mockOptionalVariable variable %}
{% if variable.name == 'delegate' %}weak {% endif %}{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }}
{% if variable.name == 'delegate' %}weak {% endif %}{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call mockedVariableName variable %}: {{ variable.typeName }}
{% endmacro %}
{% macro mockNonOptionalArrayOrDictionaryVariable variable %}
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }} = {% if variable.isArray %}[]{% elif variable.isDictionary %}[:]{% endif %}
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call mockedVariableName variable %}: {{ variable.typeName }} = {% if variable.isArray %}[]{% elif variable.isDictionary %}[:]{% endif %}
{% endmacro %}
{% macro mockNonOptionalVariable variable %}
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}: {{ variable.typeName }} {
@@ -171,10 +172,10 @@ import {{ import }}
set(value) { {% call underlyingMockedVariableName variable %} = value }
}
{% set wrappedTypeName %}{% if variable.typeName.isProtocolComposition %}({{ variable.typeName }}){% else %}{{ variable.typeName }}{% endif %}{% endset %}
{% call accessLevel variable.readAccess %}var {% call underlyingMockedVariableName variable %}: {{ wrappedTypeName }}!
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call underlyingMockedVariableName variable %}: {{ wrappedTypeName }}!
{% endmacro %}
{% macro variableThrowableErrorDeclaration variable %}
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}ThrowableError: Error?
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call mockedVariableName variable %}ThrowableError: Error?
{% endmacro %}
{% macro variableThrowableErrorUsage variable %}
if let error = {% call mockedVariableName variable %}ThrowableError {
@@ -182,11 +183,11 @@ import {{ import }}
}
{% endmacro %}
{% macro variableClosureDeclaration variable %}
{% call accessLevel variable.readAccess %}var {% call variableClosureName variable %}: (() {% if variable.isAsync %}async {% endif %}{% if variable.throws %}throws {% endif %}-> {{ variable.typeName }})?
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call variableClosureName variable %}: (() {% if variable.isAsync %}async {% endif %}{% if variable.throws %}throws {% endif %}-> {{ variable.typeName }})?
{% endmacro %}
{% macro variableClosureName variable %}{% call mockedVariableName variable %}Closure{% endmacro %}
{% macro mockAsyncOrThrowingVariable variable %}
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}CallsCount = 0
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call mockedVariableName variable %}CallsCount = 0
{% call accessLevel variable.readAccess %}var {% call mockedVariableName variable %}Called: Bool {
return {% call mockedVariableName variable %}CallsCount > 0
}
@@ -204,7 +205,7 @@ import {{ import }}
}
}
}
{% call accessLevel variable.readAccess %}var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}{{ '!' if not variable.isOptional }}
{% call accessLevel variable.readAccess %}nonisolated(unsafe) var {% call underlyingMockedVariableName variable %}: {{ variable.typeName }}{{ '!' if not variable.isOptional }}
{% if variable.throws %}
{% call variableThrowableErrorDeclaration variable %}
{% endif %}
@@ -213,7 +214,7 @@ import {{ import }}
{% macro underlyingMockedVariableName variable %}underlying{{ variable.name|upperFirstLetter }}{% endmacro %}
{% macro mockedVariableName variable %}{{ variable.name }}{% endmacro %}
{% for type in types.protocols where type.based.AutoMockable or type|annotated:"AutoMockable" %}{% if type.name != "AutoMockable" %}
{% call accessLevel type.accessLevel %}class {{ type.name | replace:"Protocol","" }}Mock: {{ type.name }}, @unchecked Sendable {
{% call accessLevel type.accessLevel %}nonisolated class {{ type.name | replace:"Protocol","" }}Mock: {{ type.name }}, @unchecked Sendable {
{% if type.accessLevel == "public" %}public init() {}{% endif %}
{% for variable in type.allVariables|!definedInExtension %}
{% if variable.isAsync or variable.throws %}{% call mockAsyncOrThrowingVariable variable %}{% elif variable.isOptional %}{% call mockOptionalVariable variable %}{% elif variable.isArray or variable.isDictionary %}{% call mockNonOptionalArrayOrDictionaryVariable variable %}{% else %}{% call mockNonOptionalVariable variable %}{% endif %}
@@ -59,7 +59,7 @@ import Foundation
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
{{accessModifier}} enum {{enumName}} {
{{accessModifier}} nonisolated enum {{enumName}} {
{% if tables.count > 1 or param.forceFileNameEnum %}
{% for table in tables %}
{% filter indent:2 %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
@@ -73,7 +73,7 @@ import Foundation
// MARK: - Implementation Details
extension {{enumName}} {
nonisolated extension {{enumName}} {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
// No need to check languages, we always default to en for untranslated strings
guard let bundle = Bundle.lprojBundle(for: "en") else { return key }
@@ -59,7 +59,7 @@ import Foundation
// swiftlint:disable explicit_type_interface function_parameter_count identifier_name line_length
// swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces
{% set enumName %}{{param.enumName|default:"L10n"}}{% endset %}
{{accessModifier}} enum {{enumName}} {
{{accessModifier}} nonisolated enum {{enumName}} {
{% if tables.count > 1 or param.forceFileNameEnum %}
{% for table in tables %}
{% filter indent:2 %}{% call recursiveBlock table.name table.levels %}{% endfilter %}
@@ -73,7 +73,7 @@ import Foundation
// MARK: - Implementation Details
extension {{enumName}} {
nonisolated extension {{enumName}} {
static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
// Use preferredLocalizations to get a language that is in the bundle and the user's preferred list of languages.
let languages = Bundle.overrideLocalizations ?? Bundle.app.preferredLocalizations