Skip to content

Commit f34e57c

Browse files
committed
RequirementMachine: Use ProtocolDecl::getAllInheritedProtocols()
1 parent 17a6a80 commit f34e57c

File tree

6 files changed

+11
-50
lines changed

6 files changed

+11
-50
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
10721072

10731073
// Collect all typealiases from inherited protocols recursively.
10741074
llvm::MapVector<Identifier, TinyPtrVector<TypeDecl *>> inheritedTypeDecls;
1075-
for (auto *inheritedProto : ctx.getRewriteContext().getInheritedProtocols(proto)) {
1075+
for (auto *inheritedProto : proto->getAllInheritedProtocols()) {
10761076
for (auto req : inheritedProto->getMembers()) {
10771077
if (auto *typeReq = dyn_cast<TypeDecl>(req)) {
10781078
if (!isSuitableType(typeReq))

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -250,41 +250,10 @@ void RewriteContext::endTimer(StringRef name) {
250250

251251
}
252252

253-
const llvm::TinyPtrVector<const ProtocolDecl *> &
254-
RewriteContext::getInheritedProtocols(const ProtocolDecl *proto) {
255-
auto found = AllInherited.find(proto);
256-
if (found != AllInherited.end())
257-
return found->second;
258-
259-
AllInherited.insert(std::make_pair(proto, TinyPtrVector<const ProtocolDecl *>()));
260-
261-
llvm::SmallDenseSet<const ProtocolDecl *, 4> visited;
262-
llvm::TinyPtrVector<const ProtocolDecl *> protos;
263-
264-
for (auto *inheritedProto : proto->getInheritedProtocols()) {
265-
if (!visited.insert(inheritedProto).second)
266-
continue;
267-
268-
protos.push_back(inheritedProto);
269-
const auto &allInherited = getInheritedProtocols(inheritedProto);
270-
271-
for (auto *otherProto : allInherited) {
272-
if (!visited.insert(otherProto).second)
273-
continue;
274-
275-
protos.push_back(otherProto);
276-
}
277-
}
278-
279-
auto &result = AllInherited[proto];
280-
std::swap(protos, result);
281-
return result;
282-
}
283-
284253
int RewriteContext::compareProtocols(const ProtocolDecl *lhs,
285254
const ProtocolDecl *rhs) {
286-
unsigned lhsSupport = getInheritedProtocols(lhs).size();
287-
unsigned rhsSupport = getInheritedProtocols(rhs).size();
255+
unsigned lhsSupport = lhs->getAllInheritedProtocols().size();
256+
unsigned rhsSupport = rhs->getAllInheritedProtocols().size();
288257

289258
if (lhsSupport != rhsSupport)
290259
return rhsSupport - lhsSupport;

lib/AST/RequirementMachine/RewriteContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ class RewriteContext final {
5151
/// Folding set for uniquing terms.
5252
llvm::FoldingSet<Term::Storage> Terms;
5353

54-
/// Cache for transitive closure of inherited protocols.
55-
llvm::DenseMap<const ProtocolDecl *,
56-
llvm::TinyPtrVector<const ProtocolDecl *>> AllInherited;
57-
5854
/// Requirement machines built from generic signatures.
5955
llvm::DenseMap<GenericSignature, RequirementMachine *> Machines;
6056

@@ -163,9 +159,6 @@ class RewriteContext final {
163159
///
164160
//////////////////////////////////////////////////////////////////////////////
165161

166-
const llvm::TinyPtrVector<const ProtocolDecl *> &
167-
getInheritedProtocols(const ProtocolDecl *proto);
168-
169162
int compareProtocols(const ProtocolDecl *lhs,
170163
const ProtocolDecl *rhs);
171164

lib/AST/RequirementMachine/Rule.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ bool Rule::isProtocolRefinementRule(RewriteContext &ctx) const {
113113
auto *proto = LHS[0].getProtocol();
114114
auto *otherProto = LHS[1].getProtocol();
115115

116-
auto inherited = ctx.getInheritedProtocols(proto);
117-
return (std::find(inherited.begin(), inherited.end(), otherProto)
118-
!= inherited.end());
116+
return proto->inheritsFrom(otherProto);
119117
}
120118

121119
return false;

lib/AST/RequirementMachine/RuleBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void RuleBuilder::initWithProtocolSignatureRequirements(
112112
// between getTypeForTerm() and isValidTypeParameter(), we need to add rules
113113
// for inherited protocols.
114114
if (reqs.getErrors().contains(GenericSignatureErrorFlags::CompletionFailed)) {
115-
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
115+
for (auto *inheritedProto : proto->getAllInheritedProtocols()) {
116116
Requirement req(RequirementKind::Conformance,
117117
proto->getSelfInterfaceType(),
118118
inheritedProto->getDeclaredInterfaceType());
@@ -238,7 +238,7 @@ void RuleBuilder::addPermanentProtocolRules(const ProtocolDecl *proto) {
238238
for (auto *assocType : proto->getAssociatedTypeMembers())
239239
addAssociatedType(assocType, proto);
240240

241-
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
241+
for (auto *inheritedProto : proto->getAllInheritedProtocols()) {
242242
for (auto *assocType : inheritedProto->getAssociatedTypeMembers())
243243
addAssociatedType(assocType, proto);
244244
}
@@ -553,7 +553,7 @@ void RuleBuilder::collectPackShapeRules(ArrayRef<GenericTypeParamType *> generic
553553
addMemberShapeRule(proto, assocType);
554554
}
555555

556-
for (auto *inheritedProto : Context.getInheritedProtocols(proto)) {
556+
for (auto *inheritedProto : proto->getAllInheritedProtocols()) {
557557
for (auto *assocType : inheritedProto->getAssociatedTypeMembers()) {
558558
addMemberShapeRule(proto, assocType);
559559
}

test/Generics/protocol_requirement_signatures.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// RUN: %target-typecheck-verify-swift
12
// RUN: %target-typecheck-verify-swift -debug-generic-signatures > %t.dump 2>&1
23
// RUN: %FileCheck %s < %t.dump
34

@@ -17,7 +18,7 @@ protocol P3 {}
1718
// CHECK-LABEL: .Q1@
1819
// CHECK-NEXT: Requirement signature: <Self where Self.[Q1]X : P1>
1920
protocol Q1 {
20-
associatedtype X: P1 // expected-note 3{{declared here}}
21+
associatedtype X: P1 // expected-note {{declared here}}
2122
}
2223

2324
// inheritance
@@ -36,7 +37,7 @@ protocol Q3: Q1 {
3637
// CHECK-LABEL: .Q4@
3738
// CHECK-NEXT: Requirement signature: <Self where Self : Q1, Self.[Q1]X : P2>
3839
protocol Q4: Q1 {
39-
associatedtype X: P2 // expected-warning{{redeclaration of associated type 'X'}}
40+
associatedtype X: P2 // expected-warning{{redeclaration of associated type 'X'}} // expected-note 2{{declared here}}
4041
}
4142

4243
// multiple inheritance
@@ -50,7 +51,7 @@ protocol Q5: Q2, Q3, Q4 {}
5051
protocol Q6: Q2,
5152
Q3, Q4 {
5253
associatedtype X: P1
53-
// expected-warning@-1{{redeclaration of associated type 'X' from protocol 'Q1' is}}
54+
// expected-warning@-1{{redeclaration of associated type 'X' from protocol 'Q4' is}}
5455
}
5556

5657
// multiple inheritance with a new conformance

0 commit comments

Comments
 (0)