Skip to content

Commit bdbc475

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 c538c52 commit bdbc475

File tree

2 files changed

+23
-90
lines changed

2 files changed

+23
-90
lines changed

lib/AST/LookupVisibleDecls.cpp

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

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

924837
// Report the declarations we found to the real consumer.
925-
restateConsumer.feedResultsToConsumer(overrideConsumer);
926838
for (const auto &DeclAndReason : overrideConsumer.DeclsToReport)
927839
Consumer.foundDecl(DeclAndReason.D, DeclAndReason.Reason);
928840
}

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)