Skip to content

Commit c947eda

Browse files
committed
AST: Use ProtocolDecl::getAssociatedTypeMembers() where possible
1 parent 4551230 commit c947eda

File tree

8 files changed

+123
-142
lines changed

8 files changed

+123
-142
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ADT/DenseMap.h"
2828
#include "llvm/ADT/FoldingSet.h"
2929
#include "llvm/ADT/SmallPtrSet.h"
30+
#include "llvm/ADT/TinyPtrVector.h"
3031
#include <utility>
3132

3233
namespace swift {
@@ -174,9 +175,8 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
174175
template<typename F>
175176
bool forEachTypeWitness(LazyResolver *resolver, F f) const {
176177
const ProtocolDecl *protocol = getProtocol();
177-
for (auto req : protocol->getMembers()) {
178-
auto assocTypeReq = dyn_cast<AssociatedTypeDecl>(req);
179-
if (!assocTypeReq || req->isInvalid())
178+
for (auto assocTypeReq : protocol->getAssociatedTypeMembers()) {
179+
if (assocTypeReq->isInvalid())
180180
continue;
181181

182182
// If we don't have and cannot resolve witnesses, skip it.

include/swift/IRGen/Linking.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,9 @@ class LinkEntity {
480480
static AssociatedTypeDecl *
481481
getAssociatedTypeByIndex(const ProtocolConformance *conformance,
482482
unsigned index) {
483-
for (auto requirement : conformance->getProtocol()->getMembers()) {
484-
if (auto associate = dyn_cast<AssociatedTypeDecl>(requirement)) {
485-
if (index == 0) return associate;
486-
index--;
487-
}
483+
for (auto associate : conformance->getProtocol()->getAssociatedTypeMembers()) {
484+
if (index == 0) return associate;
485+
index--;
488486
}
489487
llvm_unreachable("didn't find associated type in protocol?");
490488
}

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,11 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
9191
}
9292

9393
// Add the associated types.
94-
for (Decl *member : protocol->getMembers()) {
95-
if (auto associatedType = dyn_cast<AssociatedTypeDecl>(member)) {
96-
// If this is a new associated type (which does not override an
97-
// existing associated type), add it.
98-
if (associatedType->getOverriddenDecls().empty())
99-
asDerived().addAssociatedType(AssociatedType(associatedType));
100-
}
94+
for (auto *associatedType : protocol->getAssociatedTypeMembers()) {
95+
// If this is a new associated type (which does not override an
96+
// existing associated type), add it.
97+
if (associatedType->getOverriddenDecls().empty())
98+
asDerived().addAssociatedType(AssociatedType(associatedType));
10199
}
102100

103101
if (asDerived().shouldVisitRequirementSignatureOnly())

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,92 +4238,90 @@ ConstraintResult GenericSignatureBuilder::expandConformanceRequirement(
42384238
};
42394239

42404240
// Add requirements for each of the associated types.
4241-
for (auto Member : proto->getMembers()) {
4242-
if (auto assocTypeDecl = dyn_cast<AssociatedTypeDecl>(Member)) {
4243-
// Add requirements placed directly on this associated type.
4244-
Type assocType =
4245-
DependentMemberType::get(selfType.getDependentType(*this), assocTypeDecl);
4246-
if (!onlySameTypeConstraints) {
4247-
auto assocResult =
4248-
addInheritedRequirements(assocTypeDecl, assocType, source,
4249-
/*inferForModule=*/nullptr);
4250-
if (isErrorResult(assocResult))
4251-
return assocResult;
4252-
}
4253-
4254-
// Add requirements from this associated type's where clause.
4255-
RequirementRequest::visitRequirements(assocTypeDecl,
4256-
TypeResolutionStage::Structural,
4257-
[&](const Requirement &req, RequirementRepr *reqRepr) {
4258-
// If we're only looking at same-type constraints, skip everything else.
4259-
if (onlySameTypeConstraints &&
4260-
req.getKind() != RequirementKind::SameType)
4261-
return false;
4262-
4263-
auto innerSource = FloatingRequirementSource::viaProtocolRequirement(
4264-
source, proto, reqRepr, /*inferred=*/false);
4265-
addRequirement(req, reqRepr, innerSource, &protocolSubMap,
4266-
/*inferForModule=*/nullptr);
4241+
for (auto assocTypeDecl : proto->getAssociatedTypeMembers()) {
4242+
// Add requirements placed directly on this associated type.
4243+
Type assocType =
4244+
DependentMemberType::get(selfType.getDependentType(*this), assocTypeDecl);
4245+
if (!onlySameTypeConstraints) {
4246+
auto assocResult =
4247+
addInheritedRequirements(assocTypeDecl, assocType, source,
4248+
/*inferForModule=*/nullptr);
4249+
if (isErrorResult(assocResult))
4250+
return assocResult;
4251+
}
4252+
4253+
// Add requirements from this associated type's where clause.
4254+
RequirementRequest::visitRequirements(assocTypeDecl,
4255+
TypeResolutionStage::Structural,
4256+
[&](const Requirement &req, RequirementRepr *reqRepr) {
4257+
// If we're only looking at same-type constraints, skip everything else.
4258+
if (onlySameTypeConstraints &&
4259+
req.getKind() != RequirementKind::SameType)
42674260
return false;
4268-
});
4269-
4270-
// Check whether we inherited any types with the same name.
4271-
auto knownInherited =
4272-
inheritedTypeDecls.find(assocTypeDecl->getFullName());
4273-
if (knownInherited == inheritedTypeDecls.end()) continue;
4274-
4275-
bool shouldWarnAboutRedeclaration =
4276-
source->kind == RequirementSource::RequirementSignatureSelf &&
4277-
!assocTypeDecl->getAttrs().hasAttribute<NonOverrideAttr>() &&
4278-
!assocTypeDecl->getAttrs().hasAttribute<OverrideAttr>() &&
4279-
!assocTypeDecl->hasDefaultDefinitionType() &&
4280-
(!assocTypeDecl->getInherited().empty() ||
4281-
assocTypeDecl->getTrailingWhereClause() ||
4282-
getASTContext().LangOpts.WarnImplicitOverrides);
4283-
for (auto inheritedType : knownInherited->second) {
4284-
// If we have inherited associated type...
4285-
if (auto inheritedAssocTypeDecl =
4286-
dyn_cast<AssociatedTypeDecl>(inheritedType)) {
4287-
// Complain about the first redeclaration.
4288-
if (shouldWarnAboutRedeclaration) {
4289-
auto inheritedFromProto = inheritedAssocTypeDecl->getProtocol();
4290-
auto fixItWhere = getProtocolWhereLoc();
4291-
Diags.diagnose(assocTypeDecl,
4292-
diag::inherited_associated_type_redecl,
4293-
assocTypeDecl->getFullName(),
4294-
inheritedFromProto->getDeclaredInterfaceType())
4295-
.fixItInsertAfter(
4296-
fixItWhere.first,
4297-
getAssociatedTypeReqs(assocTypeDecl, fixItWhere.second))
4298-
.fixItRemove(assocTypeDecl->getSourceRange());
4299-
4300-
Diags.diagnose(inheritedAssocTypeDecl, diag::decl_declared_here,
4301-
inheritedAssocTypeDecl->getFullName());
4302-
4303-
shouldWarnAboutRedeclaration = false;
4304-
}
43054261

4306-
continue;
4307-
}
4262+
auto innerSource = FloatingRequirementSource::viaProtocolRequirement(
4263+
source, proto, reqRepr, /*inferred=*/false);
4264+
addRequirement(req, reqRepr, innerSource, &protocolSubMap,
4265+
/*inferForModule=*/nullptr);
4266+
return false;
4267+
});
43084268

4309-
// We inherited a type; this associated type will be identical
4310-
// to that typealias.
4311-
if (source->kind == RequirementSource::RequirementSignatureSelf) {
4312-
auto inheritedOwningDecl =
4313-
inheritedType->getDeclContext()->getSelfNominalTypeDecl();
4269+
// Check whether we inherited any types with the same name.
4270+
auto knownInherited =
4271+
inheritedTypeDecls.find(assocTypeDecl->getFullName());
4272+
if (knownInherited == inheritedTypeDecls.end()) continue;
4273+
4274+
bool shouldWarnAboutRedeclaration =
4275+
source->kind == RequirementSource::RequirementSignatureSelf &&
4276+
!assocTypeDecl->getAttrs().hasAttribute<NonOverrideAttr>() &&
4277+
!assocTypeDecl->getAttrs().hasAttribute<OverrideAttr>() &&
4278+
!assocTypeDecl->hasDefaultDefinitionType() &&
4279+
(!assocTypeDecl->getInherited().empty() ||
4280+
assocTypeDecl->getTrailingWhereClause() ||
4281+
getASTContext().LangOpts.WarnImplicitOverrides);
4282+
for (auto inheritedType : knownInherited->second) {
4283+
// If we have inherited associated type...
4284+
if (auto inheritedAssocTypeDecl =
4285+
dyn_cast<AssociatedTypeDecl>(inheritedType)) {
4286+
// Complain about the first redeclaration.
4287+
if (shouldWarnAboutRedeclaration) {
4288+
auto inheritedFromProto = inheritedAssocTypeDecl->getProtocol();
4289+
auto fixItWhere = getProtocolWhereLoc();
43144290
Diags.diagnose(assocTypeDecl,
4315-
diag::associated_type_override_typealias,
4291+
diag::inherited_associated_type_redecl,
43164292
assocTypeDecl->getFullName(),
4317-
inheritedOwningDecl->getDescriptiveKind(),
4318-
inheritedOwningDecl->getDeclaredInterfaceType());
4293+
inheritedFromProto->getDeclaredInterfaceType())
4294+
.fixItInsertAfter(
4295+
fixItWhere.first,
4296+
getAssociatedTypeReqs(assocTypeDecl, fixItWhere.second))
4297+
.fixItRemove(assocTypeDecl->getSourceRange());
4298+
4299+
Diags.diagnose(inheritedAssocTypeDecl, diag::decl_declared_here,
4300+
inheritedAssocTypeDecl->getFullName());
4301+
4302+
shouldWarnAboutRedeclaration = false;
43194303
}
43204304

4321-
addInferredSameTypeReq(assocTypeDecl, inheritedType);
4305+
continue;
4306+
}
4307+
4308+
// We inherited a type; this associated type will be identical
4309+
// to that typealias.
4310+
if (source->kind == RequirementSource::RequirementSignatureSelf) {
4311+
auto inheritedOwningDecl =
4312+
inheritedType->getDeclContext()->getSelfNominalTypeDecl();
4313+
Diags.diagnose(assocTypeDecl,
4314+
diag::associated_type_override_typealias,
4315+
assocTypeDecl->getFullName(),
4316+
inheritedOwningDecl->getDescriptiveKind(),
4317+
inheritedOwningDecl->getDeclaredInterfaceType());
43224318
}
43234319

4324-
inheritedTypeDecls.erase(knownInherited);
4325-
continue;
4320+
addInferredSameTypeReq(assocTypeDecl, inheritedType);
43264321
}
4322+
4323+
inheritedTypeDecls.erase(knownInherited);
4324+
continue;
43274325
}
43284326

43294327
// Check all remaining inherited type declarations to determine if

lib/AST/Type.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,11 +2693,9 @@ void ArchetypeType::populateNestedTypes() const {
26932693
llvm::SmallPtrSet<Identifier, 4> knownNestedTypes;
26942694
ProtocolType::visitAllProtocols(getConformsTo(),
26952695
[&](ProtocolDecl *proto) -> bool {
2696-
for (auto member : proto->getMembers()) {
2697-
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member)) {
2698-
if (knownNestedTypes.insert(assocType->getName()).second)
2699-
nestedTypes.push_back({ assocType->getName(), Type() });
2700-
}
2696+
for (auto assocType : proto->getAssociatedTypeMembers()) {
2697+
if (knownNestedTypes.insert(assocType->getName()).second)
2698+
nestedTypes.push_back({ assocType->getName(), Type() });
27012699
}
27022700

27032701
return false;

lib/ClangImporter/ImportDecl.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7681,40 +7681,38 @@ static void finishTypeWitnesses(
76817681
auto *proto = conformance->getProtocol();
76827682
auto selfType = conformance->getType();
76837683

7684-
for (auto *req : proto->getMembers()) {
7685-
if (auto *assocType = dyn_cast<AssociatedTypeDecl>(req)) {
7686-
// FIXME: This should not happen?
7687-
if (conformance->hasTypeWitness(assocType)) continue;
7688-
7689-
bool satisfied = false;
7690-
7691-
SmallVector<ValueDecl *, 4> lookupResults;
7692-
NLOptions options = (NL_QualifiedDefault |
7693-
NL_OnlyTypes |
7694-
NL_ProtocolMembers);
7695-
7696-
dc->lookupQualified(nominal, assocType->getFullName(), options,
7697-
lookupResults);
7698-
for (auto member : lookupResults) {
7699-
auto typeDecl = cast<TypeDecl>(member);
7700-
if (isa<AssociatedTypeDecl>(typeDecl)) continue;
7701-
7702-
auto memberType = typeDecl->getDeclaredInterfaceType();
7703-
auto subMap = selfType->getContextSubstitutionMap(
7704-
module, typeDecl->getDeclContext());
7705-
memberType = memberType.subst(subMap);
7706-
conformance->setTypeWitness(assocType, memberType, typeDecl);
7707-
satisfied = true;
7708-
break;
7709-
}
7684+
for (auto *assocType : proto->getAssociatedTypeMembers()) {
7685+
// FIXME: This should not happen?
7686+
if (conformance->hasTypeWitness(assocType)) continue;
7687+
7688+
bool satisfied = false;
7689+
7690+
SmallVector<ValueDecl *, 4> lookupResults;
7691+
NLOptions options = (NL_QualifiedDefault |
7692+
NL_OnlyTypes |
7693+
NL_ProtocolMembers);
7694+
7695+
dc->lookupQualified(nominal, assocType->getFullName(), options,
7696+
lookupResults);
7697+
for (auto member : lookupResults) {
7698+
auto typeDecl = cast<TypeDecl>(member);
7699+
if (isa<AssociatedTypeDecl>(typeDecl)) continue;
7700+
7701+
auto memberType = typeDecl->getDeclaredInterfaceType();
7702+
auto subMap = selfType->getContextSubstitutionMap(
7703+
module, typeDecl->getDeclContext());
7704+
memberType = memberType.subst(subMap);
7705+
conformance->setTypeWitness(assocType, memberType, typeDecl);
7706+
satisfied = true;
7707+
break;
7708+
}
77107709

7711-
if (!satisfied) {
7712-
llvm::errs() << ("Cannot look up associated type for "
7713-
"imported conformance:\n");
7714-
conformance->getType().dump(llvm::errs());
7715-
assocType->dump(llvm::errs());
7716-
abort();
7717-
}
7710+
if (!satisfied) {
7711+
llvm::errs() << ("Cannot look up associated type for "
7712+
"imported conformance:\n");
7713+
conformance->getType().dump(llvm::errs());
7714+
assocType->dump(llvm::errs());
7715+
abort();
77187716
}
77197717
}
77207718
}

lib/IDE/CodeCompletion.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4391,10 +4391,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
43914391
auto Proto = Conformance->getProtocol();
43924392
if (!Proto->isAccessibleFrom(CurrDeclContext))
43934393
continue;
4394-
for (auto Member : Proto->getMembers()) {
4395-
auto *ATD = dyn_cast<AssociatedTypeDecl>(Member);
4396-
if (!ATD)
4397-
continue;
4394+
for (auto *ATD : Proto->getAssociatedTypeMembers()) {
43984395
// FIXME: Also exclude the type alias that has already been specified.
43994396
if (!Conformance->hasTypeWitness(ATD) ||
44004397
ATD->hasDefaultDefinitionType())

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,11 +1774,7 @@ namespace {
17741774
}
17751775

17761776
// Assume that ExpressibleByArrayLiteral contains a single associated type.
1777-
AssociatedTypeDecl *elementAssocTy = nullptr;
1778-
for (auto decl : arrayProto->getMembers()) {
1779-
if ((elementAssocTy = dyn_cast<AssociatedTypeDecl>(decl)))
1780-
break;
1781-
}
1777+
auto *elementAssocTy = arrayProto->getAssociatedTypeMembers()[0];
17821778
if (!elementAssocTy)
17831779
return Type();
17841780

@@ -1838,10 +1834,8 @@ namespace {
18381834
}
18391835

18401836
// The array element type defaults to 'Any'.
1841-
if (arrayElementTy->isTypeVariableOrMember()) {
1842-
CS.addConstraint(ConstraintKind::Defaultable, arrayElementTy,
1843-
tc.Context.TheAnyType, locator);
1844-
}
1837+
CS.addConstraint(ConstraintKind::Defaultable, arrayElementTy,
1838+
tc.Context.TheAnyType, locator);
18451839

18461840
return arrayTy;
18471841
}

0 commit comments

Comments
 (0)