Skip to content

Commit a8b3ef4

Browse files
committed
[NFC] Merge extension member lookup code paths
There are two pieces of code that can add an extension’s members to a type’s lookup table: `NominalTypeDecl::prepareLookupTable()`, which initializes the lookup table before its first use, and `NominalTypeDecl::addedExtension()`, which only updates a lookup table that has already been initialized. The two functions ought to do the same things, but it’s difficult to predict which one will be exercised by a given test, and in practice our current unit tests only seem to use `NominalTypeDecl::prepareLookupTable()` in certain important situations. Factor the common code into a shared helper method, `MemberLookupTable::addExtension()`, to increase our confidence that both code paths will work correctly even if only one is hit by our unit tests. Fixes rdar://121479725.
1 parent b98d7a5 commit a8b3ef4

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

lib/AST/NameLookup.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,10 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12771277
/// Add the given members to the lookup table.
12781278
void addMembers(DeclRange members);
12791279

1280+
/// Add the members of the extension to the lookup table, if necessary
1281+
/// registering it for future lazy member loading.
1282+
void addExtension(ExtensionDecl *ext);
1283+
12801284
void addExtensionWithLazyMembers(ExtensionDecl *ext) {
12811285
ExtensionsWithLazyMembers.push_back(ext);
12821286
}
@@ -1467,23 +1471,34 @@ void MemberLookupTable::addMembers(DeclRange members) {
14671471
}
14681472
}
14691473

1474+
void MemberLookupTable::addExtension(ExtensionDecl *ext) {
1475+
// If we can lazy-load this extension, only take the members we've loaded
1476+
// so far.
1477+
//
1478+
// FIXME: This should be 'e->hasLazyMembers()' but that crashes` because
1479+
// some imported extensions don't have a Clang node, and only support
1480+
// LazyMemberLoader::loadAllMembers() and not
1481+
// LazyMemberLoader::loadNamedMembers().
1482+
if (ext->wasDeserialized() || ext->hasClangNode()) {
1483+
addMembers(ext->getCurrentMembersWithoutLoading());
1484+
clearLazilyCompleteCache();
1485+
clearLazilyCompleteForMacroExpansionCache();
1486+
addExtensionWithLazyMembers(ext);
1487+
} else {
1488+
// Else, load all the members into the table.
1489+
addMembers(ext->getMembers());
1490+
}
1491+
addContainerWithMacroExpansions(ext);
1492+
}
1493+
14701494
void NominalTypeDecl::addedExtension(ExtensionDecl *ext) {
14711495
if (!LookupTable.getInt())
14721496
return;
14731497

14741498
auto *table = LookupTable.getPointer();
14751499
assert(table);
14761500

1477-
if (ext->wasDeserialized() || ext->hasClangNode()) {
1478-
table->addMembers(ext->getCurrentMembersWithoutLoading());
1479-
table->clearLazilyCompleteCache();
1480-
table->clearLazilyCompleteForMacroExpansionCache();
1481-
table->addExtensionWithLazyMembers(ext);
1482-
} else {
1483-
table->addMembers(ext->getMembers());
1484-
}
1485-
1486-
table->addContainerWithMacroExpansions(ext);
1501+
table->addExtension(ext);
14871502
}
14881503

14891504
void NominalTypeDecl::addedMember(Decl *member) {
@@ -1952,21 +1967,7 @@ void NominalTypeDecl::prepareLookupTable() {
19521967

19531968
// Note: this calls prepareExtensions()
19541969
for (auto e : getExtensions()) {
1955-
// If we can lazy-load this extension, only take the members we've loaded
1956-
// so far.
1957-
//
1958-
// FIXME: This should be 'e->hasLazyMembers()' but that crashes` because
1959-
// some imported extensions don't have a Clang node, and only support
1960-
// LazyMemberLoader::loadAllMembers() and not
1961-
// LazyMemberLoader::loadNamedMembers().
1962-
if (e->wasDeserialized() || e->hasClangNode()) {
1963-
table->addMembers(e->getCurrentMembersWithoutLoading());
1964-
table->addExtensionWithLazyMembers(e);
1965-
continue;
1966-
}
1967-
1968-
// Else, load all the members into the table.
1969-
table->addMembers(e->getMembers());
1970+
table->addExtension(e);
19701971
}
19711972

19721973
// Any extensions added after this point will add their members to the

0 commit comments

Comments
 (0)