Skip to content

Commit e67921f

Browse files
author
David Ungar
authored
Merge pull request #27786 from davidungar/fix-lazy-function-bodies-rdar-56384593
[NameLookup, ASTScope] Create lazily-parsed function body scopes.
2 parents b854e99 + 8469abb commit e67921f

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,8 +1359,8 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
13591359
}
13601360
// Create scope for the body.
13611361
// We create body scopes when there is no body for source kit to complete
1362-
// erroneous code in bodies. But don't let compiler synthesize one.
1363-
if (decl->getBodySourceRange().isValid() && decl->getBody(false)) {
1362+
// erroneous code in bodies.
1363+
if (decl->getBodySourceRange().isValid()) {
13641364
if (AbstractFunctionBodyScope::isAMethod(decl))
13651365
scopeCreator.constructExpandAndInsertUncheckable<MethodBodyScope>(leaf,
13661366
decl);
@@ -1901,6 +1901,7 @@ void AbstractFunctionBodyScope::beCurrent() {
19011901
bodyWhenLastExpanded = decl->getBody(false);
19021902
}
19031903
bool AbstractFunctionBodyScope::isCurrentIfWasExpanded() const {
1904+
// Pass in false to keep the compiler from synthesizing one.
19041905
return bodyWhenLastExpanded == decl->getBody(false);
19051906
}
19061907

lib/AST/UnqualifiedLookup.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,10 @@ namespace {
373373
void print(raw_ostream &OS) const;
374374

375375
void dumpResults() const;
376-
377-
bool verifyEqualTo(const UnqualifiedLookupFactory &&, const char *thisLabel,
378-
const char *otherLabel) const;
379-
376+
377+
bool verifyEqualTo(const UnqualifiedLookupFactory &&, StringRef thisLabel,
378+
StringRef otherLabel) const;
379+
380380
/// Legacy lookup is wrong here; we should NOT find this symbol.
381381
bool shouldDiffer() const;
382382
StringRef getSourceFileName() const;
@@ -515,8 +515,13 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
515515
else
516516
altLookup.lookupNamesIntroducedBy(contextAndIsCascadingUse);
517517

518-
assert(
519-
verifyEqualTo(std::move(altLookup), "main lookup", "alternate lookup"));
518+
const auto *ASTScopeLabel = "ASTScope lookup";
519+
const auto *contextLabel = "context-bsed lookup";
520+
const auto *mainLabel =
521+
useASTScopesForLookup() ? ASTScopeLabel : contextLabel;
522+
const auto *alternateLabel =
523+
useASTScopesForLookup() ? contextLabel : ASTScopeLabel;
524+
assert(verifyEqualTo(std::move(altLookup), mainLabel, alternateLabel));
520525
}
521526
}
522527

@@ -1285,16 +1290,16 @@ StringRef UnqualifiedLookupFactory::getSourceFileName() const {
12851290
return DC->getParentSourceFile()->getFilename();
12861291
}
12871292

1288-
static void writeFirstLine(const UnqualifiedLookupFactory &ul, StringRef s) {
1293+
static void writeFirstLine(const UnqualifiedLookupFactory &ul, llvm::Twine s) {
12891294
std::string line =
12901295
std::string("In file: ") + ul.getSourceFileName().str() + ", " + s.str();
12911296
writeLine(line);
12921297
}
12931298

12941299
static void writeInconsistent(const UnqualifiedLookupFactory &me,
1295-
const char *thisLabel,
1300+
StringRef thisLabel,
12961301
const UnqualifiedLookupFactory &other,
1297-
const char *otherLabel, StringRef s) {
1302+
StringRef otherLabel, llvm::Twine s) {
12981303
writeFirstLine(me, s);
12991304
other.dump();
13001305
llvm::errs() << "\n" << thisLabel << " Results:\n";
@@ -1307,23 +1312,18 @@ static void writeInconsistent(const UnqualifiedLookupFactory &me,
13071312
#pragma mark comparing results
13081313

13091314
bool UnqualifiedLookupFactory::verifyEqualTo(
1310-
const UnqualifiedLookupFactory &&other, const char *thisLabel,
1311-
const char *otherLabel) const {
1315+
const UnqualifiedLookupFactory &&other, const StringRef thisLabel,
1316+
StringRef otherLabel) const {
13121317
if (shouldDiffer()) {
13131318
return true;
13141319
}
1315-
auto writeErr = [&](StringRef s) {
1320+
auto writeErr = [&](llvm::Twine s) {
13161321
writeInconsistent(*this, thisLabel, other, otherLabel, s);
13171322
};
13181323
if (Results.size() != other.Results.size()) {
1319-
const bool tooMany = Results.size() < other.Results.size();
1320-
writeErr(std::string(tooMany ? "Found too many: " : "Found too few: ") +
1321-
std::to_string(Results.size()) + " vs " +
1322-
std::to_string(other.Results.size()));
1323-
if (tooMany)
1324-
assert(false && "ASTScopeImpl found too many");
1325-
else
1326-
assert(false && "ASTScopeImpl found too few");
1324+
writeErr(thisLabel + " found " + std::to_string(Results.size()) + " but " +
1325+
otherLabel + " found " + std::to_string(other.Results.size()));
1326+
assert(false && "mismatch in number of results");
13271327
}
13281328
for (size_t i : indices(Results)) {
13291329
const auto &e = Results[i];
@@ -1338,7 +1338,7 @@ bool UnqualifiedLookupFactory::verifyEqualTo(
13381338
llvm::errs() << "ValueDecls differ but print same\n";
13391339
else {
13401340
writeErr(std::string( "ValueDecls differ at ") + std::to_string(i));
1341-
assert(false && "ASTScopeImpl found different Decl");
1341+
assert(false && "other lookup found different Decl");
13421342
}
13431343
}
13441344
if (e.getDeclContext() != oe.getDeclContext()) {
@@ -1353,7 +1353,7 @@ bool UnqualifiedLookupFactory::verifyEqualTo(
13531353
+ std::to_string(IndexOfFirstOuterResult)
13541354
+ std::string( ", is: ")
13551355
+ std::to_string(other.IndexOfFirstOuterResult));
1356-
assert(false && "ASTScopeImpl IndexOfFirstOuterResult differs");
1356+
assert(false && "other lookup IndexOfFirstOuterResult differs");
13571357
}
13581358
if (recordedSF != other.recordedSF) {
13591359
writeErr(std::string("recordedSF differs: shouldBe: ") +
@@ -1362,13 +1362,13 @@ bool UnqualifiedLookupFactory::verifyEqualTo(
13621362
std::string(" is: ") +
13631363
(other.recordedSF ? other.recordedSF->getFilename().str()
13641364
: std::string("<no name>")));
1365-
assert(false && "ASTScopeImpl recordedSF differs");
1365+
assert(false && "other lookup recordedSF differs");
13661366
}
13671367
if (recordedSF && recordedIsCascadingUse != other.recordedIsCascadingUse) {
13681368
writeErr(std::string("recordedIsCascadingUse differs: shouldBe: ") +
13691369
std::to_string(recordedIsCascadingUse) + std::string(" is: ") +
13701370
std::to_string(other.recordedIsCascadingUse));
1371-
assert(false && "ASTScopeImpl recordedIsCascadingUse differs");
1371+
assert(false && "other lookup recordedIsCascadingUse differs");
13721372
}
13731373
return true;
13741374
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Body of closure in parameter to call of closureTaker is created lazily
2+
// and this test ensures that that body scope does get expanded
3+
var v = closureTaker {
4+
func amIFound() {}
5+
}
6+
7+
func closureTaker(_: () -> Void )
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Ensure that a lazily-parsed function body gets expanded
2+
3+
// RUN: %target-swift-frontend -typecheck -primary-file %s %S/Inputs/lazy_function_body_expansion_helper.swift
4+
5+
let a = v

0 commit comments

Comments
 (0)