Skip to content

Commit 631a04e

Browse files
Merge pull request #80159 from AnthonyLatsis/danaus-plexippus-5
Sema: Extend adoption mode for `AsyncCallerExecution` to storage declarations
2 parents 06985fe + a56695b commit 631a04e

32 files changed

+310
-225
lines changed

include/swift/AST/Decl.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,6 +3197,11 @@ class ValueDecl : public Decl {
31973197
/// Is this declaration marked with 'dynamic'?
31983198
bool isDynamic() const;
31993199

3200+
/// Returns whether accesses to this declaration are asynchronous.
3201+
/// If the declaration is neither `AbstractFunctionDecl` nor
3202+
/// `AbstractStorageDecl`, returns `false`.
3203+
bool isAsync() const;
3204+
32003205
private:
32013206
bool isObjCDynamic() const {
32023207
return isObjC() && isDynamic();
@@ -3320,6 +3325,14 @@ class ValueDecl : public Decl {
33203325
/// parameter lists, for example an enum element without associated values.
33213326
bool hasParameterList() const;
33223327

3328+
/// Returns the parameter list directly associated with this declaration,
3329+
/// or `nullptr` if there is none.
3330+
///
3331+
/// Note that some declarations with function interface types do not have
3332+
/// parameter lists. For example, an enum element without associated values.
3333+
ParameterList *getParameterList();
3334+
const ParameterList *getParameterList() const;
3335+
33233336
/// Returns the number of curry levels in the declaration's interface type.
33243337
unsigned getNumCurryLevels() const;
33253338

@@ -9733,14 +9746,6 @@ inline bool ValueDecl::hasCurriedSelf() const {
97339746
return false;
97349747
}
97359748

9736-
inline bool ValueDecl::hasParameterList() const {
9737-
if (auto *eed = dyn_cast<EnumElementDecl>(this))
9738-
return eed->hasAssociatedValues();
9739-
if (auto *macro = dyn_cast<MacroDecl>(this))
9740-
return macro->parameterList != nullptr;
9741-
return isa<AbstractFunctionDecl>(this) || isa<SubscriptDecl>(this);
9742-
}
9743-
97449749
inline unsigned ValueDecl::getNumCurryLevels() const {
97459750
unsigned curryLevels = 0;
97469751
if (hasParameterList())
@@ -9828,10 +9833,6 @@ inline EnumElementDecl *EnumDecl::getUniqueElement(bool hasValue) const {
98289833
return result;
98299834
}
98309835

9831-
/// Retrieve the parameter list for a given declaration, or nullptr if there
9832-
/// is none.
9833-
ParameterList *getParameterList(ValueDecl *source);
9834-
98359836
/// Retrieve the parameter list for a given declaration context, or nullptr if
98369837
/// there is none.
98379838
ParameterList *getParameterList(DeclContext *source);

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ DECL_ATTR(abi, ABI,
862862
DECL_ATTR_FEATURE_REQUIREMENT(ABI, ABIAttribute)
863863

864864
DECL_ATTR(execution, Execution,
865-
OnAbstractFunction,
865+
OnFunc | OnConstructor | OnSubscript | OnVar,
866866
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
867867
166)
868868
DECL_ATTR_FEATURE_REQUIREMENT(Execution, ExecutionAttribute)

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ static unsigned getUnnamedParamIndex(const ParamDecl *D) {
11391139
if (isa<AbstractClosureExpr>(DC)) {
11401140
ParamList = cast<AbstractClosureExpr>(DC)->getParameters();
11411141
} else {
1142-
ParamList = getParameterList(cast<ValueDecl>(DC->getAsDecl()));
1142+
ParamList = cast<ValueDecl>(DC->getAsDecl())->getParameterList();
11431143
}
11441144

11451145
unsigned UnnamedIndex = 0;

lib/AST/Decl.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,8 +1337,7 @@ bool Decl::hasUnderscoredNaming() const {
13371337
// underscore, it's a private function or subscript.
13381338
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
13391339
const auto VD = cast<ValueDecl>(D);
1340-
if (getParameterList(const_cast<ValueDecl *>(VD))
1341-
->hasInternalParameter("_")) {
1340+
if (VD->getParameterList()->hasInternalParameter("_")) {
13421341
return true;
13431342
}
13441343
}
@@ -4117,6 +4116,26 @@ ValueDecl::getCachedOpaqueResultTypeDecl() const {
41174116
.getCachedResult();
41184117
}
41194118

4119+
ParameterList *ValueDecl::getParameterList() {
4120+
if (auto *function = dyn_cast<AbstractFunctionDecl>(this)) {
4121+
return function->getParameters();
4122+
} else if (auto *enumElement = dyn_cast<EnumElementDecl>(this)) {
4123+
return enumElement->getParameterList();
4124+
} else if (auto *subscript = dyn_cast<SubscriptDecl>(this)) {
4125+
return subscript->getIndices();
4126+
} else if (auto *macro = dyn_cast<MacroDecl>(this)) {
4127+
return macro->parameterList;
4128+
}
4129+
4130+
return nullptr;
4131+
}
4132+
4133+
const ParameterList *ValueDecl::getParameterList() const {
4134+
return const_cast<ValueDecl *>(this)->getParameterList();
4135+
}
4136+
4137+
bool ValueDecl::hasParameterList() const { return (bool)getParameterList(); }
4138+
41204139
bool ValueDecl::isObjC() const {
41214140
ASTContext &ctx = getASTContext();
41224141
return evaluateOrDefault(ctx.evaluator,
@@ -4182,6 +4201,24 @@ bool ValueDecl::isDynamic() const {
41824201
getAttrs().hasAttribute<DynamicAttr>());
41834202
}
41844203

4204+
bool ValueDecl::isAsync() const {
4205+
if (auto *function = dyn_cast<AbstractFunctionDecl>(this)) {
4206+
return function->hasAsync();
4207+
}
4208+
4209+
// Async storage declarations must be get-only. Don't consider it async
4210+
// otherwise, even if it has an async getter.
4211+
if (auto *storage = dyn_cast<AbstractStorageDecl>(this)) {
4212+
if (storage->getAllAccessors().size() == 1) {
4213+
if (auto *getter = storage->getAccessor(AccessorKind::Get)) {
4214+
return getter->hasAsync();
4215+
}
4216+
}
4217+
}
4218+
4219+
return false;
4220+
}
4221+
41854222
bool ValueDecl::isObjCDynamicInGenericClass() const {
41864223
if (!isObjCDynamic())
41874224
return false;
@@ -9599,24 +9636,10 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
95999636
return DeclName();
96009637
}
96019638

9602-
ParameterList *swift::getParameterList(ValueDecl *source) {
9603-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(source)) {
9604-
return AFD->getParameters();
9605-
} else if (auto *EED = dyn_cast<EnumElementDecl>(source)) {
9606-
return EED->getParameterList();
9607-
} else if (auto *SD = dyn_cast<SubscriptDecl>(source)) {
9608-
return SD->getIndices();
9609-
} else if (auto *MD = dyn_cast<MacroDecl>(source)) {
9610-
return MD->parameterList;
9611-
}
9612-
9613-
return nullptr;
9614-
}
9615-
96169639
ParameterList *swift::getParameterList(DeclContext *source) {
96179640
if (auto *D = source->getAsDecl()) {
96189641
if (auto *VD = dyn_cast<ValueDecl>(D)) {
9619-
return getParameterList(VD);
9642+
return VD->getParameterList();
96209643
}
96219644
} else if (auto *CE = dyn_cast<AbstractClosureExpr>(source)) {
96229645
return CE->getParameters();
@@ -9628,7 +9651,7 @@ ParameterList *swift::getParameterList(DeclContext *source) {
96289651
const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
96299652
unsigned index) {
96309653
auto *source = declRef.getDecl();
9631-
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
9654+
if (auto *params = source->getParameterList()) {
96329655
unsigned origIndex = params->getOrigParamIndex(declRef.getSubstitutions(),
96339656
index);
96349657
return params->get(origIndex);
@@ -9638,7 +9661,7 @@ const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
96389661

96399662
const ParamDecl *swift::getParameterAt(const ValueDecl *source,
96409663
unsigned index) {
9641-
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
9664+
if (auto *params = source->getParameterList()) {
96429665
return index < params->size() ? params->get(index) : nullptr;
96439666
}
96449667
return nullptr;

lib/AST/DeclContext.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
501501
dc = dc->getParent();
502502

503503
auto *VD = cast<ValueDecl>(dc->getAsDecl());
504-
assert(VD->hasParameterList());
504+
ASSERT(VD->hasParameterList());
505505

506506
if (VD->getDeclContext()->isLocalContext()) {
507507
auto kind = VD->getDeclContext()->getFragileFunctionKind();
@@ -1615,15 +1615,10 @@ bool DeclContext::isAsyncContext() const {
16151615
return getParent()->isAsyncContext();
16161616
case DeclContextKind::AbstractClosureExpr:
16171617
return cast<AbstractClosureExpr>(this)->isBodyAsync();
1618-
case DeclContextKind::AbstractFunctionDecl: {
1619-
const AbstractFunctionDecl *function = cast<AbstractFunctionDecl>(this);
1620-
return function->hasAsync();
1621-
}
1622-
case DeclContextKind::SubscriptDecl: {
1623-
AccessorDecl *getter =
1624-
cast<SubscriptDecl>(this)->getAccessor(AccessorKind::Get);
1625-
return getter != nullptr && getter->hasAsync();
1626-
}
1618+
case DeclContextKind::AbstractFunctionDecl:
1619+
return cast<AbstractFunctionDecl>(this)->hasAsync();
1620+
case DeclContextKind::SubscriptDecl:
1621+
return cast<SubscriptDecl>(this)->isAsync();
16271622
}
16281623
llvm_unreachable("Unhandled DeclContextKind switch");
16291624
}

lib/AST/FeatureSet.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,13 @@ static bool usesFeatureBuiltinEmplaceTypedThrows(Decl *decl) {
478478
}
479479

480480
static bool usesFeatureExecutionAttribute(Decl *decl) {
481-
if (auto *ASD = dyn_cast<AbstractStorageDecl>(decl)) {
482-
if (auto *getter = ASD->getAccessor(AccessorKind::Get))
483-
return usesFeatureExecutionAttribute(getter);
481+
if (!DeclAttribute::canAttributeAppearOnDecl(DeclAttrKind::Execution, decl)) {
484482
return false;
485483
}
486484

487485
if (decl->getAttrs().hasAttribute<ExecutionAttr>())
488486
return true;
489487

490-
auto VD = dyn_cast<ValueDecl>(decl);
491-
if (!VD)
492-
return false;
493-
494488
auto hasExecutionAttr = [](TypeRepr *R) {
495489
if (!R)
496490
return false;
@@ -508,8 +502,10 @@ static bool usesFeatureExecutionAttribute(Decl *decl) {
508502
});
509503
};
510504

505+
auto *VD = cast<ValueDecl>(decl);
506+
511507
// Check if any parameters that have `@execution` attribute.
512-
if (auto *PL = getParameterList(VD)) {
508+
if (auto *PL = VD->getParameterList()) {
513509
for (auto *P : *PL) {
514510
if (hasExecutionAttr(P->getTypeRepr()))
515511
return true;

lib/AST/NameLookup.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,13 +3714,14 @@ createOpaqueParameterGenericParams(GenericContext *genericContext, GenericParamL
37143714
return { };
37153715

37163716
// Functions, initializers, and subscripts can contain opaque parameters.
3717-
ParameterList *params = nullptr;
3718-
if (auto func = dyn_cast<AbstractFunctionDecl>(value))
3719-
params = func->getParameters();
3720-
else if (auto subscript = dyn_cast<SubscriptDecl>(value))
3721-
params = subscript->getIndices();
3722-
else
3717+
// FIXME: What's wrong with allowing them in macro decls?
3718+
if (isa<MacroDecl>(value)) {
37233719
return { };
3720+
}
3721+
auto *params = value->getParameterList();
3722+
if (!params) {
3723+
return {};
3724+
}
37243725

37253726
// Look for parameters that have "some" types in them.
37263727
unsigned index = parsedGenericParams ? parsedGenericParams->size() : 0;

lib/AST/RawComment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static bool hasDoubleUnderscore(const Decl *D) {
242242
// If it's a function or subscript with a parameter with leading
243243
// double underscore, it's a private function or subscript.
244244
if (isa<AbstractFunctionDecl>(D) || isa<SubscriptDecl>(D)) {
245-
auto *params = getParameterList(cast<ValueDecl>(const_cast<Decl *>(D)));
245+
auto *params = cast<ValueDecl>(D)->getParameterList();
246246
if (params->hasInternalParameter(Prefix))
247247
return true;
248248
}

lib/AST/Type.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,13 +1359,11 @@ ParameterListInfo::ParameterListInfo(
13591359
return;
13601360

13611361
// Find the corresponding parameter list.
1362-
const ParameterList *paramList =
1363-
getParameterList(const_cast<ValueDecl *>(paramOwner));
1362+
auto *paramList = paramOwner->getParameterList();
13641363

13651364
// No parameter list means no default arguments - hand back the zeroed
13661365
// bitvector.
13671366
if (!paramList) {
1368-
assert(!paramOwner->hasParameterList());
13691367
return;
13701368
}
13711369

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ static FuncDecl *getInsertFunc(NominalTypeDecl *decl,
8181
FuncDecl *insert = nullptr;
8282
for (auto candidate : inserts) {
8383
if (auto candidateMethod = dyn_cast<FuncDecl>(candidate)) {
84-
if (!candidateMethod->hasParameterList())
85-
continue;
8684
auto params = candidateMethod->getParameters();
8785
if (params->size() != 1)
8886
continue;
@@ -158,7 +156,7 @@ static ValueDecl *lookupOperator(NominalTypeDecl *decl, Identifier id,
158156
static ValueDecl *getEqualEqualOperator(NominalTypeDecl *decl) {
159157
auto isValid = [&](ValueDecl *equalEqualOp) -> bool {
160158
auto equalEqual = dyn_cast<FuncDecl>(equalEqualOp);
161-
if (!equalEqual || !equalEqual->hasParameterList())
159+
if (!equalEqual)
162160
return false;
163161
auto params = equalEqual->getParameters();
164162
if (params->size() != 2)
@@ -187,7 +185,7 @@ static FuncDecl *getMinusOperator(NominalTypeDecl *decl) {
187185

188186
auto isValid = [&](ValueDecl *minusOp) -> bool {
189187
auto minus = dyn_cast<FuncDecl>(minusOp);
190-
if (!minus || !minus->hasParameterList())
188+
if (!minus)
191189
return false;
192190
auto params = minus->getParameters();
193191
if (params->size() != 2)
@@ -218,7 +216,7 @@ static FuncDecl *getMinusOperator(NominalTypeDecl *decl) {
218216
static FuncDecl *getPlusEqualOperator(NominalTypeDecl *decl, Type distanceTy) {
219217
auto isValid = [&](ValueDecl *plusEqualOp) -> bool {
220218
auto plusEqual = dyn_cast<FuncDecl>(plusEqualOp);
221-
if (!plusEqual || !plusEqual->hasParameterList())
219+
if (!plusEqual)
222220
return false;
223221
auto params = plusEqual->getParameters();
224222
if (params->size() != 2)

lib/IDE/Formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class RangeWalker: protected ASTWalker {
518518
if (!handleBraces(cast<SubscriptDecl>(D)->getBracesRange(), ContextLoc))
519519
return Action::Stop();
520520
}
521-
auto *PL = getParameterList(cast<ValueDecl>(D));
521+
auto *PL = cast<ValueDecl>(D)->getParameterList();
522522
if (!handleParens(PL->getLParenLoc(), PL->getRParenLoc(), ContextLoc))
523523
return Action::Stop();
524524
} else if (auto *PGD = dyn_cast<PrecedenceGroupDecl>(D)) {

lib/IDE/SourceEntityWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ ASTWalker::PreWalkAction SemaAnnotator::walkToDeclPreProper(Decl *D) {
169169
};
170170

171171
if (isa<AbstractFunctionDecl>(VD) || isa<SubscriptDecl>(VD)) {
172-
auto ParamList = getParameterList(VD);
172+
auto ParamList = VD->getParameterList();
173173
if (!ReportParamList(ParamList))
174174
return Action::Stop();
175175
}

0 commit comments

Comments
 (0)