Skip to content

Commit 4f2ae17

Browse files
authored
Merge pull request swiftlang#18809 from DougGregor/collapse-protocol-requirement-type-resolver
2 parents 700da2e + 2905274 commit 4f2ae17

File tree

4 files changed

+16
-52
lines changed

4 files changed

+16
-52
lines changed

lib/Sema/GenericTypeResolver.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,6 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
103103
virtual bool areSameType(Type type1, Type type2);
104104
};
105105

106-
/// Generic type resolver that only handles what can appear in a protocol
107-
/// definition, i.e. Self, and Self.A.B.C dependent types.
108-
///
109-
/// This should only be used when resolving/validating where clauses in
110-
/// protocols.
111-
class ProtocolRequirementTypeResolver : public GenericTypeResolver {
112-
public:
113-
virtual bool usesArchetypes() { return false; }
114-
115-
virtual Type mapTypeIntoContext(Type type);
116-
117-
virtual Type resolveDependentMemberType(Type baseTy, DeclContext *DC,
118-
SourceRange baseRange,
119-
ComponentIdentTypeRepr *ref);
120-
121-
virtual bool areSameType(Type type1, Type type2);
122-
};
123-
124106
/// Generic type resolver that performs complete resolution of dependent
125107
/// types based on a given generic signature builder.
126108
///

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class DenseMapInfo<RawValueKey> {
208208
static void validateAttributes(TypeChecker &TC, Decl *D);
209209

210210
void TypeChecker::resolveTrailingWhereClause(ProtocolDecl *proto) {
211-
ProtocolRequirementTypeResolver resolver;
211+
DependentGenericTypeResolver resolver;
212212
validateWhereClauses(proto, &resolver);
213213
}
214214

@@ -4488,7 +4488,7 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
44884488
auto helper = [&] {
44894489
(void) typealias->getFormalAccess();
44904490

4491-
ProtocolRequirementTypeResolver resolver;
4491+
DependentGenericTypeResolver resolver;
44924492
TypeResolutionOptions options(TypeResolverContext::TypeAliasDecl);
44934493
if (validateType(typealias->getUnderlyingTypeLoc(),
44944494
typealias, options, &resolver)) {

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
4343
}
4444

4545
bool DependentGenericTypeResolver::areSameType(Type type1, Type type2) {
46-
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
47-
return type1->isEqual(type2);
46+
if (type1->isEqual(type2))
47+
return true;
48+
49+
// If both refer to associated types with the same name, they'll implicitly
50+
// be considered equivalent.
51+
auto depMem1 = type1->getAs<DependentMemberType>();
52+
if (!depMem1) return false;
53+
54+
auto depMem2 = type2->getAs<DependentMemberType>();
55+
if (!depMem2) return false;
56+
57+
if (depMem1->getName() != depMem2->getName()) return false;
4858

49-
// Conservative answer: they could be the same.
50-
return true;
59+
return areSameType(depMem1->getBase(), depMem2->getBase());
5160
}
5261

5362
Type GenericTypeToArchetypeResolver::mapTypeIntoContext(Type type) {
@@ -66,33 +75,6 @@ bool GenericTypeToArchetypeResolver::areSameType(Type type1, Type type2) {
6675
return type1->isEqual(type2);
6776
}
6877

69-
Type ProtocolRequirementTypeResolver::mapTypeIntoContext(Type type) {
70-
return type;
71-
}
72-
73-
Type ProtocolRequirementTypeResolver::resolveDependentMemberType(
74-
Type baseTy, DeclContext *DC, SourceRange baseRange,
75-
ComponentIdentTypeRepr *ref) {
76-
return DependentMemberType::get(baseTy, ref->getIdentifier());
77-
}
78-
79-
bool ProtocolRequirementTypeResolver::areSameType(Type type1, Type type2) {
80-
if (type1->isEqual(type2))
81-
return true;
82-
83-
// If both refer to associated types with the same name, they'll implicitly
84-
// be considered equivalent.
85-
auto depMem1 = type1->getAs<DependentMemberType>();
86-
if (!depMem1) return false;
87-
88-
auto depMem2 = type2->getAs<DependentMemberType>();
89-
if (!depMem2) return false;
90-
91-
if (depMem1->getName() != depMem2->getName()) return false;
92-
93-
return areSameType(depMem1->getBase(), depMem2->getBase());
94-
}
95-
9678
CompleteGenericTypeResolver::CompleteGenericTypeResolver(
9779
TypeChecker &tc,
9880
GenericSignature *genericSig)

lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ InheritedTypeRequest::evaluate(
4040
options |= TypeResolutionFlags::AllowUnavailableProtocol;
4141
}
4242

43-
ProtocolRequirementTypeResolver protoResolver;
43+
DependentGenericTypeResolver protoResolver;
4444
GenericTypeToArchetypeResolver archetypeResolver(dc);
4545
GenericTypeResolver *resolver;
4646
if (isa<ProtocolDecl>(dc)) {

0 commit comments

Comments
 (0)