Skip to content

Commit 8e7943d

Browse files
committed
Make TopLevelCode async context
This patch makes the FileUnit and TopLevelCodeDecl contexts asynchronous contexts when the `-enable-experimental-async-top-level` flag is passed.
1 parent 130f5ca commit 8e7943d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,9 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
20152015
SourceLoc getStartLoc() const;
20162016
SourceRange getSourceRange() const;
20172017

2018+
LLVM_READONLY
2019+
ASTContext &getASTContext() const { return DeclContext::getASTContext(); }
2020+
20182021
static bool classof(const Decl *D) {
20192022
return D->getKind() == DeclKind::TopLevelCode;
20202023
}

lib/AST/DeclContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,14 +1211,15 @@ bool DeclContext::isClassConstrainedProtocolExtension() const {
12111211
bool DeclContext::isAsyncContext() const {
12121212
switch (getContextKind()) {
12131213
case DeclContextKind::Initializer:
1214-
case DeclContextKind::TopLevelCodeDecl:
12151214
case DeclContextKind::EnumElementDecl:
12161215
case DeclContextKind::ExtensionDecl:
12171216
case DeclContextKind::SerializedLocal:
12181217
case DeclContextKind::Module:
1219-
case DeclContextKind::FileUnit:
12201218
case DeclContextKind::GenericTypeDecl:
12211219
return false;
1220+
case DeclContextKind::FileUnit:
1221+
case DeclContextKind::TopLevelCodeDecl:
1222+
return getASTContext().LangOpts.EnableExperimentalAsyncTopLevel;
12221223
case DeclContextKind::AbstractClosureExpr:
12231224
return cast<AbstractClosureExpr>(this)->isBodyAsync();
12241225
case DeclContextKind::AbstractFunctionDecl: {

lib/Sema/TypeCheckEffects.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,8 +1491,10 @@ class Context {
14911491

14921492
static Context forTopLevelCode(TopLevelCodeDecl *D) {
14931493
// Top-level code implicitly handles errors.
1494-
// TODO: Eventually, it will handle async as well.
1495-
return Context(/*handlesErrors=*/true, /*handlesAsync=*/false, None);
1494+
return Context(/*handlesErrors=*/true,
1495+
/*handlesAsync=*/
1496+
D->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel,
1497+
None);
14961498
}
14971499

14981500
static Context forFunction(AbstractFunctionDecl *D) {

0 commit comments

Comments
 (0)