@@ -5008,6 +5008,13 @@ static std::optional<unsigned> getIsolatedParamIndex(ValueDecl *value) {
5008
5008
return std::nullopt;
5009
5009
}
5010
5010
5011
+ static bool belongsToActor (ValueDecl *value) {
5012
+ if (auto nominal = value->getDeclContext ()->getSelfNominalTypeDecl ()) {
5013
+ return nominal->isAnyActor ();
5014
+ }
5015
+ return false ;
5016
+ }
5017
+
5011
5018
// / Verifies rules about `isolated` parameters for the given decl. There is more
5012
5019
// / checking about these in TypeChecker::checkParameterList.
5013
5020
// /
@@ -5041,75 +5048,53 @@ static void checkDeclWithIsolatedParameter(ValueDecl *value) {
5041
5048
}
5042
5049
}
5043
5050
5044
- ActorIsolation ActorIsolationRequest::evaluate (
5045
- Evaluator &evaluator, ValueDecl *value) const {
5046
- auto &ctx = value->getASTContext ();
5047
-
5048
- const bool hasIsolatedSelf =
5049
- evaluateOrDefault (evaluator, HasIsolatedSelfRequest{value}, false );
5050
-
5051
- auto addAttributesForActorIsolation = [&](ActorIsolation isolation) {
5052
- ASTContext &ctx = value->getASTContext ();
5053
- switch (isolation) {
5054
- case ActorIsolation::Nonisolated:
5055
- case ActorIsolation::NonisolatedUnsafe: {
5056
- value->getAttrs ().add (new (ctx) NonisolatedAttr (
5057
- isolation == ActorIsolation::NonisolatedUnsafe, /* implicit=*/ true ));
5058
- break ;
5059
- }
5060
- case ActorIsolation::GlobalActor: {
5061
- auto typeExpr = TypeExpr::createImplicit (isolation.getGlobalActor (), ctx);
5062
- auto attr = CustomAttr::create (ctx, SourceLoc (), typeExpr, /* implicit=*/ true );
5063
- value->getAttrs ().add (attr);
5051
+ static void addAttributesForActorIsolation (ValueDecl *value,
5052
+ ActorIsolation isolation) {
5053
+ ASTContext &ctx = value->getASTContext ();
5054
+ switch (isolation) {
5055
+ case ActorIsolation::Nonisolated:
5056
+ case ActorIsolation::NonisolatedUnsafe: {
5057
+ value->getAttrs ().add (new (ctx) NonisolatedAttr (
5058
+ isolation == ActorIsolation::NonisolatedUnsafe, /* implicit=*/ true ));
5059
+ break ;
5060
+ }
5061
+ case ActorIsolation::GlobalActor: {
5062
+ auto typeExpr = TypeExpr::createImplicit (isolation.getGlobalActor (), ctx);
5063
+ auto attr =
5064
+ CustomAttr::create (ctx, SourceLoc (), typeExpr, /* implicit=*/ true );
5065
+ value->getAttrs ().add (attr);
5064
5066
5065
- if (isolation.preconcurrency () && !value->getAttrs ().hasAttribute <PreconcurrencyAttr>()) {
5066
- auto preconcurrency = new (ctx) PreconcurrencyAttr (/* isImplicit*/ true );
5067
- value->getAttrs ().add (preconcurrency);
5068
- }
5069
- break ;
5067
+ if (isolation.preconcurrency () &&
5068
+ !value->getAttrs ().hasAttribute <PreconcurrencyAttr>()) {
5069
+ auto preconcurrency = new (ctx) PreconcurrencyAttr (/* isImplicit*/ true );
5070
+ value->getAttrs ().add (preconcurrency);
5070
5071
}
5072
+ break ;
5073
+ }
5071
5074
case ActorIsolation::Erased:
5072
5075
llvm_unreachable (" cannot add attributes for erased isolation" );
5073
5076
case ActorIsolation::ActorInstance: {
5074
5077
// Nothing to do. Default value for actors.
5075
- assert (hasIsolatedSelf );
5078
+ assert (belongsToActor (value) );
5076
5079
break ;
5077
5080
}
5078
5081
case ActorIsolation::Unspecified: {
5079
5082
// Nothing to do. Default value for non-actors.
5080
- assert (!hasIsolatedSelf );
5083
+ assert (!belongsToActor (value) );
5081
5084
break ;
5082
5085
}
5083
5086
}
5084
- };
5085
-
5086
- auto isolationFromAttr = getIsolationFromAttributes (value);
5087
-
5088
- // No need to isolate implicit deinit, unless there is already an isolated one
5089
- // in the superclass
5090
- if (isa<DestructorDecl>(value)) {
5091
- if (value->isImplicit () && !isolationFromAttr) {
5092
- ValueDecl *overriddenValue = value->getOverriddenDeclOrSuperDeinit ();
5093
- ActorIsolation isolation = ActorIsolation::forUnspecified ();
5094
- if (overriddenValue) {
5095
- isolation = getOverriddenIsolationFor (value);
5096
- }
5097
-
5098
- if (hasIsolatedSelf && isolation.isUnspecified ()) {
5099
- // Don't use 'unspecified' for actors, use 'nonisolated' instead.
5100
- // To force generation of the 'nonisolated' attribute in SIL and
5101
- // .swiftmodule
5102
- isolation = ActorIsolation::forNonisolated (false );
5103
- }
5087
+ }
5104
5088
5105
- addAttributesForActorIsolation (isolation);
5106
- return isolation;
5107
- }
5108
- }
5089
+ static bool isImplicitDeinit (ValueDecl *value) {
5090
+ return isa<DestructorDecl>(value) && value->isImplicit ();
5091
+ }
5109
5092
5093
+ ActorIsolation ActorIsolationRequest::evaluate (Evaluator &evaluator,
5094
+ ValueDecl *value) const {
5110
5095
// If this declaration has actor-isolated "self", it's isolated to that
5111
5096
// actor.
5112
- if (hasIsolatedSelf ) {
5097
+ if (evaluateOrDefault (evaluator, HasIsolatedSelfRequest{value}, false ) ) {
5113
5098
auto actor = value->getDeclContext ()->getSelfNominalTypeDecl ();
5114
5099
assert (actor && " could not find the actor that 'self' is isolated to" );
5115
5100
@@ -5120,7 +5105,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
5120
5105
actor->getDeclContext ()->isModuleScopeContext () &&
5121
5106
actor->getDeclContext ()->getParentModule ()->getABIName ().is (" Swift" )) {
5122
5107
auto isolation = ActorIsolation::forNonisolated (false );
5123
- addAttributesForActorIsolation (isolation);
5108
+ addAttributesForActorIsolation (value, isolation);
5124
5109
return isolation;
5125
5110
}
5126
5111
return ActorIsolation::forActorInstanceSelf (value);
@@ -5144,6 +5129,8 @@ ActorIsolation ActorIsolationRequest::evaluate(
5144
5129
return ActorIsolation::forActorInstanceParameter (param, *paramIdx);
5145
5130
}
5146
5131
5132
+ auto isolationFromAttr = getIsolationFromAttributes (value);
5133
+
5147
5134
// Diagnose global state that is not either immutable plus Sendable or
5148
5135
// isolated to a global actor.
5149
5136
auto checkGlobalIsolation = [var = dyn_cast<VarDecl>(value)](
@@ -5200,6 +5187,7 @@ ActorIsolation ActorIsolationRequest::evaluate(
5200
5187
return isolation;
5201
5188
};
5202
5189
5190
+ ASTContext &ctx = value->getASTContext ();
5203
5191
if (isolationFromAttr && isolationFromAttr->preconcurrency () &&
5204
5192
!value->getAttrs ().hasAttribute <PreconcurrencyAttr>()) {
5205
5193
auto preconcurrency =
@@ -5303,14 +5291,14 @@ ActorIsolation ActorIsolationRequest::evaluate(
5303
5291
}
5304
5292
5305
5293
// Add nonisolated attribute
5306
- addAttributesForActorIsolation (inferred);
5294
+ addAttributesForActorIsolation (value, inferred);
5307
5295
break ;
5308
5296
5309
5297
case ActorIsolation::Erased:
5310
5298
llvm_unreachable (" cannot infer erased isolation" );
5311
5299
case ActorIsolation::GlobalActor: {
5312
5300
// Add global actor attribute
5313
- addAttributesForActorIsolation (inferred);
5301
+ addAttributesForActorIsolation (value, inferred);
5314
5302
break ;
5315
5303
}
5316
5304
@@ -5455,8 +5443,14 @@ ActorIsolation ActorIsolationRequest::evaluate(
5455
5443
// If the declaration is in a nominal type (or extension thereof) that
5456
5444
// has isolation, use that.
5457
5445
if (auto selfTypeDecl = value->getDeclContext ()->getSelfNominalTypeDecl ()) {
5458
- if (auto selfTypeIsolation = getActorIsolation (selfTypeDecl))
5459
- return inferredIsolation (selfTypeIsolation, onlyGlobal);
5446
+ if (auto selfTypeIsolation = getActorIsolation (selfTypeDecl)) {
5447
+ if (isImplicitDeinit (value)) {
5448
+ return inferredIsolation (ActorIsolation::forUnspecified (),
5449
+ onlyGlobal);
5450
+ } else {
5451
+ return inferredIsolation (selfTypeIsolation, onlyGlobal);
5452
+ }
5453
+ }
5460
5454
}
5461
5455
}
5462
5456
@@ -5552,6 +5546,24 @@ bool HasIsolatedSelfRequest::evaluate(
5552
5546
return false ;
5553
5547
}
5554
5548
5549
+ if (isImplicitDeinit (value)) {
5550
+ // Actors don't have inheritance (except inheriting from NSObject),
5551
+ // but check for it anyway, just in case it will be re-introduced later.
5552
+ ValueDecl *overriddenValue = value->getOverriddenDeclOrSuperDeinit ();
5553
+ if (overriddenValue) {
5554
+ ActorIsolation isolation = getOverriddenIsolationFor (value);
5555
+ if (isolation.isActorIsolated ()) {
5556
+ return isolation.getKind () == ActorIsolation::ActorInstance;
5557
+ }
5558
+ }
5559
+
5560
+ // No need to isolate implicit deinit, unless there is already an isolated
5561
+ // one in the superclass
5562
+ addAttributesForActorIsolation (value,
5563
+ ActorIsolation::forNonisolated (false ));
5564
+ return false ;
5565
+ }
5566
+
5555
5567
return true ;
5556
5568
}
5557
5569
0 commit comments