Skip to content

Commit b09c995

Browse files
committed
Reintroduce NameLookupFlags::IgnoreNewExtensions
Soft revert a09382c. It should now be safe to add this flag back as an optimization to specifically disable lazy member loading instead of all extension loading. Push the flag back everywhere it was needed, but also push it into lookup for associated type members which will never appear in extensions.
1 parent 26d8bad commit b09c995

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,12 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
34013401
/// Whether to include @_implements members.
34023402
/// Used by conformance-checking to find special @_implements members.
34033403
IncludeAttrImplements = 1 << 0,
3404+
/// Whether to avoid loading lazy members from any new extensions that would otherwise be found
3405+
/// by deserialization.
3406+
///
3407+
/// Used by the module loader to break recursion and as an optimization e.g. when it is known that a
3408+
/// particular member declaration will never appear in an extension.
3409+
IgnoreNewExtensions = 1 << 1,
34043410
};
34053411

34063412
/// Find all of the declarations with the given name within this nominal type

include/swift/AST/NameLookupRequests.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,6 @@ class DirectLookupRequest
513513
// Evaluation.
514514
llvm::Expected<TinyPtrVector<ValueDecl *>>
515515
evaluate(Evaluator &evaluator, DirectLookupDescriptor desc) const;
516-
517-
public:
518-
// Cycle handling
519-
void diagnoseCycle(DiagnosticEngine &diags) const;
520516
};
521517

522518
#define SWIFT_TYPEID_ZONE NameLookup

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4648,7 +4648,8 @@ ValueDecl *ProtocolDecl::getSingleRequirement(DeclName name) const {
46484648
}
46494649

46504650
AssociatedTypeDecl *ProtocolDecl::getAssociatedType(Identifier name) const {
4651-
auto results = const_cast<ProtocolDecl *>(this)->lookupDirect(name);
4651+
const auto flags = NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
4652+
auto results = const_cast<ProtocolDecl *>(this)->lookupDirect(name, flags);
46524653
for (auto candidate : results) {
46534654
if (candidate->getDeclContext() == this &&
46544655
isa<AssociatedTypeDecl>(candidate)) {

lib/AST/NameLookup.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,8 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
12731273
ASTContext &ctx = decl->getASTContext();
12741274
const bool useNamedLazyMemberLoading = (ctx.LangOpts.NamedLazyMemberLoading &&
12751275
decl->hasLazyMembers());
1276-
1276+
const bool disableAdditionalExtensionLoading =
1277+
flags.contains(NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions);
12771278
const bool includeAttrImplements =
12781279
flags.contains(NominalTypeDecl::LookupDirectFlags::IncludeAttrImplements);
12791280

@@ -1301,11 +1302,15 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
13011302
includeAttrImplements);
13021303
};
13031304

1304-
auto updateLookupTable = [&decl](MemberLookupTable &table) {
1305+
auto updateLookupTable = [&decl](MemberLookupTable &table,
1306+
bool noExtensions) {
13051307
// Make sure we have the complete list of members (in this nominal and in
13061308
// all extensions).
13071309
(void)decl->getMembers();
13081310

1311+
if (noExtensions)
1312+
return;
1313+
13091314
for (auto E : decl->getExtensions())
13101315
(void)E->getMembers();
13111316

@@ -1314,16 +1319,16 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
13141319

13151320
auto &Table = *decl->LookupTable;
13161321
if (!useNamedLazyMemberLoading) {
1317-
updateLookupTable(Table);
1322+
updateLookupTable(Table, disableAdditionalExtensionLoading);
13181323
} else if (!Table.isLazilyComplete(name.getBaseName())) {
13191324
// The lookup table believes it doesn't have a complete accounting of this
13201325
// name - either because we're never seen it before, or another extension
13211326
// was registered since the last time we searched. Ask the loaders to give
13221327
// us a hand.
13231328
DeclBaseName baseName(name.getBaseName());
13241329
if (populateLookupTableEntryFromLazyIDCLoader(ctx, Table, baseName, decl)) {
1325-
updateLookupTable(Table);
1326-
} else {
1330+
updateLookupTable(Table, disableAdditionalExtensionLoading);
1331+
} else if (!disableAdditionalExtensionLoading) {
13271332
populateLookupTableEntryFromExtensions(ctx, Table, decl, baseName);
13281333
}
13291334
Table.markLazilyComplete(baseName);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,9 @@ static VarDecl *findAnonymousInnerFieldDecl(VarDecl *importedFieldDecl,
767767
auto anonymousFieldTypeDecl
768768
= anonymousFieldType->getStructOrBoundGenericStruct();
769769

770+
const auto flags = NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
770771
for (auto decl : anonymousFieldTypeDecl->lookupDirect(
771-
importedFieldDecl->getName())) {
772+
importedFieldDecl->getName(), flags)) {
772773
if (isa<VarDecl>(decl)) {
773774
return cast<VarDecl>(decl);
774775
}
@@ -8303,7 +8304,8 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
83038304
DeclName initName = DeclName(ctx, DeclBaseName::createConstructor(),
83048305
{ ctx.Id_rawValue });
83058306
auto nominal = type->getAnyNominal();
8306-
for (auto found : nominal->lookupDirect(initName)) {
8307+
const auto flags = NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
8308+
for (auto found : nominal->lookupDirect(initName, flags)) {
83078309
init = dyn_cast<ConstructorDecl>(found);
83088310
if (init && init->getDeclContext() == nominal)
83098311
break;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5143,8 +5143,10 @@ void TypeChecker::checkConformancesInContext(DeclContext *dc,
51435143
continue;
51445144

51455145
bool valueIsType = isa<TypeDecl>(value);
5146+
const auto flags =
5147+
NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
51465148
for (auto requirement
5147-
: diag.Protocol->lookupDirect(value->getFullName())) {
5149+
: diag.Protocol->lookupDirect(value->getFullName(), flags)) {
51485150
if (requirement->getDeclContext() != diag.Protocol)
51495151
continue;
51505152

@@ -5345,7 +5347,8 @@ swift::findWitnessedObjCRequirements(const ValueDecl *witness,
53455347
if (!proto->isObjC()) continue;
53465348

53475349
Optional<ProtocolConformance *> conformance;
5348-
for (auto req : proto->lookupDirect(name)) {
5350+
const auto flags = NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
5351+
for (auto req : proto->lookupDirect(name, flags)) {
53495352
// Skip anything in a protocol extension.
53505353
if (req->getDeclContext() != proto) continue;
53515354

0 commit comments

Comments
 (0)