Skip to content

Commit 3c0206d

Browse files
committed
LookupVisibleDecls: Remove RestateFilteringConsumer
It's no longer needed after the previous set of changes, and removing it fixes a crash. Fixes <rdar://problem/46853611>.
1 parent 6fd25fd commit 3c0206d

File tree

2 files changed

+23
-90
lines changed

2 files changed

+23
-90
lines changed

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -466,92 +466,6 @@ lookupVisibleMemberDeclsImpl(Type BaseTy, VisibleDeclConsumer &Consumer,
466466
GenericSignatureBuilder *GSB,
467467
VisitedSet &Visited);
468468

469-
// Filters out restated declarations from a protocol hierarchy
470-
// or equivalent requirements from protocol composition types.
471-
class RestateFilteringConsumer : public VisibleDeclConsumer {
472-
LazyResolver *resolver;
473-
474-
using FoundDecl = std::pair<ValueDecl*, DeclVisibilityKind>;
475-
using NameAndType = std::pair<DeclName, CanType>;
476-
477-
llvm::DenseMap<DeclName, FoundDecl> foundVars;
478-
llvm::DenseMap<NameAndType, FoundDecl> foundFuncs;
479-
llvm::MapVector<ValueDecl*, DeclVisibilityKind> declsToReport;
480-
481-
template <typename K>
482-
void addDecl(llvm::DenseMap<K, FoundDecl> &Map, K Key, FoundDecl FD) {
483-
// Add the declaration if we haven't found an equivalent yet, otherwise
484-
// replace the equivalent if the found decl has a higher access level.
485-
auto existingDecl = Map.find(Key);
486-
487-
if ((existingDecl == Map.end()) ||
488-
(Map[Key].first->getFormalAccess() < FD.first->getFormalAccess())) {
489-
if (existingDecl != Map.end())
490-
declsToReport.erase({existingDecl->getSecond().first});
491-
Map[Key] = FD;
492-
declsToReport.insert(FD);
493-
}
494-
}
495-
496-
CanType stripSelfRequirementsIfNeeded(ValueDecl *VD,
497-
GenericFunctionType *GFT) const {
498-
// Preserve the generic signature if this is a subscript, which are uncurried,
499-
// or if we have generic params other than Self. Otherwise, use
500-
// the resultType of the curried function type.
501-
// When we keep the generic signature, we remove the requirements
502-
// from Self to make sure they don't prevent us from recognizing restatements.
503-
auto params = GFT->getGenericParams();
504-
if (params.size() == 1 && !isa<SubscriptDecl>(VD)) {
505-
return GFT->getResult()->getCanonicalType();
506-
}
507-
auto Self = VD->getDeclContext()->getSelfInterfaceType();
508-
SmallVector<Requirement, 4> newReqs;
509-
for (auto req: GFT->getRequirements()) {
510-
if (!Self->isEqual(req.getFirstType()))
511-
newReqs.push_back(req);
512-
}
513-
auto newSig = GenericSignature::get(params, newReqs, false);
514-
515-
return GenericFunctionType::get(newSig, GFT->getParams(),
516-
GFT->getResult(), GFT->getExtInfo())
517-
->getCanonicalType();
518-
}
519-
520-
public:
521-
RestateFilteringConsumer(Type baseTy, const DeclContext *DC,
522-
LazyResolver *resolver)
523-
: resolver(resolver) {
524-
assert(DC && baseTy && !baseTy->hasLValueType());
525-
}
526-
527-
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override {
528-
assert(VD);
529-
// If this isn't a protocol context, don't look further into the decl.
530-
if (!isa<ProtocolDecl>(VD->getDeclContext())) {
531-
declsToReport.insert({VD, Reason});
532-
return;
533-
}
534-
if (resolver)
535-
resolver->resolveDeclSignature(VD);
536-
537-
if (!VD->hasInterfaceType()) {
538-
declsToReport.insert({VD, Reason});
539-
return;
540-
}
541-
if (auto GFT = VD->getInterfaceType()->getAs<GenericFunctionType>()) {
542-
auto type = stripSelfRequirementsIfNeeded(VD, GFT);
543-
addDecl(foundFuncs, {VD->getFullName(), type}, {VD, Reason});
544-
return;
545-
}
546-
addDecl(foundVars, VD->getFullName(), {VD, Reason});
547-
}
548-
549-
void feedResultsToConsumer(VisibleDeclConsumer &Consumer) const {
550-
for (const auto entry: declsToReport)
551-
Consumer.foundDecl(entry.first, entry.second);
552-
}
553-
};
554-
555469
static void
556470
lookupVisibleProtocolMemberDecls(Type BaseTy, ProtocolType *PT,
557471
VisibleDeclConsumer &Consumer,
@@ -911,13 +825,11 @@ static void lookupVisibleMemberDecls(
911825
LookupState LS, DeclVisibilityKind Reason, LazyResolver *TypeResolver,
912826
GenericSignatureBuilder *GSB) {
913827
OverrideFilteringConsumer overrideConsumer(BaseTy, CurrDC, TypeResolver);
914-
RestateFilteringConsumer restateConsumer(BaseTy, CurrDC, TypeResolver);
915828
VisitedSet Visited;
916-
lookupVisibleMemberDeclsImpl(BaseTy, restateConsumer, CurrDC, LS, Reason,
829+
lookupVisibleMemberDeclsImpl(BaseTy, overrideConsumer, CurrDC, LS, Reason,
917830
TypeResolver, GSB, Visited);
918831

919832
// Report the declarations we found to the real consumer.
920-
restateConsumer.feedResultsToConsumer(overrideConsumer);
921833
for (const auto &DeclAndReason : overrideConsumer.DeclsToReport)
922834
Consumer.foundDecl(DeclAndReason.D, DeclAndReason.Reason);
923835
}

test/Sema/typo_correction.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ struct Generic<T> {
113113
}
114114

115115
protocol P { // expected-note {{'P' previously declared here}}
116+
// expected-note@-1 {{did you mean 'P'?}}
116117
typealias a = Generic
117118
}
118119

119120
protocol P {} // expected-error {{invalid redeclaration of 'P'}}
121+
// expected-note@-1 {{did you mean 'P'?}}
120122

121123
func hasTypo() {
122124
_ = P.a.a // expected-error {{type 'P.a' (aka 'Generic') has no member 'a'}}
@@ -170,7 +172,7 @@ class CircularValidationWithTypo {
170172
// Crash with invalid extension that has not been bound -- https://bugs.swift.org/browse/SR-8984
171173
protocol PP {}
172174

173-
func boo() { // expected-note {{did you mean 'boo'?}}
175+
func boo() {
174176
extension PP { // expected-error {{declaration is only valid at file scope}}
175177
func g() {
176178
booo() // expected-error {{use of unresolved identifier 'booo'}}
@@ -197,3 +199,22 @@ func testFwdRef() {
197199
let _ = forward_refX + 1 // expected-error {{use of unresolved identifier 'forward_refX'}}
198200
let forward_ref1 = 4
199201
}
202+
203+
// Crash with protocol members.
204+
protocol P1 {
205+
associatedtype A1
206+
associatedtype A2
207+
}
208+
209+
protocol P2 {
210+
associatedtype A1
211+
associatedtype A2
212+
213+
func method<T: P1>(_: T) where T.A1 == A1, T.A2 == A2
214+
}
215+
216+
extension P2 {
217+
func f() { // expected-note {{did you mean 'f'?}}
218+
_ = a // expected-error {{use of unresolved identifier 'a'}}
219+
}
220+
}

0 commit comments

Comments
 (0)