Skip to content

Commit 5c0c673

Browse files
authored
Merge pull request swiftlang#33515 from slavapestov/clean-up-name-lookup-flags
Clean up for TypeCheckNameLookup.cpp
2 parents 7145615 + b26abf7 commit 5c0c673

11 files changed

+189
-175
lines changed

lib/AST/NameLookup.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@ static void recordShadowedDeclsAfterTypeMatch(
312312
}
313313
}
314314

315+
// If one declaration is in a protocol or extension thereof and the
316+
// other is not, prefer the one that is not.
317+
if ((bool)firstDecl->getDeclContext()->getSelfProtocolDecl() !=
318+
(bool)secondDecl->getDeclContext()->getSelfProtocolDecl()) {
319+
if (firstDecl->getDeclContext()->getSelfProtocolDecl()) {
320+
shadowed.insert(firstDecl);
321+
break;
322+
} else {
323+
shadowed.insert(secondDecl);
324+
continue;
325+
}
326+
}
327+
315328
continue;
316329
}
317330

lib/Sema/CodeSynthesis.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,8 @@ static bool canInheritDesignatedInits(Evaluator &eval, ClassDecl *decl) {
911911

912912
static void collectNonOveriddenSuperclassInits(
913913
ClassDecl *subclass, SmallVectorImpl<ConstructorDecl *> &results) {
914-
auto superclassTy = subclass->getSuperclass();
915-
assert(superclassTy);
914+
auto *superclassDecl = subclass->getSuperclassDecl();
915+
assert(superclassDecl);
916916

917917
// Record all of the initializers the subclass has overriden, excluding stub
918918
// overrides, which we don't want to consider as viable delegates for
@@ -924,11 +924,17 @@ static void collectNonOveriddenSuperclassInits(
924924
if (auto overridden = ctor->getOverriddenDecl())
925925
overriddenInits.insert(overridden);
926926

927-
auto superclassCtors = TypeChecker::lookupConstructors(
928-
subclass, superclassTy, NameLookupFlags::IgnoreAccessControl);
927+
superclassDecl->synthesizeSemanticMembersIfNeeded(
928+
DeclBaseName::createConstructor());
929929

930-
for (auto memberResult : superclassCtors) {
931-
auto superclassCtor = cast<ConstructorDecl>(memberResult.getValueDecl());
930+
NLOptions subOptions = (NL_QualifiedDefault | NL_IgnoreAccessControl);
931+
SmallVector<ValueDecl *, 4> lookupResults;
932+
subclass->lookupQualified(
933+
superclassDecl, DeclNameRef::createConstructor(),
934+
subOptions, lookupResults);
935+
936+
for (auto decl : lookupResults) {
937+
auto superclassCtor = cast<ConstructorDecl>(decl);
932938

933939
// Skip invalid superclass initializers.
934940
if (superclassCtor->isInvalid())
@@ -958,12 +964,7 @@ static void addImplicitInheritedConstructorsToClass(ClassDecl *decl) {
958964
decl->setAddedImplicitInitializers();
959965

960966
// We can only inherit initializers if we have a superclass.
961-
// FIXME: We should be bailing out earlier in the function, but unfortunately
962-
// that currently regresses associated type inference for cases like
963-
// compiler_crashers_2_fixed/0124-sr5825.swift due to the fact that we no
964-
// longer eagerly compute the interface types of the other constructors.
965-
auto superclassTy = decl->getSuperclass();
966-
if (!superclassTy)
967+
if (!decl->getSuperclassDecl() || !decl->getSuperclass())
967968
return;
968969

969970
// Check whether the user has defined a designated initializer for this class,

lib/Sema/MiscDiagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
594594
isExistential = instanceTy->isExistentialType();
595595
if (!isExistential &&
596596
instanceTy->mayHaveMembers() &&
597-
!TypeChecker::lookupConstructors(const_cast<DeclContext *>(DC),
598-
instanceTy).empty()) {
597+
!TypeChecker::lookupMember(const_cast<DeclContext *>(DC), instanceTy,
598+
DeclNameRef::createConstructor()).empty()) {
599599
Ctx.Diags.diagnose(E->getEndLoc(), diag::add_parens_to_type)
600600
.fixItInsertAfter(E->getEndLoc(), "()");
601601
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ static void diagnoseClassWithoutInitializers(ClassDecl *classDecl) {
11361136
{ C, DeclBaseName::createConstructor(), { C.Id_from } });
11371137
auto result =
11381138
TypeChecker::lookupMember(superclassDecl, superclassType, initFrom,
1139-
NameLookupFlags::ProtocolMembers |
1140-
NameLookupFlags::IgnoreAccessControl);
1139+
NameLookupFlags::IgnoreAccessControl);
11411140

11421141
if (!result.empty() && !result.front().getValueDecl()->isImplicit())
11431142
diagDest = result.front().getValueDecl();

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ namespace {
113113

114114
// If this isn't a protocol member to be given special
115115
// treatment, just add the result.
116-
if (!Options.contains(NameLookupFlags::ProtocolMembers) ||
117-
!isa<ProtocolDecl>(foundDC) ||
116+
if (!isa<ProtocolDecl>(foundDC) ||
118117
isa<GenericTypeParamDecl>(found) ||
119118
isa<TypeAliasDecl>(found) ||
120119
(isa<FuncDecl>(found) && cast<FuncDecl>(found)->isOperator())) {
@@ -124,9 +123,6 @@ namespace {
124123

125124
assert(isa<ProtocolDecl>(foundDC));
126125

127-
if (!Options.contains(NameLookupFlags::PerformConformanceCheck))
128-
return;
129-
130126
// If we found something within the protocol itself, and our
131127
// search began somewhere that is not in a protocol or extension
132128
// thereof, remap this declaration to the witness.
@@ -210,11 +206,9 @@ namespace {
210206

211207
static UnqualifiedLookupOptions
212208
convertToUnqualifiedLookupOptions(NameLookupOptions options) {
213-
UnqualifiedLookupOptions newOptions;
209+
UnqualifiedLookupOptions newOptions = UnqualifiedLookupFlags::AllowProtocolMembers;
214210
if (options.contains(NameLookupFlags::KnownPrivate))
215211
newOptions |= UnqualifiedLookupFlags::KnownPrivate;
216-
if (options.contains(NameLookupFlags::ProtocolMembers))
217-
newOptions |= UnqualifiedLookupFlags::AllowProtocolMembers;
218212
if (options.contains(NameLookupFlags::IgnoreAccessControl))
219213
newOptions |= UnqualifiedLookupFlags::IgnoreAccessControl;
220214
if (options.contains(NameLookupFlags::IncludeOuterResults))
@@ -278,8 +272,7 @@ TypeChecker::lookupUnqualifiedType(DeclContext *dc, DeclNameRef name,
278272

279273
auto lookup =
280274
evaluateOrDefault(ctx.evaluator, UnqualifiedLookupRequest{desc}, {});
281-
if (!lookup.allResults().empty() ||
282-
!options.contains(NameLookupFlags::ProtocolMembers))
275+
if (!lookup.allResults().empty())
283276
return lookup;
284277
}
285278

@@ -302,18 +295,12 @@ LookupResult TypeChecker::lookupMember(DeclContext *dc,
302295
assert(type->mayHaveMembers());
303296

304297
LookupResult result;
305-
NLOptions subOptions = NL_QualifiedDefault;
298+
NLOptions subOptions = (NL_QualifiedDefault | NL_ProtocolMembers);
306299
if (options.contains(NameLookupFlags::KnownPrivate))
307300
subOptions |= NL_KnownNonCascadingDependency;
308301
if (options.contains(NameLookupFlags::IgnoreAccessControl))
309302
subOptions |= NL_IgnoreAccessControl;
310303

311-
if (options.contains(NameLookupFlags::ProtocolMembers))
312-
subOptions |= NL_ProtocolMembers;
313-
314-
if (options.contains(NameLookupFlags::IncludeAttributeImplements))
315-
subOptions |= NL_IncludeAttributeImplements;
316-
317304
// We handle our own overriding/shadowing filtering.
318305
subOptions &= ~NL_RemoveOverridden;
319306
subOptions &= ~NL_RemoveNonVisible;
@@ -387,12 +374,10 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
387374

388375
// Look for members with the given name.
389376
SmallVector<ValueDecl *, 4> decls;
390-
NLOptions subOptions = NL_QualifiedDefault | NL_OnlyTypes;
377+
NLOptions subOptions = (NL_QualifiedDefault | NL_OnlyTypes | NL_ProtocolMembers);
391378

392379
if (options.contains(NameLookupFlags::KnownPrivate))
393380
subOptions |= NL_KnownNonCascadingDependency;
394-
if (options.contains(NameLookupFlags::ProtocolMembers))
395-
subOptions |= NL_ProtocolMembers;
396381
if (options.contains(NameLookupFlags::IgnoreAccessControl))
397382
subOptions |= NL_IgnoreAccessControl;
398383

@@ -434,21 +419,7 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
434419
if (auto assocType = dyn_cast<AssociatedTypeDecl>(typeDecl)) {
435420
if (!type->is<ArchetypeType>() &&
436421
!type->isTypeParameter()) {
437-
if (options.contains(NameLookupFlags::PerformConformanceCheck))
438-
inferredAssociatedTypes.push_back(assocType);
439-
continue;
440-
}
441-
}
442-
443-
// FIXME: This is a hack, we should be able to remove this entire 'if'
444-
// statement once we learn how to deal with the circularity here.
445-
if (auto *aliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
446-
if (isa<ProtocolDecl>(aliasDecl->getDeclContext()) &&
447-
!type->is<ArchetypeType>() &&
448-
!type->isTypeParameter() &&
449-
aliasDecl->getUnderlyingType()->getCanonicalType()
450-
->hasTypeParameter() &&
451-
!options.contains(NameLookupFlags::PerformConformanceCheck)) {
422+
inferredAssociatedTypes.push_back(assocType);
452423
continue;
453424
}
454425
}
@@ -511,11 +482,6 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
511482
return result;
512483
}
513484

514-
LookupResult TypeChecker::lookupConstructors(DeclContext *dc, Type type,
515-
NameLookupOptions options) {
516-
return lookupMember(dc, type, DeclNameRef::createConstructor(), options);
517-
}
518-
519485
unsigned TypeChecker::getCallEditDistance(DeclNameRef writtenName,
520486
DeclName correctedName,
521487
unsigned maxEditDistance) {

0 commit comments

Comments
 (0)