Skip to content

Commit 831cfb5

Browse files
author
Zak Kent
committed
[Sema] [NFC] Don't accumulate lazily typechecked functions in lazy immediate mode
1 parent c0a16d0 commit 831cfb5

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

include/swift/AST/SourceFile.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ class SourceFile final : public FileUnit {
170170
/// located in the source file.
171171
llvm::SetVector<ValueDecl *> DeclsWithRuntimeDiscoverableAttrs;
172172

173+
/// The list of functions defined in this file whose bodies have yet to be
174+
/// typechecked. They must be held in this list instead of eagerly validated
175+
/// because their bodies may force us to perform semantic checks of arbitrary
176+
/// complexity, and we currently cannot handle those checks in isolation. E.g.
177+
/// we cannot, in general, perform witness matching on singular requirements
178+
/// unless the entire conformance has been evaluated.
179+
std::vector<AbstractFunctionDecl *> DelayedFunctions;
180+
173181
/// The list of top-level items in the source file. This is \c None if
174182
/// they have not yet been parsed.
175183
/// FIXME: Once addTopLevelDecl/prependTopLevelDecl
@@ -271,13 +279,11 @@ class SourceFile final : public FileUnit {
271279
/// The list of local type declarations in the source file.
272280
llvm::SetVector<TypeDecl *> LocalTypeDecls;
273281

274-
/// The list of functions defined in this file whose bodies have yet to be
275-
/// typechecked. They must be held in this list instead of eagerly validated
276-
/// because their bodies may force us to perform semantic checks of arbitrary
277-
/// complexity, and we currently cannot handle those checks in isolation. E.g.
278-
/// we cannot, in general, perform witness matching on singular requirements
279-
/// unless the entire conformance has been evaluated.
280-
std::vector<AbstractFunctionDecl *> DelayedFunctions;
282+
/// Defer type checking of `AFD` to the end of `Sema`
283+
void addDelayedFunction(AbstractFunctionDecl *AFD);
284+
285+
/// Typecheck the bodies of all lazily checked functions
286+
void typeCheckDelayedFunctions();
281287

282288
/// A mapping from Objective-C selectors to the methods that have
283289
/// those selectors.

lib/AST/Module.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3802,6 +3802,27 @@ void SourceFile::prependTopLevelDecl(Decl *d) {
38023802
ctx.evaluator.clearCachedOutput(ParseTopLevelDeclsRequest{mutableThis});
38033803
}
38043804

3805+
void SourceFile::addDelayedFunction(AbstractFunctionDecl *AFD) {
3806+
// If we defer type checking to runtime, we won't
3807+
// have to type check `AFD` ahead of time
3808+
auto &Ctx = getASTContext();
3809+
if (Ctx.TypeCheckerOpts.DeferToRuntime &&
3810+
Ctx.LangOpts.hasFeature(Feature::LazyImmediate))
3811+
return;
3812+
DelayedFunctions.push_back(AFD);
3813+
}
3814+
3815+
void SourceFile::typeCheckDelayedFunctions() {
3816+
3817+
for (unsigned i = 0; i < DelayedFunctions.size(); i++) {
3818+
auto *AFD = DelayedFunctions[i];
3819+
assert(!AFD->getDeclContext()->isLocalContext());
3820+
AFD->getTypecheckedBody();
3821+
}
3822+
3823+
DelayedFunctions.clear();
3824+
}
3825+
38053826
ArrayRef<ASTNode> SourceFile::getTopLevelItems() const {
38063827
auto &ctx = getASTContext();
38073828
auto *mutableThis = const_cast<SourceFile *>(this);

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,12 +1844,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
18441844

18451845
ASTContext &getASTContext() const { return Ctx; }
18461846
void addDelayedFunction(AbstractFunctionDecl *AFD) {
1847-
if (!SF) return;
1847+
if (!SF)
1848+
return;
18481849

1849-
while (auto enclosingSF = SF->getEnclosingSourceFile())
1850-
SF = enclosingSF;
1850+
while (auto *ESF = SF->getEnclosingSourceFile())
1851+
SF = ESF;
18511852

1852-
SF->DelayedFunctions.push_back(AFD);
1853+
SF->addDelayedFunction(AFD);
18531854
}
18541855

18551856
void visit(Decl *decl) {

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
717717
continue;
718718

719719
if (auto thunk = var->getDistributedThunk())
720-
SF->DelayedFunctions.push_back(thunk);
720+
SF->addDelayedFunction(thunk);
721721

722722
continue;
723723
}
@@ -780,7 +780,7 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
780780
}
781781

782782
if (auto thunk = func->getDistributedThunk()) {
783-
SF->DelayedFunctions.push_back(thunk);
783+
SF->addDelayedFunction(thunk);
784784
}
785785
}
786786
}

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,6 @@ void swift::bindExtensions(ModuleDecl &mod) {
241241
// typeCheckDecl().
242242
}
243243

244-
static void typeCheckDelayedFunctions(SourceFile &SF) {
245-
unsigned currentFunctionIdx = 0;
246-
247-
while (currentFunctionIdx < SF.DelayedFunctions.size()) {
248-
auto *AFD = SF.DelayedFunctions[currentFunctionIdx];
249-
assert(!AFD->getDeclContext()->isLocalContext());
250-
(void) AFD->getTypecheckedBody();
251-
++currentFunctionIdx;
252-
}
253-
254-
SF.DelayedFunctions.clear();
255-
}
256-
257244
void swift::performTypeChecking(SourceFile &SF) {
258245
return (void)evaluateOrDefault(SF.getASTContext().evaluator,
259246
TypeCheckSourceFileRequest{&SF}, {});
@@ -306,10 +293,7 @@ TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
306293
}
307294
}
308295
}
309-
if (!Ctx.TypeCheckerOpts.DeferToRuntime ||
310-
!Ctx.LangOpts.hasFeature(Feature::LazyImmediate)) {
311-
typeCheckDelayedFunctions(*SF);
312-
}
296+
SF->typeCheckDelayedFunctions();
313297
}
314298

315299
diagnoseUnnecessaryPreconcurrencyImports(*SF);

0 commit comments

Comments
 (0)