Skip to content

Commit b69b877

Browse files
committed
Sema: Typo correction should use GenericSignature and not GenericSignatureBuilder
Typo correction would call directly into the GenericSignatureBuilder to get a list of all nested types of a type parameter. Since the time that code was written, higher level APIs were added to GenericSignature which originally wrapped the GenericSignatureBuilder. These days when the rewrite system is enabled, the GenericSignature operations also use the rewrite system, and the long-term goal is to get rid of GenericSignatureBuilder altogether.
1 parent fd0e1e2 commit b69b877

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

include/swift/AST/NameLookup.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717
#ifndef SWIFT_AST_NAME_LOOKUP_H
1818
#define SWIFT_AST_NAME_LOOKUP_H
1919

20-
#include "llvm/ADT/SmallVector.h"
2120
#include "swift/AST/ASTVisitor.h"
21+
#include "swift/AST/GenericSignature.h"
2222
#include "swift/AST/Identifier.h"
2323
#include "swift/AST/Module.h"
2424
#include "swift/Basic/Compiler.h"
2525
#include "swift/Basic/Debug.h"
2626
#include "swift/Basic/NullablePtr.h"
2727
#include "swift/Basic/SourceLoc.h"
2828
#include "swift/Basic/SourceManager.h"
29+
#include "llvm/ADT/SmallVector.h"
2930

3031
namespace swift {
3132
class ASTContext;
3233
class DeclName;
33-
class GenericSignatureBuilder;
3434
class Type;
3535
class TypeDecl;
3636
class ValueDecl;
@@ -487,7 +487,7 @@ void lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer,
487487
bool includeInstanceMembers,
488488
bool includeDerivedRequirements,
489489
bool includeProtocolExtensionMembers,
490-
GenericSignatureBuilder *GSB = nullptr);
490+
GenericSignature genericSig = GenericSignature());
491491

492492
namespace namelookup {
493493

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "clang/AST/DeclObjC.h"
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/GenericSignature.h"
22-
#include "swift/AST/GenericSignatureBuilder.h"
2322
#include "swift/AST/ImportCache.h"
2423
#include "swift/AST/Initializer.h"
2524
#include "swift/AST/LazyResolver.h"
@@ -413,7 +412,7 @@ static void doDynamicLookup(VisibleDeclConsumer &Consumer,
413412
}
414413

415414
namespace {
416-
typedef llvm::SmallPtrSet<TypeDecl *, 8> VisitedSet;
415+
typedef llvm::SmallPtrSet<const TypeDecl *, 8> VisitedSet;
417416
} // end anonymous namespace
418417

419418
static DeclVisibilityKind getReasonForSuper(DeclVisibilityKind Reason) {
@@ -528,15 +527,15 @@ static void
528527
lookupVisibleMemberDeclsImpl(Type BaseTy, VisibleDeclConsumer &Consumer,
529528
const DeclContext *CurrDC, LookupState LS,
530529
DeclVisibilityKind Reason,
531-
GenericSignatureBuilder *GSB,
530+
GenericSignature Sig,
532531
VisitedSet &Visited);
533532

534533
static void
535-
lookupVisibleProtocolMemberDecls(Type BaseTy, ProtocolDecl *PD,
534+
lookupVisibleProtocolMemberDecls(Type BaseTy, const ProtocolDecl *PD,
536535
VisibleDeclConsumer &Consumer,
537536
const DeclContext *CurrDC, LookupState LS,
538537
DeclVisibilityKind Reason,
539-
GenericSignatureBuilder *GSB,
538+
GenericSignature Sig,
540539
VisitedSet &Visited) {
541540
if (!Visited.insert(PD).second)
542541
return;
@@ -545,7 +544,7 @@ static void
545544
lookupVisibleProtocolMemberDecls(BaseTy, Proto,
546545
Consumer, CurrDC, LS,
547546
getReasonForSuper(Reason),
548-
GSB, Visited);
547+
Sig, Visited);
549548
lookupTypeMembers(BaseTy, PD->getDeclaredInterfaceType(),
550549
Consumer, CurrDC, LS, Reason);
551550
}
@@ -597,7 +596,7 @@ static void synthesizeMemberDeclsForLookup(NominalTypeDecl *NTD,
597596

598597
static void lookupVisibleMemberDeclsImpl(
599598
Type BaseTy, VisibleDeclConsumer &Consumer, const DeclContext *CurrDC,
600-
LookupState LS, DeclVisibilityKind Reason, GenericSignatureBuilder *GSB,
599+
LookupState LS, DeclVisibilityKind Reason, GenericSignature Sig,
601600
VisitedSet &Visited) {
602601
// Just look through l-valueness. It doesn't affect name lookup.
603602
assert(BaseTy && "lookup into null type");
@@ -628,7 +627,7 @@ static void lookupVisibleMemberDeclsImpl(
628627
// functions, and can even look up non-static functions as well (thus
629628
// getting the address of the member).
630629
lookupVisibleMemberDeclsImpl(Ty, Consumer, CurrDC, subLS, Reason,
631-
GSB, Visited);
630+
Sig, Visited);
632631
return;
633632
}
634633

@@ -652,15 +651,15 @@ static void lookupVisibleMemberDeclsImpl(
652651
if (ProtocolType *PT = BaseTy->getAs<ProtocolType>()) {
653652
lookupVisibleProtocolMemberDecls(BaseTy, PT->getDecl(),
654653
Consumer, CurrDC, LS, Reason,
655-
GSB, Visited);
654+
Sig, Visited);
656655
return;
657656
}
658657

659658
// If the base is a protocol composition, enumerate members of the protocols.
660659
if (auto PC = BaseTy->getAs<ProtocolCompositionType>()) {
661660
for (auto Member : PC->getMembers())
662661
lookupVisibleMemberDeclsImpl(Member, Consumer, CurrDC, LS, Reason,
663-
GSB, Visited);
662+
Sig, Visited);
664663
return;
665664
}
666665

@@ -669,38 +668,35 @@ static void lookupVisibleMemberDeclsImpl(
669668
for (auto Proto : Archetype->getConformsTo())
670669
lookupVisibleProtocolMemberDecls(
671670
BaseTy, Proto, Consumer, CurrDC, LS,
672-
Reason, GSB, Visited);
671+
Reason, Sig, Visited);
673672

674673
if (auto superclass = Archetype->getSuperclass())
675674
lookupVisibleMemberDeclsImpl(superclass, Consumer, CurrDC, LS,
676-
Reason, GSB, Visited);
675+
Reason, Sig, Visited);
677676
return;
678677
}
679678

680-
// If we're looking into a type parameter and we have a generic signature
681-
// builder, use the GSB to resolve where we should look.
682-
if (BaseTy->isTypeParameter() && GSB) {
683-
auto EquivClass =
684-
GSB->resolveEquivalenceClass(BaseTy,
685-
ArchetypeResolutionKind::CompleteWellFormed);
686-
if (!EquivClass) return;
687-
688-
if (EquivClass->concreteType) {
689-
BaseTy = EquivClass->concreteType;
679+
// If we're looking into a type parameter and we have a GenericSignature,
680+
// query the signature to resolve where we should look.
681+
if (BaseTy->isTypeParameter() && Sig) {
682+
// The type might be fully concrete via a same-type requirement.
683+
if (auto ConcreteTy = Sig->getConcreteType(BaseTy)) {
684+
BaseTy = ConcreteTy;
690685
} else {
691-
// Conformances
692-
for (const auto &Conforms : EquivClass->conformsTo) {
686+
// Look into protocols of conformance requirements
687+
for (const auto *Proto : Sig->getRequiredProtocols(BaseTy)) {
693688
lookupVisibleProtocolMemberDecls(
694-
BaseTy, Conforms.first, Consumer, CurrDC,
695-
LS, getReasonForSuper(Reason), GSB, Visited);
689+
BaseTy, Proto, Consumer, CurrDC,
690+
LS, getReasonForSuper(Reason), Sig, Visited);
696691
}
697692

698-
// Superclass.
699-
if (EquivClass->superclass) {
700-
lookupVisibleMemberDeclsImpl(EquivClass->superclass, Consumer, CurrDC,
693+
// Look into the superclass requirement type, if there is one.
694+
if (auto SuperclassTy = Sig->getSuperclassBound(BaseTy)) {
695+
lookupVisibleMemberDeclsImpl(SuperclassTy, Consumer, CurrDC,
701696
LS, getReasonForSuper(Reason),
702-
GSB, Visited);
697+
Sig, Visited);
703698
}
699+
704700
return;
705701
}
706702
}
@@ -1073,7 +1069,7 @@ struct KeyPathDynamicMemberConsumer : public VisibleDeclConsumer {
10731069
static void lookupVisibleDynamicMemberLookupDecls(
10741070
Type baseType, KeyPathDynamicMemberConsumer &consumer,
10751071
const DeclContext *dc, LookupState LS, DeclVisibilityKind reason,
1076-
GenericSignatureBuilder *GSB, VisitedSet &visited,
1072+
GenericSignature Sig, VisitedSet &visited,
10771073
llvm::DenseSet<TypeBase *> &seenDynamicLookup);
10781074

10791075
/// Enumerates all members of \c baseType, including both directly visible and
@@ -1084,11 +1080,11 @@ static void lookupVisibleDynamicMemberLookupDecls(
10841080
static void lookupVisibleMemberAndDynamicMemberDecls(
10851081
Type baseType, VisibleDeclConsumer &consumer,
10861082
KeyPathDynamicMemberConsumer &dynamicMemberConsumer, const DeclContext *DC,
1087-
LookupState LS, DeclVisibilityKind reason, GenericSignatureBuilder *GSB,
1083+
LookupState LS, DeclVisibilityKind reason, GenericSignature Sig,
10881084
VisitedSet &visited, llvm::DenseSet<TypeBase *> &seenDynamicLookup) {
1089-
lookupVisibleMemberDeclsImpl(baseType, consumer, DC, LS, reason, GSB, visited);
1085+
lookupVisibleMemberDeclsImpl(baseType, consumer, DC, LS, reason, Sig, visited);
10901086
lookupVisibleDynamicMemberLookupDecls(baseType, dynamicMemberConsumer, DC, LS,
1091-
reason, GSB, visited, seenDynamicLookup);
1087+
reason, Sig, visited, seenDynamicLookup);
10921088
}
10931089

10941090
/// Enumerates all keypath dynamic members of \c baseType, as seen from the
@@ -1100,7 +1096,7 @@ static void lookupVisibleMemberAndDynamicMemberDecls(
11001096
static void lookupVisibleDynamicMemberLookupDecls(
11011097
Type baseType, KeyPathDynamicMemberConsumer &consumer,
11021098
const DeclContext *dc, LookupState LS, DeclVisibilityKind reason,
1103-
GenericSignatureBuilder *GSB, VisitedSet &visited,
1099+
GenericSignature Sig, VisitedSet &visited,
11041100
llvm::DenseSet<TypeBase *> &seenDynamicLookup) {
11051101
if (!seenDynamicLookup.insert(baseType.getPointer()).second)
11061102
return;
@@ -1138,7 +1134,7 @@ static void lookupVisibleDynamicMemberLookupDecls(
11381134
baseType);
11391135

11401136
lookupVisibleMemberAndDynamicMemberDecls(memberType, consumer, consumer, dc,
1141-
LS, reason, GSB, visited,
1137+
LS, reason, Sig, visited,
11421138
seenDynamicLookup);
11431139
}
11441140
}
@@ -1151,7 +1147,7 @@ static void lookupVisibleDynamicMemberLookupDecls(
11511147
/// binding.
11521148
static void lookupVisibleMemberDecls(
11531149
Type BaseTy, VisibleDeclConsumer &Consumer, const DeclContext *CurrDC,
1154-
LookupState LS, DeclVisibilityKind Reason, GenericSignatureBuilder *GSB) {
1150+
LookupState LS, DeclVisibilityKind Reason, GenericSignature Sig) {
11551151
OverrideFilteringConsumer overrideConsumer(BaseTy, CurrDC);
11561152
KeyPathDynamicMemberConsumer dynamicConsumer(
11571153
Consumer,
@@ -1161,7 +1157,7 @@ static void lookupVisibleMemberDecls(
11611157
llvm::DenseSet<TypeBase *> seenDynamicLookup;
11621158
lookupVisibleMemberAndDynamicMemberDecls(
11631159
BaseTy, overrideConsumer, dynamicConsumer, CurrDC, LS, Reason,
1164-
GSB, Visited, seenDynamicLookup);
1160+
Sig, Visited, seenDynamicLookup);
11651161

11661162
// Report the declarations we found to the real consumer.
11671163
overrideConsumer.filterDecls(Consumer);
@@ -1332,7 +1328,7 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
13321328
bool includeInstanceMembers,
13331329
bool includeDerivedRequirements,
13341330
bool includeProtocolExtensionMembers,
1335-
GenericSignatureBuilder *GSB) {
1331+
GenericSignature Sig) {
13361332
assert(CurrDC);
13371333
LookupState ls = LookupState::makeQualified();
13381334
if (includeInstanceMembers) {
@@ -1347,5 +1343,5 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
13471343

13481344
::lookupVisibleMemberDecls(BaseTy, Consumer, CurrDC, ls,
13491345
DeclVisibilityKind::MemberOfCurrentNominal,
1350-
GSB);
1346+
Sig);
13511347
}

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
595595
Type baseTypeOrNull,
596596
NameLookupOptions lookupOptions,
597597
TypoCorrectionResults &corrections,
598-
GenericSignatureBuilder *gsb,
598+
GenericSignature genericSig,
599599
unsigned maxResults) {
600600
// Disable typo-correction if we won't show the diagnostic anyway or if
601601
// we've hit our typo correction limit.
@@ -636,7 +636,8 @@ void TypeChecker::performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
636636
lookupVisibleMemberDecls(consumer, baseTypeOrNull, DC,
637637
/*includeInstanceMembers*/true,
638638
/*includeDerivedRequirements*/false,
639-
/*includeProtocolExtensionMembers*/true, gsb);
639+
/*includeProtocolExtensionMembers*/true,
640+
genericSig);
640641
} else {
641642
lookupVisibleDecls(consumer, DC, /*top level*/ true,
642643
corrections.Loc.getBaseNameLoc());

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ Type TypeResolution::resolveDependentMemberType(
193193
}
194194

195195
assert(stage == TypeResolutionStage::Interface);
196-
if (!getGenericSignature())
196+
auto genericSig = getGenericSignature();
197+
if (!genericSig)
197198
return ErrorType::get(baseTy);
198199

199200
auto builder = getGenericSignatureBuilder();
@@ -218,7 +219,7 @@ Type TypeResolution::resolveDependentMemberType(
218219
TypeChecker::performTypoCorrection(DC, DeclRefKind::Ordinary,
219220
MetatypeType::get(baseTy),
220221
defaultMemberLookupOptions,
221-
corrections, builder);
222+
corrections, genericSig);
222223

223224
// Check whether we have a single type result.
224225
auto singleType = cast_or_null<TypeDecl>(

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Availability.h"
2424
#include "swift/AST/DiagnosticsSema.h"
2525
#include "swift/AST/GenericParamList.h"
26+
#include "swift/AST/GenericSignature.h"
2627
#include "swift/AST/KnownProtocols.h"
2728
#include "swift/AST/LazyResolver.h"
2829
#include "swift/AST/NameLookup.h"
@@ -42,7 +43,6 @@ class Decl;
4243
class DeclAttribute;
4344
class DiagnosticEngine;
4445
class ExportContext;
45-
class GenericSignatureBuilder;
4646
class NominalTypeDecl;
4747
class NormalProtocolConformance;
4848
class RootProtocolConformance;
@@ -1109,7 +1109,7 @@ void performTypoCorrection(DeclContext *DC, DeclRefKind refKind,
11091109
Type baseTypeOrNull,
11101110
NameLookupOptions lookupOptions,
11111111
TypoCorrectionResults &corrections,
1112-
GenericSignatureBuilder *gsb = nullptr,
1112+
GenericSignature genericSig = GenericSignature(),
11131113
unsigned maxResults = 4);
11141114

11151115
/// Check if the given decl has a @_semantics attribute that gives it

0 commit comments

Comments
 (0)