Skip to content

Commit 235be5e

Browse files
committed
[Concurrency] Factor "is @asyncHandler"? check into a request.
This is stating for inference of @asyncHandler.
1 parent 8cd892a commit 235be5e

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5892,9 +5892,12 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
58925892
///
58935893
/// Functions that are an 'async' context can make calls to 'async' functions.
58945894
bool isAsyncContext() const {
5895-
return hasAsync() || getAttrs().hasAttribute<AsyncHandlerAttr>();
5895+
return hasAsync() || isAsyncHandler();
58965896
}
58975897

5898+
/// Returns true if the function is an @asyncHandler.
5899+
bool isAsyncHandler() const;
5900+
58985901
/// Returns true if the function body throws.
58995902
bool hasThrows() const { return Bits.AbstractFunctionDecl.Throws; }
59005903

include/swift/AST/TypeCheckRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,24 @@ class SelfAccessKindRequest :
781781
void cacheResult(SelfAccessKind value) const;
782782
};
783783

784+
/// Determine whether the given function is an @asyncHandler.
785+
class IsAsyncHandlerRequest :
786+
public SimpleRequest<IsAsyncHandlerRequest,
787+
bool(FuncDecl *),
788+
RequestFlags::Cached> {
789+
public:
790+
using SimpleRequest::SimpleRequest;
791+
792+
private:
793+
friend SimpleRequest;
794+
795+
bool evaluate(Evaluator &evaluator, FuncDecl *func) const;
796+
797+
public:
798+
// Caching
799+
bool isCached() const { return true; }
800+
};
801+
784802
/// Request whether the storage has a mutating getter.
785803
class IsGetterMutatingRequest :
786804
public SimpleRequest<IsGetterMutatingRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ SWIFT_REQUEST(TypeChecker, ExtendedTypeRequest, Type(ExtensionDecl *), Cached,
8181
NoLocationInfo)
8282
SWIFT_REQUEST(TypeChecker, FunctionBuilderTypeRequest, Type(ValueDecl *),
8383
Cached, NoLocationInfo)
84+
SWIFT_REQUEST(TypeChecker, IsAsyncHandlerRequest, bool(FuncDecl *),
85+
Cached, NoLocationInfo)
8486
SWIFT_REQUEST(TypeChecker, FunctionOperatorRequest, OperatorDecl *(FuncDecl *),
8587
Cached, NoLocationInfo)
8688
SWIFT_REQUEST(NameLookup, GenericSignatureRequest,

lib/AST/Decl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6691,6 +6691,17 @@ bool AbstractFunctionDecl::argumentNameIsAPIByDefault() const {
66916691
return false;
66926692
}
66936693

6694+
bool AbstractFunctionDecl::isAsyncHandler() const {
6695+
auto func = dyn_cast<FuncDecl>(this);
6696+
if (!func)
6697+
return false;
6698+
6699+
auto mutableFunc = const_cast<FuncDecl *>(func);
6700+
return evaluateOrDefault(getASTContext().evaluator,
6701+
IsAsyncHandlerRequest{mutableFunc},
6702+
false);
6703+
}
6704+
66946705
BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
66956706
if ((getBodyKind() == BodyKind::Synthesize ||
66966707
getBodyKind() == BodyKind::Unparsed) &&

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
330330
return;
331331
}
332332

333-
if (checkAsyncHandler(func, /*diagnose=*/true)) {
334-
attr->setInvalid();
335-
return;
336-
}
333+
// Trigger the request to check for @asyncHandler.
334+
(void)func->isAsyncHandler();
337335
}
338336
};
339337
} // end anonymous namespace
@@ -5236,3 +5234,19 @@ void swift::addAsyncNotes(FuncDecl *func) {
52365234
.fixItInsert(func->getAttributeInsertionLoc(false), "@asyncHandler ");
52375235
}
52385236
}
5237+
5238+
bool IsAsyncHandlerRequest::evaluate(
5239+
Evaluator &evaluator, FuncDecl *func) const {
5240+
// Check whether the attribute was explicitly specified.
5241+
if (auto attr = func->getAttrs().getAttribute<AsyncHandlerAttr>()) {
5242+
// Check for well-formedness.
5243+
if (checkAsyncHandler(func, /*diagnose=*/true)) {
5244+
attr->setInvalid();
5245+
return false;
5246+
}
5247+
5248+
return true;
5249+
}
5250+
5251+
return false;
5252+
}

0 commit comments

Comments
 (0)