Skip to content

Commit 48eab6d

Browse files
authored
Merge pull request swiftlang#39872 from DougGregor/mainactor-run-hop-back
2 parents ec1ab71 + 150f32c commit 48eab6d

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

lib/SILGen/SILGenProlog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ SILValue SILGenFunction::emitLoadGlobalActorExecutor(Type globalActor) {
642642
actorType->getTypeOfMember(SGM.SwiftModule, sharedInstanceDecl);
643643

644644
auto metaRepr =
645-
nominal->isResilient(SGM.SwiftModule, ResilienceExpansion::Maximal)
645+
nominal->isResilient(SGM.SwiftModule, F.getResilienceExpansion())
646646
? MetatypeRepresentation::Thick
647647
: MetatypeRepresentation::Thin;
648648

stdlib/public/Concurrency/MainActor.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,24 @@ import Swift
4545
@available(SwiftStdlib 5.5, *)
4646
extension MainActor {
4747
/// Execute the given body closure on the main actor.
48-
@_silgen_name("$sScM3run10resultType4bodyxxm_xyYbKScMYcXEtYaKlFZ")
49-
public static func run<T: Sendable>(
48+
///
49+
/// Historical ABI entry point, superceded by the Sendable version that is
50+
/// also inlined to back-deploy a semantic fix where this operation would
51+
/// not hop back at the end.
52+
@usableFromInline
53+
static func run<T>(
5054
resultType: T.Type = T.self,
5155
body: @MainActor @Sendable () throws -> T
5256
) async rethrows -> T {
53-
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) async rethrows -> T {
54-
return try body()
55-
}
57+
return try await body()
58+
}
5659

57-
return try await runOnMain(body: body)
60+
/// Execute the given body closure on the main actor.
61+
@_alwaysEmitIntoClient
62+
public static func run<T: Sendable>(
63+
resultType: T.Type = T.self,
64+
body: @MainActor @Sendable () throws -> T
65+
) async rethrows -> T {
66+
return try await body()
5867
}
5968
}

test/Concurrency/Runtime/mainactor.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ actor A {
8181
// CHECK-NOT: ERROR
8282
// CHECK: finished with return counter = 4
8383

84+
// CHECK: detached task not on main queue
85+
// CHECK: on main queue again
86+
// CHECK: detached task hopped back
87+
8488
@main struct RunIt {
8589
static func main() async {
8690
print("starting")
@@ -91,5 +95,25 @@ actor A {
9195
}
9296
let result = await someFunc()
9397
print("finished with return counter = \(result)")
98+
99+
// Check actor hopping with MainActor.run.
100+
let task = Task.detached {
101+
if checkIfMainQueue(expectedAnswer: false) {
102+
print("detached task not on main queue")
103+
} else {
104+
print("ERROR: detached task is on the main queue?")
105+
}
106+
107+
_ = await MainActor.run {
108+
checkAnotherFn(1)
109+
}
110+
111+
if checkIfMainQueue(expectedAnswer: false) {
112+
print("detached task hopped back")
113+
} else {
114+
print("ERROR: detached task is on the main queue?")
115+
}
116+
}
117+
_ = await task.value
94118
}
95119
}

test/api-digester/stability-concurrency-abi.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
// UNSUPPORTED: swift_evolve
5151

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

0 commit comments

Comments
 (0)