Skip to content

[region-isolation] Check if we have a self param before attempting to get the self type. #74529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
16 changes: 9 additions & 7 deletions lib/SILOptimizer/Utils/SILIsolationInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,16 @@ SILIsolationInfo SILIsolationInfo::get(SILInstruction *inst) {
//
auto *func = fri->getReferencedFunction();
auto funcType = func->getLoweredFunctionType();
auto selfParam = funcType->getSelfInstanceType(
fri->getModule(), func->getTypeExpansionContext());
if (auto *nomDecl = selfParam->getNominalOrBoundGenericNominal()) {
auto isolation = swift::getActorIsolation(nomDecl);
if (isolation.isGlobalActor()) {
return SILIsolationInfo::getGlobalActorIsolated(
fri, isolation.getGlobalActor())
if (funcType->hasSelfParam()) {
auto selfParam = funcType->getSelfInstanceType(
fri->getModule(), func->getTypeExpansionContext());
if (auto *nomDecl = selfParam->getNominalOrBoundGenericNominal()) {
auto isolation = swift::getActorIsolation(nomDecl);
if (isolation.isGlobalActor()) {
return SILIsolationInfo::getGlobalActorIsolated(
fri, isolation.getGlobalActor())
.withUnsafeNonIsolated(true);
}
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion test/Concurrency/transfernonsendable_nonisolatedunsafe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protocol ProvidesStaticValue {
@MainActor func transferToMainIndirectConsuming<T>(_ t: consuming T) async {}
@MainActor func transferToMainDirectConsuming(_ t: consuming NonSendableKlass) async {}

func useInOut<T>(_ t: inout T) {}

actor CustomActorInstance {}

@globalActor
Expand Down Expand Up @@ -349,13 +351,28 @@ func testAccessStaticGlobals() async {
nonisolated(unsafe) let globalNonIsolatedUnsafeLetObject = NonSendableKlass()
nonisolated(unsafe) var globalNonIsolatedUnsafeVarObject = NonSendableKlass()

func testAccessGlobals() async {
func testPassGlobalToMainActorIsolatedFunction() async {
await transferToMainDirect(globalNonIsolatedUnsafeLetObject)
await transferToMainIndirect(globalNonIsolatedUnsafeLetObject)
await transferToMainDirect(globalNonIsolatedUnsafeVarObject)
await transferToMainIndirect(globalNonIsolatedUnsafeVarObject)
}

// We use this to force the modify in testPassGlobalToModify
nonisolated(unsafe)
var computedGlobalNonIsolatedUnsafeVarObject : NonSendableKlass {
_read {
yield globalNonIsolatedUnsafeVarObject
}
_modify {
yield &globalNonIsolatedUnsafeVarObject
}
}

func testPassGlobalToModify() async {
useInOut(&computedGlobalNonIsolatedUnsafeVarObject)
}

///////////////////////
// MARK: Field Tests //
///////////////////////
Expand Down