@@ -9405,7 +9405,18 @@ void ParamDecl::setDefaultExpr(Expr *E) {
94059405}
94069406
94079407void ParamDecl::setTypeCheckedDefaultExpr (Expr *E) {
9408- assert (E || getDefaultArgumentKind () == DefaultArgumentKind::Inherited);
9408+ // The type-checker will only produce a null expression here if the
9409+ // default argument is inherited, so if we're called with a null pointer
9410+ // in any other case, it must be from a request cycle. Don't crash;
9411+ // just wrap the original expression with an ErrorExpr and proceed.
9412+ if (!E && getDefaultArgumentKind () != DefaultArgumentKind::Inherited) {
9413+ auto *initExpr = getStructuralDefaultExpr ();
9414+ assert (initExpr);
9415+ auto &ctx = getASTContext ();
9416+ E = new (ctx) ErrorExpr (initExpr->getSourceRange (), ErrorType::get (ctx),
9417+ initExpr);
9418+ }
9419+
94099420 setDefaultExpr (E);
94109421
94119422 auto *defaultInfo = DefaultValueAndFlags.getPointer ();
@@ -11800,6 +11811,12 @@ PrecedenceGroupDecl *InfixOperatorDecl::getPrecedenceGroup() const {
1180011811 nullptr );
1180111812}
1180211813
11814+ bool ValueDecl::isDeferBody () const {
11815+ if (auto fn = dyn_cast<FuncDecl>(this ))
11816+ return fn->isDeferBody ();
11817+ return false ;
11818+ }
11819+
1180311820bool FuncDecl::isDeferBody () const {
1180411821 return getBaseIdentifier () == getASTContext ().getIdentifier (" $defer" );
1180511822}
@@ -11991,55 +12008,93 @@ ActorIsolation swift::getActorIsolationOfContext(
1199112008 DeclContext *dc,
1199212009 llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
1199312010 getClosureActorIsolation) {
11994- auto &ctx = dc->getASTContext ();
1199512011 auto dcToUse = dc;
11996- // Defer bodies share actor isolation of their enclosing context.
11997- if (auto FD = dyn_cast<FuncDecl>(dcToUse)) {
11998- if (FD->isDeferBody ()) {
11999- dcToUse = FD->getDeclContext ();
12000- }
12012+
12013+ // Defer bodies share the actor isolation of their enclosing context.
12014+ // We don't actually have to do this check here because
12015+ // getActorIsolation does consider it already, but it's nice to
12016+ // avoid some extra request evaluation in a trivial case.
12017+ while (auto FD = dyn_cast<FuncDecl>(dcToUse)) {
12018+ if (!FD->isDeferBody ()) break ;
12019+ dcToUse = FD->getDeclContext ();
1200112020 }
12021+
1200212022 if (auto *vd = dyn_cast_or_null<ValueDecl>(dcToUse->getAsDecl ()))
1200312023 return getActorIsolation (vd);
1200412024
12005- // In the context of the initializing or default-value expression of a
12006- // stored property:
12007- // - For a static stored property, the isolation matches the VarDecl.
12008- // Static properties are initialized upon first use, so the isolation
12009- // of the initializer must match the isolation required to access the
12010- // property.
12011- // - For a field of a nominal type, the expression can require the same
12012- // actor isolation as the field itself. That default expression may only
12013- // be used from inits that meet the required isolation.
12014- if (auto *var = dcToUse->getNonLocalVarDecl ()) {
12015- // If IsolatedDefaultValues are enabled, treat this context as having
12016- // unspecified isolation. We'll compute the required isolation for
12017- // the initializer and validate that it matches the isolation of the
12018- // var itself in the DefaultInitializerIsolation request.
12019- if (ctx.LangOpts .hasFeature (Feature::IsolatedDefaultValues))
12020- return ActorIsolation::forUnspecified ();
12021-
12022- return getActorIsolation (var);
12023- }
12024-
1202512025 if (auto *closure = dyn_cast<AbstractClosureExpr>(dcToUse)) {
1202612026 return getClosureActorIsolation (closure);
1202712027 }
1202812028
12029+ if (auto *init = dyn_cast<Initializer>(dcToUse)) {
12030+ // FIXME: force default argument initializers to report a meaningless
12031+ // isolation in order to break a bunch of cycles with the way that
12032+ // isolation is computed for them.
12033+ return getActorIsolation (init, /* ignoreDefaultArguments*/ true );
12034+ }
12035+
1202912036 if (isa<TopLevelCodeDecl>(dcToUse)) {
12037+ auto &ctx = dc->getASTContext ();
1203012038 if (dcToUse->isAsyncContext () ||
12031- dcToUse->getASTContext ().LangOpts .StrictConcurrencyLevel >=
12032- StrictConcurrency::Complete) {
12033- if (Type mainActor = dcToUse->getASTContext ().getMainActorType ())
12039+ ctx.LangOpts .StrictConcurrencyLevel >= StrictConcurrency::Complete) {
12040+ if (Type mainActor = ctx.getMainActorType ())
1203412041 return ActorIsolation::forGlobalActor (mainActor)
12035- .withPreconcurrency (
12036- !dcToUse->getASTContext ().isSwiftVersionAtLeast (6 ));
12042+ .withPreconcurrency (!ctx.isSwiftVersionAtLeast (6 ));
1203712043 }
1203812044 }
1203912045
1204012046 return ActorIsolation::forUnspecified ();
1204112047}
1204212048
12049+ ActorIsolation swift::getActorIsolation (Initializer *init,
12050+ bool ignoreDefaultArguments) {
12051+ switch (init->getInitializerKind ()) {
12052+ case InitializerKind::PatternBinding:
12053+ // In the context of the initializing or default-value expression of a
12054+ // stored property:
12055+ // - For a static stored property, the isolation matches the VarDecl.
12056+ // Static properties are initialized upon first use, so the isolation
12057+ // of the initializer must match the isolation required to access the
12058+ // property.
12059+ // - For a field of a nominal type, the expression can require the same
12060+ // actor isolation as the field itself. That default expression may only
12061+ // be used from inits that meet the required isolation.
12062+ if (auto *var = init->getNonLocalVarDecl ()) {
12063+ auto &ctx = var->getASTContext ();
12064+
12065+ // If IsolatedDefaultValues are enabled, treat this context as having
12066+ // unspecified isolation. We'll compute the required isolation for
12067+ // the initializer and validate that it matches the isolation of the
12068+ // var itself in the DefaultInitializerIsolation request.
12069+ if (ctx.LangOpts .hasFeature (Feature::IsolatedDefaultValues))
12070+ return ActorIsolation::forUnspecified ();
12071+
12072+ return getActorIsolation (var);
12073+ }
12074+
12075+ return ActorIsolation::forUnspecified ();
12076+
12077+ case InitializerKind::DefaultArgument: {
12078+ auto defArgInit = cast<DefaultArgumentInitializer>(init);
12079+
12080+ // A hack when used from getActorIsolationOfContext to maintain the
12081+ // current behavior and avoid request cycles.
12082+ if (ignoreDefaultArguments)
12083+ return ActorIsolation::forUnspecified ();
12084+
12085+ auto fn = cast<ValueDecl>(defArgInit->getParent ()->getAsDecl ());
12086+ auto param = getParameterAt (fn, defArgInit->getIndex ());
12087+ assert (param);
12088+ return param->getInitializerIsolation ();
12089+ }
12090+
12091+ case InitializerKind::PropertyWrapper:
12092+ case InitializerKind::CustomAttribute:
12093+ return ActorIsolation::forUnspecified ();
12094+ }
12095+ llvm_unreachable (" bad initializer kind" );
12096+ }
12097+
1204312098bool swift::isSameActorIsolated (ValueDecl *value, DeclContext *dc) {
1204412099 auto valueIsolation = getActorIsolation (value);
1204512100 auto dcIsolation = getActorIsolationOfContext (dc);
0 commit comments