Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ SILValue SILGenFunction::emitLoadGlobalActorExecutor(Type globalActor) {
actorType->getTypeOfMember(SGM.SwiftModule, sharedInstanceDecl);

auto metaRepr =
nominal->isResilient(SGM.SwiftModule, ResilienceExpansion::Maximal)
nominal->isResilient(SGM.SwiftModule, F.getResilienceExpansion())
? MetatypeRepresentation::Thick
: MetatypeRepresentation::Thin;

Expand Down
21 changes: 15 additions & 6 deletions stdlib/public/Concurrency/MainActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,24 @@ import Swift
@available(SwiftStdlib 5.5, *)
extension MainActor {
/// Execute the given body closure on the main actor.
@_silgen_name("$sScM3run10resultType4bodyxxm_xyYbKScMYcXEtYaKlFZ")
public static func run<T: Sendable>(
///
/// Historical ABI entry point, superceded by the Sendable version that is
/// also inlined to back-deploy a semantic fix where this operation would
/// not hop back at the end.
@usableFromInline
static func run<T>(
resultType: T.Type = T.self,
body: @MainActor @Sendable () throws -> T
) async rethrows -> T {
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) async rethrows -> T {
return try body()
}
return try await body()
}

return try await runOnMain(body: body)
/// Execute the given body closure on the main actor.
@_alwaysEmitIntoClient
public static func run<T: Sendable>(
resultType: T.Type = T.self,
body: @MainActor @Sendable () throws -> T
) async rethrows -> T {
return try await body()
}
}
24 changes: 24 additions & 0 deletions test/Concurrency/Runtime/mainactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ actor A {
// CHECK-NOT: ERROR
// CHECK: finished with return counter = 4

// CHECK: detached task not on main queue
// CHECK: on main queue again
// CHECK: detached task hopped back

@main struct RunIt {
static func main() async {
print("starting")
Expand All @@ -91,5 +95,25 @@ actor A {
}
let result = await someFunc()
print("finished with return counter = \(result)")

// Check actor hopping with MainActor.run.
let task = Task.detached {
if checkIfMainQueue(expectedAnswer: false) {
print("detached task not on main queue")
} else {
print("ERROR: detached task is on the main queue?")
}

_ = await MainActor.run {
checkAnotherFn(1)
}

if checkIfMainQueue(expectedAnswer: false) {
print("detached task hopped back")
} else {
print("ERROR: detached task is on the main queue?")
}
}
_ = await task.value
}
}
2 changes: 1 addition & 1 deletion test/api-digester/stability-concurrency-abi.test
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
// UNSUPPORTED: swift_evolve

// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

Func MainActor.run(resultType:body:) has generic signature change from <T where T : Swift.Sendable> to <T>
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)