Skip to content

Commit 9aae417

Browse files
bjhomerDougGregor
authored andcommitted
Stop inferring actor isolation from property wrappers in Swift 6.
In Swift 5, isolation will continue, but will now produce a warning and a fix-it suggesting that the user add explicit isolation (cherry picked from commit 982e14b)
1 parent acf3671 commit 9aae417

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5364,6 +5364,12 @@ ERROR(async_unavailable_decl,none,
53645364
"%0 %1 is unavailable from asynchronous contexts%select{|; %2}2",
53655365
(DescriptiveDeclKind, DeclBaseName, StringRef))
53665366

5367+
WARNING(actor_isolation_inferred_from_property_wrapper,none,
5368+
"%0 is implicitly %1 because it uses %2. This implicit isolation "
5369+
"will no longer happen in Swift 6.",
5370+
(DeclName, ActorIsolation, StringRef))
5371+
5372+
53675373
//------------------------------------------------------------------------------
53685374
// MARK: String Processing
53695375
//------------------------------------------------------------------------------

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,8 +3622,16 @@ static Optional<ActorIsolation> getIsolationFromWrappers(
36223622

36233623
if (!nominal->getParentSourceFile())
36243624
return None;
3625-
3626-
Optional<ActorIsolation> foundIsolation;
3625+
3626+
ASTContext &ctx = nominal->getASTContext();
3627+
if (ctx.isSwiftVersionAtLeast(6)) {
3628+
// In Swift 6, we no longer infer isolation of a nominal type based
3629+
// on property wrappers used in its stored properties
3630+
return None;
3631+
}
3632+
3633+
Optional<std::pair<ActorIsolation, Type>> foundIsolationAndType;
3634+
36273635
for (auto member : nominal->getMembers()) {
36283636
auto var = dyn_cast<VarDecl>(member);
36293637
if (!var || !var->isInstanceMember())
@@ -3648,19 +3656,36 @@ static Optional<ActorIsolation> getIsolationFromWrappers(
36483656

36493657
case ActorIsolation::GlobalActor:
36503658
case ActorIsolation::GlobalActorUnsafe:
3651-
if (!foundIsolation) {
3652-
foundIsolation = isolation;
3653-
continue;
3659+
if (!foundIsolationAndType) {
3660+
if (auto propertyWrapperType = var->getAttachedPropertyWrapperType(0)) {
3661+
foundIsolationAndType = { isolation, propertyWrapperType };
3662+
continue;
3663+
}
36543664
}
36553665

3656-
if (*foundIsolation != isolation)
3666+
if (foundIsolationAndType->first != isolation)
36573667
return None;
36583668

36593669
break;
36603670
}
36613671
}
36623672

3663-
return foundIsolation;
3673+
if (foundIsolationAndType) {
3674+
// We are inferring isolation for the type because
3675+
// it contains an actor-isolated property wrapper.
3676+
// Warn that this inferrence will be going away in
3677+
// Swift 6
3678+
const ActorIsolation isolation = foundIsolationAndType->first;
3679+
const Type type = foundIsolationAndType->second;
3680+
3681+
nominal->diagnose(diag::actor_isolation_inferred_from_property_wrapper,
3682+
nominal->getName(), isolation, "'@"+type->getString()+"'")
3683+
.fixItInsert(nominal->getAttributeInsertionLoc(false), "@" + isolation.getGlobalActor().getString());
3684+
return isolation;
3685+
}
3686+
else {
3687+
return None;
3688+
}
36643689
}
36653690

36663691
namespace {
@@ -4236,8 +4261,8 @@ ActorIsolation ActorIsolationRequest::evaluate(
42364261
if (auto inferred = inferredIsolation(*conformanceIsolation))
42374262
return inferred;
42384263

4239-
// If the declaration is a nominal type and any property wrappers on
4240-
// its stored properties require isolation, use that.
4264+
// Before Swift 6: If the declaration is a nominal type and any property
4265+
// wrappers on its stored properties require isolation, use that.
42414266
if (auto wrapperIsolation = getIsolationFromWrappers(nominal)) {
42424267
if (auto inferred = inferredIsolation(*wrapperIsolation))
42434268
return inferred;

0 commit comments

Comments
 (0)