Skip to content

Commit 5ee4928

Browse files
author
David Ungar
authored
Merge pull request swiftlang#28075 from davidungar/rdar-54906120-health-bug
[NameLookup, ASTScope] Eagerly expand function bodies before type-checking them
2 parents 3a4c4d0 + 785117d commit 5ee4928

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

include/swift/AST/ASTScope.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,12 @@ class ASTScopeImpl {
431431

432432
protected:
433433
/// Not const because may reexpand some scopes.
434-
const ASTScopeImpl *findInnermostEnclosingScope(SourceLoc,
435-
NullablePtr<raw_ostream>);
436-
const ASTScopeImpl *findInnermostEnclosingScopeImpl(SourceLoc,
437-
NullablePtr<raw_ostream>,
438-
SourceManager &,
439-
ScopeCreator &);
434+
ASTScopeImpl *findInnermostEnclosingScope(SourceLoc,
435+
NullablePtr<raw_ostream>);
436+
ASTScopeImpl *findInnermostEnclosingScopeImpl(SourceLoc,
437+
NullablePtr<raw_ostream>,
438+
SourceManager &,
439+
ScopeCreator &);
440440

441441
private:
442442
NullablePtr<ASTScopeImpl> findChildContaining(SourceLoc loc,
@@ -567,6 +567,8 @@ class ASTSourceFileScope final : public ASTScopeImpl {
567567
void
568568
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals();
569569

570+
void expandFunctionBody(AbstractFunctionDecl *AFD);
571+
570572
const SourceFile *getSourceFile() const override;
571573
NullablePtr<const void> addressForPrinting() const override { return SF; }
572574
bool crossCheckWithAST();

include/swift/AST/NameLookup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ class ASTScope {
596596
void
597597
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals();
598598

599+
static void expandFunctionBody(AbstractFunctionDecl *);
600+
599601
/// Flesh out the tree for dumping
600602
void buildFullyExpandedTree();
601603

@@ -631,6 +633,8 @@ class ASTScope {
631633

632634
private:
633635
static ast_scope::ASTSourceFileScope *createScopeTree(SourceFile *);
636+
637+
void expandFunctionBodyImpl(AbstractFunctionDecl *);
634638
};
635639

636640
} // end namespace swift

lib/AST/ASTScopeCreation.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,16 @@ bool ASTScope::areInactiveIfConfigClausesSupported() {
735735
return ScopeCreator::includeInactiveIfConfigClauses;
736736
}
737737

738+
void ASTScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
739+
auto *const SF = AFD->getParentSourceFile();
740+
if (SF->isSuitableForASTScopes())
741+
SF->getScope().expandFunctionBodyImpl(AFD);
742+
}
743+
744+
void ASTScope::expandFunctionBodyImpl(AbstractFunctionDecl *AFD) {
745+
impl->expandFunctionBody(AFD);
746+
}
747+
738748
ASTSourceFileScope *ASTScope::createScopeTree(SourceFile *SF) {
739749
ScopeCreator *scopeCreator = new (SF->getASTContext()) ScopeCreator(SF);
740750
return scopeCreator->sourceFileScope;
@@ -752,6 +762,15 @@ void ASTSourceFileScope::
752762
expandAndBeCurrentDetectingRecursion(*scopeCreator);
753763
}
754764

765+
void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
766+
if (!AFD)
767+
return;
768+
auto sr = AFD->getBodySourceRange();
769+
if (sr.isInvalid())
770+
return;
771+
ASTScopeImpl *bodyScope = findInnermostEnclosingScope(sr.Start, nullptr);
772+
bodyScope->expandAndBeCurrentDetectingRecursion(*scopeCreator);
773+
}
755774

756775
ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
757776
ScopeCreator *scopeCreator)
@@ -1059,6 +1078,8 @@ void ASTScopeImpl::disownDescendants(ScopeCreator &scopeCreator) {
10591078

10601079
ASTScopeImpl *
10611080
ASTScopeImpl::expandAndBeCurrentDetectingRecursion(ScopeCreator &scopeCreator) {
1081+
assert(scopeCreator.getASTContext().LangOpts.EnableASTScopeLookup &&
1082+
"Should not be getting here if ASTScopes are disabled");
10621083
return evaluateOrDefault(scopeCreator.getASTContext().evaluator,
10631084
ExpandASTScopeRequest{this, &scopeCreator}, nullptr);
10641085
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup(
6363
if (name.isOperator())
6464
return fileScope; // operators always at file scope
6565

66-
const auto innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
66+
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
6767
ASTScopeAssert(innermost->getWasExpanded(),
6868
"If looking in a scope, it must have been expanded.");
6969

@@ -117,14 +117,14 @@ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup(
117117
return startingScope;
118118
}
119119

120-
const ASTScopeImpl *
120+
ASTScopeImpl *
121121
ASTScopeImpl::findInnermostEnclosingScope(SourceLoc loc,
122122
NullablePtr<raw_ostream> os) {
123123
return findInnermostEnclosingScopeImpl(loc, os, getSourceManager(),
124124
getScopeCreator());
125125
}
126126

127-
const ASTScopeImpl *ASTScopeImpl::findInnermostEnclosingScopeImpl(
127+
ASTScopeImpl *ASTScopeImpl::findInnermostEnclosingScopeImpl(
128128
SourceLoc loc, NullablePtr<raw_ostream> os, SourceManager &sourceMgr,
129129
ScopeCreator &scopeCreator) {
130130
expandAndBeCurrentDetectingRecursion(scopeCreator);

lib/AST/UnqualifiedLookup.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace {
232232
bool useASTScopesForLookup() const;
233233

234234
/// For testing, assume this lookup is enabled:
235-
bool useASTScopesForLookupIfEnabled() const;
235+
bool wouldUseASTScopesForLookupIfItWereEnabled() const;
236236

237237
void lookUpTopLevelNamesInModuleScopeContext(DeclContext *);
238238

@@ -506,7 +506,8 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
506506
lookupNamesIntroducedBy(contextAndIsCascadingUse);
507507
}
508508

509-
if (crosscheckUnqualifiedLookup && useASTScopesForLookupIfEnabled()) {
509+
if (crosscheckUnqualifiedLookup &&
510+
wouldUseASTScopesForLookupIfItWereEnabled()) {
510511
ResultsVector results;
511512
size_t indexOfFirstOuterResult = 0;
512513
UnqualifiedLookupFactory altLookup(Name, DC, Loc, options, results,
@@ -550,10 +551,12 @@ void UnqualifiedLookupFactory::lookUpTopLevelNamesInModuleScopeContext(
550551
}
551552

552553
bool UnqualifiedLookupFactory::useASTScopesForLookup() const {
553-
return Ctx.LangOpts.EnableASTScopeLookup && useASTScopesForLookupIfEnabled();
554+
return Ctx.LangOpts.EnableASTScopeLookup &&
555+
wouldUseASTScopesForLookupIfItWereEnabled();
554556
}
555557

556-
bool UnqualifiedLookupFactory::useASTScopesForLookupIfEnabled() const {
558+
bool UnqualifiedLookupFactory::wouldUseASTScopesForLookupIfItWereEnabled()
559+
const {
557560
if (!Loc.isValid())
558561
return false;
559562
const auto *const SF = DC->getParentSourceFile();

lib/Sema/TypeCheckStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,12 @@ TypeCheckFunctionBodyUntilRequest::evaluate(Evaluator &evaluator,
21422142
body->getRBraceLoc(), body->isImplicit());
21432143
}
21442144

2145+
// Typechecking, in particular ApplySolution is going to replace closures
2146+
// with OpaqueValueExprs and then try to do lookups into the closures.
2147+
// So, build out the body now.
2148+
if (ctx.LangOpts.EnableASTScopeLookup)
2149+
ASTScope::expandFunctionBody(AFD);
2150+
21452151
StmtChecker SC(tc, AFD);
21462152
SC.EndTypeCheckLoc = endTypeCheckLoc;
21472153
bool hadError = SC.typeCheckBody(body);

0 commit comments

Comments
 (0)