Skip to content

Commit f1a1998

Browse files
committed
[gardening]: refactor checkGlobalIsolation function & add comments
1 parent 96ee7a0 commit f1a1998

File tree

1 file changed

+57
-47
lines changed

1 file changed

+57
-47
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6198,56 +6198,66 @@ void swift::checkOverrideActorIsolation(ValueDecl *value) {
61986198

61996199
void swift::checkGlobalIsolation(VarDecl *var) {
62006200
const auto isolation = getActorIsolation(var);
6201-
if (var->getLoc() &&
6202-
var->getASTContext().LangOpts.hasFeature(Feature::GlobalConcurrency) &&
6203-
!isolation.isGlobalActor() &&
6204-
(isolation != ActorIsolation::NonisolatedUnsafe)) {
6205-
if (var->isGlobalStorage()) {
6206-
auto *diagVar = var;
6207-
if (auto *originalVar = var->getOriginalWrappedProperty()) {
6208-
diagVar = originalVar;
6209-
}
62106201

6211-
bool diagnosed = false;
6212-
if (var->isLet()) {
6213-
auto type = var->getInterfaceType();
6214-
diagnosed = diagnoseIfAnyNonSendableTypes(
6215-
type, SendableCheckContext(var->getDeclContext()),
6216-
/*inDerivedConformance=*/Type(), /*typeLoc=*/SourceLoc(),
6217-
/*diagnoseLoc=*/var->getLoc(), diag::shared_immutable_state_decl,
6218-
diagVar);
6219-
} else {
6220-
diagVar->diagnose(diag::shared_mutable_state_decl, diagVar)
6221-
.warnUntilSwiftVersion(6);
6222-
diagnosed = true;
6223-
}
6224-
6225-
// If we diagnosed this global, tack on notes to suggest potential courses
6226-
// of action.
6227-
if (diagnosed) {
6228-
if (!var->isLet()) {
6229-
auto diag =
6230-
diagVar->diagnose(diag::shared_state_make_immutable, diagVar);
6231-
SourceLoc fixItLoc = getFixItLocForVarToLet(diagVar);
6232-
if (fixItLoc.isValid()) {
6233-
diag.fixItReplace(fixItLoc, "let");
6234-
}
6235-
}
6202+
// Skip this if the relevant features aren't supported.
6203+
if (!var->getLoc() ||
6204+
!var->getASTContext().LangOpts.hasFeature(Feature::GlobalConcurrency))
6205+
return;
62366206

6237-
auto mainActor = var->getASTContext().getMainActorType();
6238-
if (mainActor) {
6239-
diagVar
6240-
->diagnose(diag::add_globalactor_to_decl, mainActor->getString(),
6241-
diagVar, mainActor)
6242-
.fixItInsert(diagVar->getAttributeInsertionLoc(false),
6243-
diag::insert_globalactor_attr, mainActor);
6244-
}
6245-
diagVar->diagnose(diag::shared_state_nonisolated_unsafe, diagVar)
6246-
.fixItInsert(diagVar->getAttributeInsertionLoc(true),
6247-
"nonisolated(unsafe) ");
6248-
}
6249-
}
6207+
// Skip if the decl is global actor-isolated or is unsafely opted-out.
6208+
if (isolation.isGlobalActor() || isolation.isNonisolatedUnsafe())
6209+
return;
6210+
6211+
// We're only concerned with global storage.
6212+
if (!var->isGlobalStorage())
6213+
return;
6214+
6215+
// At this point, we've found global state that may need to be diagnosed.
6216+
auto *diagVar = var;
6217+
6218+
// Look through property wrappers.
6219+
if (auto *originalVar = var->getOriginalWrappedProperty())
6220+
diagVar = originalVar;
6221+
6222+
bool diagnosed = false;
6223+
if (var->isLet()) {
6224+
// `let` variables are okay if they are of Sendable type.
6225+
auto type = var->getInterfaceType();
6226+
diagnosed = diagnoseIfAnyNonSendableTypes(
6227+
type, SendableCheckContext(var->getDeclContext()),
6228+
/*inDerivedConformance=*/Type(), /*typeLoc=*/SourceLoc(),
6229+
/*diagnoseLoc=*/var->getLoc(), diag::shared_immutable_state_decl,
6230+
diagVar);
6231+
} else {
6232+
diagVar->diagnose(diag::shared_mutable_state_decl, diagVar)
6233+
.warnUntilSwiftVersion(6);
6234+
diagnosed = true;
62506235
}
6236+
6237+
// If we didn't find anything to report, we're done.
6238+
if (!diagnosed)
6239+
return;
6240+
6241+
// If we diagnosed this global, tack on notes to suggest potential courses
6242+
// of action.
6243+
if (!var->isLet()) {
6244+
auto diag = diagVar->diagnose(diag::shared_state_make_immutable, diagVar);
6245+
SourceLoc fixItLoc = getFixItLocForVarToLet(diagVar);
6246+
if (fixItLoc.isValid())
6247+
diag.fixItReplace(fixItLoc, "let");
6248+
}
6249+
6250+
auto mainActor = var->getASTContext().getMainActorType();
6251+
if (mainActor) {
6252+
diagVar
6253+
->diagnose(diag::add_globalactor_to_decl, mainActor->getString(),
6254+
diagVar, mainActor)
6255+
.fixItInsert(diagVar->getAttributeInsertionLoc(false),
6256+
diag::insert_globalactor_attr, mainActor);
6257+
}
6258+
diagVar->diagnose(diag::shared_state_nonisolated_unsafe, diagVar)
6259+
.fixItInsert(diagVar->getAttributeInsertionLoc(true),
6260+
"nonisolated(unsafe) ");
62516261
}
62526262

62536263
bool swift::contextRequiresStrictConcurrencyChecking(

0 commit comments

Comments
 (0)