Skip to content

Commit f0a4571

Browse files
committed
[rbi] Ensure that we properly handle nonisolated(nonsending) for forward declared vars.
rdar://164042741
1 parent df6a05c commit f0a4571

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/SILOptimizer/Utils/SILIsolationInfo.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,19 @@ SILIsolationInfo SILIsolationInfo::get(SILInstruction *inst) {
965965
}
966966
}
967967

968+
if (auto *bbi = dyn_cast<BeginBorrowInst>(inst)) {
969+
if (bbi->isFromVarDecl()) {
970+
// See if we have the actual AST information on our instruction.
971+
if (auto *varDecl = bbi->getLoc().getAsASTNode<VarDecl>()) {
972+
auto isolation = swift::getActorIsolation(varDecl);
973+
if (isolation.getKind() == ActorIsolation::NonisolatedUnsafe) {
974+
return SILIsolationInfo::getDisconnected(
975+
true /*is nonisolated(unsafe)*/);
976+
}
977+
}
978+
}
979+
}
980+
968981
/// Consider non-Sendable metatypes to be task-isolated, so they cannot cross
969982
/// into another isolation domain.
970983
if (auto *mi = dyn_cast<MetatypeInst>(inst)) {

test/Concurrency/transfernonsendable_nonisolatedunsafe.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ func useAfterTransferLetSquelchedIndirectAddressOnly<T : ProvidesStaticValue>(_
329329
print(ns4)
330330
}
331331

332+
func testNonisolatedUnsafeForwardDeclaredVar<T: Sendable>(_ body: @escaping @Sendable () async throws -> T) throws -> T {
333+
nonisolated(unsafe) var result: Result<T, Error>!
334+
Task {
335+
do {
336+
result = .success(try await body())
337+
} catch {
338+
result = .failure(error)
339+
}
340+
}
341+
return try result.get()
342+
}
343+
332344
////////////////////////
333345
// MARK: Global Tests //
334346
////////////////////////

0 commit comments

Comments
 (0)