Skip to content

Commit c12a141

Browse files
committed
Revert "[concurrency] Split out the default inferred actor isolation computation into a helper from ActorIsolationRequest::evaluate."
This reverts commit 5d6131e.
1 parent 0c7c79d commit c12a141

File tree

1 file changed

+74
-88
lines changed

1 file changed

+74
-88
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 74 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5529,82 +5529,6 @@ static void addAttributesForActorIsolation(ValueDecl *value,
55295529
}
55305530
}
55315531

5532-
/// Determine the default isolation and isolation source for this declaration,
5533-
/// which may still be overridden by other inference rules.
5534-
static std::tuple<InferredActorIsolation, ValueDecl *,
5535-
std::optional<ActorIsolation>>
5536-
computeDefaultInferredActorIsolation(ValueDecl *value) {
5537-
auto &ctx = value->getASTContext();
5538-
5539-
// If we are supposed to infer main actor isolation by default for entities
5540-
// within our module, make our default isolation main actor.
5541-
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated) &&
5542-
value->getModuleContext() == ctx.MainModule) {
5543-
5544-
// Default global actor isolation does not apply to any declarations
5545-
// within actors and distributed actors.
5546-
bool inActorContext = false;
5547-
auto *dc = value->getInnermostDeclContext();
5548-
while (dc && !inActorContext) {
5549-
if (auto *nominal = dc->getSelfNominalTypeDecl()) {
5550-
inActorContext = nominal->isAnyActor();
5551-
}
5552-
dc = dc->getParent();
5553-
}
5554-
5555-
if (!inActorContext) {
5556-
// FIXME: deinit should be implicitly MainActor too.
5557-
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
5558-
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
5559-
isa<ConstructorDecl>(value)) {
5560-
return {{ActorIsolation::forMainActor(ctx), {}}, nullptr, {}};
5561-
}
5562-
}
5563-
}
5564-
5565-
// If we have an async function... by default we inherit isolation.
5566-
if (ctx.LangOpts.hasFeature(
5567-
Feature::NonIsolatedAsyncInheritsIsolationFromContext)) {
5568-
if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
5569-
func && func->hasAsync() &&
5570-
func->getModuleContext() == ctx.MainModule) {
5571-
return {{ActorIsolation::forNonisolated(false /*is unsafe*/), {}},
5572-
nullptr,
5573-
{}};
5574-
}
5575-
}
5576-
5577-
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
5578-
// A @Sendable function is assumed to be actor-independent.
5579-
if (func->isSendable()) {
5580-
return {
5581-
{ActorIsolation::forConcurrent(/*unsafe=*/false), {}}, nullptr, {}};
5582-
}
5583-
}
5584-
5585-
// When no other isolation applies, an actor's non-async init is independent
5586-
if (auto nominal = value->getDeclContext()->getSelfNominalTypeDecl())
5587-
if (nominal->isAnyActor())
5588-
if (auto ctor = dyn_cast<ConstructorDecl>(value))
5589-
if (!ctor->hasAsync())
5590-
return {{ActorIsolation::forConcurrent(/*unsafe=*/false), {}},
5591-
nullptr,
5592-
{}};
5593-
5594-
// Look for and remember the overridden declaration's isolation.
5595-
if (auto *overriddenValue = value->getOverriddenDeclOrSuperDeinit()) {
5596-
// Use the overridden decl's iso as the default isolation for this decl.
5597-
auto isolation = getOverriddenIsolationFor(value);
5598-
return {{isolation,
5599-
IsolationSource(overriddenValue, IsolationSource::Override)},
5600-
overriddenValue,
5601-
isolation};
5602-
}
5603-
5604-
// We did not find anything special, return unspecified.
5605-
return {{ActorIsolation::forUnspecified(), {}}, nullptr, {}};
5606-
}
5607-
56085532
InferredActorIsolation ActorIsolationRequest::evaluate(
56095533
Evaluator &evaluator, ValueDecl *value) const {
56105534
// If this declaration has actor-isolated "self", it's isolated to that
@@ -5648,7 +5572,7 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
56485572
value->getAttrs().add(preconcurrency);
56495573
}
56505574

5651-
if (auto *fd = dyn_cast<FuncDecl>(value)) {
5575+
if (FuncDecl *fd = dyn_cast<FuncDecl>(value)) {
56525576
// Main.main() and Main.$main are implicitly MainActor-protected.
56535577
// Any other isolation is an error.
56545578
std::optional<ActorIsolation> mainIsolation =
@@ -5681,11 +5605,74 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
56815605
IsolationSource(/*source*/ nullptr, IsolationSource::Explicit)};
56825606
}
56835607

5684-
InferredActorIsolation defaultIsolation;
5685-
ValueDecl *overriddenValue;
5686-
std::optional<ActorIsolation> overriddenIsolation;
5687-
std::tie(defaultIsolation, overriddenValue, overriddenIsolation) =
5688-
computeDefaultInferredActorIsolation(value);
5608+
// Determine the default isolation for this declaration, which may still be
5609+
// overridden by other inference rules.
5610+
ActorIsolation defaultIsolation = ActorIsolation::forUnspecified();
5611+
IsolationSource defaultIsolationSource;
5612+
5613+
// If we are supposed to infer main actor isolation by default for entities
5614+
// within our module, make our default isolation main actor.
5615+
if (ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated) &&
5616+
value->getModuleContext() == ctx.MainModule) {
5617+
5618+
// Default global actor isolation does not apply to any declarations
5619+
// within actors and distributed actors.
5620+
bool inActorContext = false;
5621+
auto *dc = value->getInnermostDeclContext();
5622+
while (dc && !inActorContext) {
5623+
if (auto *nominal = dc->getSelfNominalTypeDecl()) {
5624+
inActorContext = nominal->isAnyActor();
5625+
}
5626+
dc = dc->getParent();
5627+
}
5628+
5629+
if (!inActorContext) {
5630+
// FIXME: deinit should be implicitly MainActor too.
5631+
if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
5632+
isa<AbstractStorageDecl>(value) || isa<FuncDecl>(value) ||
5633+
isa<ConstructorDecl>(value)) {
5634+
defaultIsolation = ActorIsolation::forMainActor(ctx);
5635+
}
5636+
}
5637+
}
5638+
5639+
// If we have an async function... by default we inherit isolation.
5640+
if (ctx.LangOpts.hasFeature(
5641+
Feature::NonIsolatedAsyncInheritsIsolationFromContext)) {
5642+
if (auto *func = dyn_cast<AbstractFunctionDecl>(value);
5643+
func && func->hasAsync() &&
5644+
func->getModuleContext() == ctx.MainModule) {
5645+
defaultIsolation = ActorIsolation::forNonisolated(false /*is unsafe*/);
5646+
}
5647+
}
5648+
5649+
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
5650+
// A @Sendable function is assumed to be actor-independent.
5651+
if (func->isSendable()) {
5652+
defaultIsolation = ActorIsolation::forConcurrent(/*unsafe=*/false);
5653+
}
5654+
}
5655+
5656+
// When no other isolation applies, an actor's non-async init is independent
5657+
if (auto nominal = value->getDeclContext()->getSelfNominalTypeDecl())
5658+
if (nominal->isAnyActor())
5659+
if (auto ctor = dyn_cast<ConstructorDecl>(value))
5660+
if (!ctor->hasAsync())
5661+
defaultIsolation = ActorIsolation::forConcurrent(/*unsafe=*/false);
5662+
5663+
// Look for and remember the overridden declaration's isolation.
5664+
std::optional<ActorIsolation> overriddenIso;
5665+
ValueDecl *overriddenValue = value->getOverriddenDeclOrSuperDeinit();
5666+
if (overriddenValue) {
5667+
// use the overridden decl's iso as the default isolation for this decl.
5668+
defaultIsolation = getOverriddenIsolationFor(value);
5669+
defaultIsolationSource =
5670+
IsolationSource(overriddenValue, IsolationSource::Override);
5671+
overriddenIso = defaultIsolation;
5672+
}
5673+
5674+
// NOTE: After this point, the default has been set. Only touch the default
5675+
// isolation above this point since code below assumes it is now constant.
56895676

56905677
// Function used when returning an inferred isolation.
56915678
auto inferredIsolation = [&](ActorIsolation inferred,
@@ -5696,17 +5683,16 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
56965683
// if the inferred isolation is not valid, then carry-over the overridden
56975684
// declaration's isolation as this decl's inferred isolation.
56985685
switch (validOverrideIsolation(value, inferred, overriddenValue,
5699-
*overriddenIsolation)) {
5686+
*overriddenIso)) {
57005687
case OverrideIsolationResult::Allowed:
57015688
case OverrideIsolationResult::Sendable:
57025689
break;
57035690

57045691
case OverrideIsolationResult::Disallowed:
5705-
if (overriddenValue->hasClangNode() &&
5706-
overriddenIsolation->isUnspecified()) {
5707-
inferred = overriddenIsolation->withPreconcurrency(true);
5692+
if (overriddenValue->hasClangNode() && overriddenIso->isUnspecified()) {
5693+
inferred = overriddenIso->withPreconcurrency(true);
57085694
} else {
5709-
inferred = *overriddenIsolation;
5695+
inferred = *overriddenIso;
57105696
}
57115697
break;
57125698
}
@@ -5965,7 +5951,7 @@ InferredActorIsolation ActorIsolationRequest::evaluate(
59655951
}
59665952

59675953
// Default isolation for this member.
5968-
return defaultIsolation;
5954+
return {defaultIsolation, defaultIsolationSource};
59695955
}
59705956

59715957
bool HasIsolatedSelfRequest::evaluate(

0 commit comments

Comments
 (0)