Skip to content

Commit 66c98a4

Browse files
authored
Merge pull request #22458 from benlangmuir/witness-5
[5.0] [index] Add back override relation for associated type witnesses
2 parents fe280cf + f5a453e commit 66c98a4

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

lib/Index/Index.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class SourceFileOrModule {
129129
}
130130
};
131131

132-
struct ValueWitness {
132+
struct IndexedWitness {
133133
ValueDecl *Member;
134134
ValueDecl *Requirement;
135135
};
@@ -146,7 +146,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
146146
Decl *D;
147147
SymbolInfo SymInfo;
148148
SymbolRoleSet Roles;
149-
SmallVector<ValueWitness, 6> ExplicitValueWitnesses;
149+
SmallVector<IndexedWitness, 6> ExplicitWitnesses;
150150
SmallVector<SourceLoc, 6> RefsToSuppress;
151151
};
152152
SmallVector<Entity, 6> EntitiesStack;
@@ -462,8 +462,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
462462
bool reportExtension(ExtensionDecl *D);
463463
bool reportRef(ValueDecl *D, SourceLoc Loc, IndexSymbol &Info,
464464
Optional<AccessKind> AccKind);
465-
bool reportImplicitValueConformance(ValueDecl *witness, ValueDecl *requirement,
466-
Decl *container);
465+
bool reportImplicitConformance(ValueDecl *witness, ValueDecl *requirement,
466+
Decl *container);
467467

468468
bool startEntity(Decl *D, IndexSymbol &Info, bool IsRef);
469469
bool startEntityDecl(ValueDecl *D);
@@ -535,7 +535,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
535535
/// members.
536536
///
537537
/// \returns false if AST visitation should stop.
538-
bool handleValueWitnesses(Decl *D, SmallVectorImpl<ValueWitness> &explicitValueWitnesses);
538+
bool handleWitnesses(Decl *D, SmallVectorImpl<IndexedWitness> &explicitWitnesses);
539539

540540
void getModuleHash(SourceFileOrModule SFOrMod, llvm::raw_ostream &OS);
541541
llvm::hash_code hashModule(llvm::hash_code code, SourceFileOrModule SFOrMod);
@@ -695,7 +695,7 @@ bool IndexSwiftASTWalker::visitImports(
695695
return true;
696696
}
697697

698-
bool IndexSwiftASTWalker::handleValueWitnesses(Decl *D, SmallVectorImpl<ValueWitness> &explicitValueWitnesses) {
698+
bool IndexSwiftASTWalker::handleWitnesses(Decl *D, SmallVectorImpl<IndexedWitness> &explicitWitnesses) {
699699
auto DC = dyn_cast<DeclContext>(D);
700700
if (!DC)
701701
return true;
@@ -719,11 +719,26 @@ bool IndexSwiftASTWalker::handleValueWitnesses(Decl *D, SmallVectorImpl<ValueWit
719719
return;
720720

721721
if (decl->getDeclContext() == DC) {
722-
explicitValueWitnesses.push_back(ValueWitness{decl, req});
722+
explicitWitnesses.push_back({decl, req});
723+
} else {
724+
reportImplicitConformance(decl, req, D);
725+
}
726+
});
727+
728+
normal->forEachTypeWitness(nullptr,
729+
[&](AssociatedTypeDecl *assoc, Type type, TypeDecl *typeDecl) {
730+
if (Cancelled)
731+
return true;
732+
if (typeDecl == nullptr)
733+
return false;
734+
735+
if (typeDecl->getDeclContext() == DC) {
736+
explicitWitnesses.push_back({typeDecl, assoc});
723737
} else {
724738
// Report the implicit conformance.
725-
reportImplicitValueConformance(decl, req, D);
739+
reportImplicitConformance(typeDecl, assoc, D);
726740
}
741+
return false;
727742
});
728743
}
729744

@@ -741,12 +756,12 @@ bool IndexSwiftASTWalker::startEntity(Decl *D, IndexSymbol &Info, bool IsRef) {
741756
case swift::index::IndexDataConsumer::Skip:
742757
return false;
743758
case swift::index::IndexDataConsumer::Continue: {
744-
SmallVector<ValueWitness, 6> explicitValueWitnesses;
759+
SmallVector<IndexedWitness, 6> explicitWitnesses;
745760
if (!IsRef) {
746-
if (!handleValueWitnesses(D, explicitValueWitnesses))
761+
if (!handleWitnesses(D, explicitWitnesses))
747762
return false;
748763
}
749-
EntitiesStack.push_back({D, Info.symInfo, Info.roles, std::move(explicitValueWitnesses), {}});
764+
EntitiesStack.push_back({D, Info.symInfo, Info.roles, std::move(explicitWitnesses), {}});
750765
return true;
751766
}
752767
}
@@ -781,7 +796,7 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
781796
}
782797

783798
if (auto Parent = getParentDecl()) {
784-
for (const ValueWitness &witness : EntitiesStack.back().ExplicitValueWitnesses) {
799+
for (const IndexedWitness &witness : EntitiesStack.back().ExplicitWitnesses) {
785800
if (witness.Member == D)
786801
addRelation(Info, (SymbolRoleSet) SymbolRole::RelationOverrideOf, witness.Requirement);
787802
}
@@ -1104,8 +1119,8 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
11041119
return finishCurrentEntity();
11051120
}
11061121

1107-
bool IndexSwiftASTWalker::reportImplicitValueConformance(ValueDecl *witness, ValueDecl *requirement,
1108-
Decl *container) {
1122+
bool IndexSwiftASTWalker::reportImplicitConformance(ValueDecl *witness, ValueDecl *requirement,
1123+
Decl *container) {
11091124
if (!shouldIndex(witness, /*IsRef=*/true))
11101125
return true; // keep walking
11111126

test/Index/conformances.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,29 @@ extension InheritingP { // CHECK: [[@LINE]]:11 | extension/ext-protocol/Swift |
9898
// CHECK-NEXT: RelOver | instance-method/Swift | foo() | [[InheritingP_foo_USR]]
9999
// CHECK-NEXT: RelChild | extension/ext-protocol/Swift | InheritingP | [[InheritingP_USR]]
100100
}
101+
102+
protocol WithAssocType {
103+
associatedtype T // CHECK: [[@LINE]]:18 | type-alias/associated-type/Swift | T | [[WithAssocT_USR:.*]] | Def
104+
func foo() -> T // CHECK: [[@LINE]]:17 | type-alias/associated-type/Swift | T | [[WithAssocT_USR]] | Ref
105+
}
106+
107+
struct SAssocTypeAlias: WithAssocType {
108+
typealias T = Int // CHECK: [[@LINE]]:13 | type-alias/Swift | T | [[SAssocT:.*]] | Def,RelChild,RelOver | rel: 2
109+
// CHECK-NEXT: RelOver | type-alias/associated-type/Swift | T | [[WithAssocT_USR]]
110+
// CHECK-NEXT: RelChild | struct/Swift | SAssocTypeAlias
111+
func foo() -> T { return 0 } // CHECK: [[@LINE]]:17 | type-alias/Swift | T | [[SAssocT:.*]] | Ref
112+
}
113+
114+
struct SAssocTypeInferred: WithAssocType {
115+
func foo() -> Int { return 1 }
116+
func bar() -> T { return 2 } // CHECK: [[@LINE]]:17 | type-alias/associated-type/Swift | T | [[WithAssocT_USR]] | Ref
117+
}
118+
119+
struct AssocViaExtension {
120+
struct T {} // CHECK: [[@LINE]]:10 | struct/Swift | T | [[AssocViaExtensionT_USR:.*]] | Def
121+
func foo() -> T { return T() }
122+
}
123+
124+
extension AssocViaExtension: WithAssocType {} // CHECK: [[@LINE]]:11 | struct/Swift | T | [[AssocViaExtensionT_USR]] | Impl,RelOver,RelCont | rel: 2
125+
// CHECK-NEXT: RelOver | type-alias/associated-type/Swift | T | [[WithAssocT_USR]]
126+
// CHECK-NEXT: RelCont | extension/ext-struct/Swift | AssocViaExtension

0 commit comments

Comments
 (0)