Skip to content

Commit dbdb8d6

Browse files
authored
Merge pull request #6094 from slavapestov/cleanup-generic-resolver-workaround
Cleanup generic resolver workaround
2 parents 44aa9bf + 63178e8 commit dbdb8d6

16 files changed

+244
-253
lines changed

lib/AST/DeclContext.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,8 @@ GenericParamList *DeclContext::getGenericParamsOfContext() const {
206206
continue;
207207

208208
case DeclContextKind::ExtensionDecl:
209-
if (auto GP = cast<ExtensionDecl>(dc)->getGenericParams())
210-
return GP;
211-
continue;
209+
// Extensions do not capture outer generic parameters.
210+
return cast<ExtensionDecl>(dc)->getGenericParams();
212211
}
213212
llvm_unreachable("bad DeclContextKind");
214213
}
@@ -247,9 +246,8 @@ GenericSignature *DeclContext::getGenericSignatureOfContext() const {
247246

248247
case DeclContextKind::ExtensionDecl: {
249248
auto ED = cast<ExtensionDecl>(dc);
250-
if (auto genericSig = ED->getGenericSignature())
251-
return genericSig;
252-
continue;
249+
// Extensions do not capture outer generic parameters.
250+
return ED->getGenericSignature();
253251
}
254252
}
255253
llvm_unreachable("bad DeclContextKind");
@@ -288,9 +286,8 @@ GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
288286

289287
case DeclContextKind::ExtensionDecl: {
290288
auto ED = cast<ExtensionDecl>(dc);
291-
if (auto genericCtx = ED->getGenericEnvironment())
292-
return genericCtx;
293-
continue;
289+
// Extensions do not capture outer generic parameters.
290+
return ED->getGenericEnvironment();
294291
}
295292
}
296293
llvm_unreachable("bad DeclContextKind");
@@ -468,17 +465,20 @@ bool DeclContext::isGenericContext() const {
468465
case DeclContextKind::AbstractFunctionDecl:
469466
if (cast<AbstractFunctionDecl>(dc)->getGenericParams())
470467
return true;
468+
// Check parent context.
471469
continue;
472470

473471
case DeclContextKind::GenericTypeDecl:
474472
if (cast<GenericTypeDecl>(dc)->getGenericParams())
475473
return true;
474+
// Check parent context.
476475
continue;
477476

478477
case DeclContextKind::ExtensionDecl:
479478
if (cast<ExtensionDecl>(dc)->getGenericParams())
480479
return true;
481-
continue;
480+
// Extensions do not capture outer generic parameters.
481+
return false;
482482
}
483483
llvm_unreachable("bad decl context kind");
484484
}

lib/Sema/GenericTypeResolver.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GenericTypeResolver {
4747
/// \returns The resolved generic type parameter type, which may be \c gp.
4848
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp) = 0;
4949

50-
/// Resolve a reference to a member within a dependent type.
50+
/// Resolve a qualified reference to a type member within a dependent type.
5151
///
5252
/// \param baseTy The base of the member access.
5353
/// \param baseRange The source range covering the base type.
@@ -60,7 +60,7 @@ class GenericTypeResolver {
6060
SourceRange baseRange,
6161
ComponentIdentTypeRepr *ref) = 0;
6262

63-
/// Resolve a reference to an associated type within the 'Self' type
63+
/// Resolve an unqualified reference to an associated type of the 'Self' type
6464
/// of a protocol.
6565
///
6666
/// \param selfTy The base of the member access.
@@ -69,21 +69,15 @@ class GenericTypeResolver {
6969
/// \returns A type that refers to the dependent member type, or an error
7070
/// type if such a reference is ill-formed.
7171
virtual Type resolveSelfAssociatedType(Type selfTy,
72-
DeclContext *DC,
7372
AssociatedTypeDecl *assocType) = 0;
7473

75-
/// Retrieve the type when referring to the given context.
74+
/// Resolve the self type within the given context.
7675
///
7776
/// \param dc A context in which type checking occurs, which must be a type
7877
/// context (i.e., nominal type or extension thereof).
7978
///
80-
/// \param wantSelf Do we want the type of the context itself (the
81-
/// existential type, for protocols) or the type of 'self' inside the
82-
/// context (the 'Self' generic parameter, for protocols). Has no effect
83-
/// for concrete types.
84-
///
8579
/// \returns the type of context.
86-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false) = 0;
80+
virtual Type resolveTypeOfContext(DeclContext *dc) = 0;
8781

8882
/// Retrieve the type when referring to the given type declaration within
8983
/// its context.
@@ -116,10 +110,9 @@ class DependentGenericTypeResolver : public GenericTypeResolver {
116110
ComponentIdentTypeRepr *ref);
117111

118112
virtual Type resolveSelfAssociatedType(Type selfTy,
119-
DeclContext *DC,
120113
AssociatedTypeDecl *assocType);
121114

122-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
115+
virtual Type resolveTypeOfContext(DeclContext *dc);
123116

124117
virtual Type resolveTypeOfDecl(TypeDecl *decl);
125118

@@ -149,10 +142,9 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
149142
ComponentIdentTypeRepr *ref);
150143

151144
virtual Type resolveSelfAssociatedType(Type selfTy,
152-
DeclContext *DC,
153145
AssociatedTypeDecl *assocType);
154146

155-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
147+
virtual Type resolveTypeOfContext(DeclContext *dc);
156148

157149
virtual Type resolveTypeOfDecl(TypeDecl *decl);
158150

@@ -182,10 +174,9 @@ class CompleteGenericTypeResolver : public GenericTypeResolver {
182174
ComponentIdentTypeRepr *ref);
183175

184176
virtual Type resolveSelfAssociatedType(Type selfTy,
185-
DeclContext *DC,
186177
AssociatedTypeDecl *assocType);
187178

188-
virtual Type resolveTypeOfContext(DeclContext *dc, bool wantSelf=false);
179+
virtual Type resolveTypeOfContext(DeclContext *dc);
189180

190181
virtual Type resolveTypeOfDecl(TypeDecl *decl);
191182

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
5050
}
5151

5252
Type DependentGenericTypeResolver::resolveSelfAssociatedType(
53-
Type selfTy,
54-
DeclContext *DC,
55-
AssociatedTypeDecl *assocType) {
53+
Type selfTy, AssociatedTypeDecl *assocType) {
5654
auto archetype = Builder.resolveArchetype(selfTy);
5755
assert(archetype && "Bad generic context nesting?");
5856

@@ -61,15 +59,8 @@ Type DependentGenericTypeResolver::resolveSelfAssociatedType(
6159
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/true);
6260
}
6361

64-
static Type getTypeOfContext(DeclContext *dc, bool wantSelf) {
65-
if (wantSelf)
66-
return dc->getSelfInterfaceType();
67-
return dc->getDeclaredInterfaceType();
68-
}
69-
70-
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc,
71-
bool wantSelf) {
72-
return getTypeOfContext(dc, wantSelf);
62+
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
63+
return dc->getSelfInterfaceType();
7364
}
7465

7566
Type DependentGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
@@ -106,21 +97,20 @@ Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
10697
}
10798

10899
Type GenericTypeToArchetypeResolver::resolveSelfAssociatedType(
109-
Type selfTy,
110-
DeclContext *DC,
111-
AssociatedTypeDecl *assocType) {
100+
Type selfTy, AssociatedTypeDecl *assocType) {
112101
llvm_unreachable("Dependent type after archetype substitution");
113102
}
114103

115-
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc,
116-
bool wantSelf) {
104+
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc) {
117105
// FIXME: Fallback case.
118-
if (dc->isGenericContext() && !dc->isValidGenericContext())
119-
return getTypeOfContext(dc, wantSelf);
106+
if (!isa<ExtensionDecl>(dc) &&
107+
dc->isGenericContext() &&
108+
!dc->isValidGenericContext())
109+
return dc->getSelfInterfaceType();
120110

121111
return ArchetypeBuilder::mapTypeIntoContext(
122112
dc->getParentModule(), GenericEnv,
123-
getTypeOfContext(dc, wantSelf));
113+
dc->getSelfInterfaceType());
124114
}
125115

126116
Type GenericTypeToArchetypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
@@ -242,17 +232,15 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
242232
return ErrorType::get(TC.Context);
243233
}
244234

245-
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(Type selfTy,
246-
DeclContext *DC,
247-
AssociatedTypeDecl *assocType) {
235+
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(
236+
Type selfTy, AssociatedTypeDecl *assocType) {
248237
return Builder.resolveArchetype(selfTy)->getRepresentative()
249238
->getNestedType(assocType->getName(), Builder)
250239
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/false);
251240
}
252241

253-
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc,
254-
bool wantSelf) {
255-
return getTypeOfContext(dc, wantSelf);
242+
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
243+
return dc->getSelfInterfaceType();
256244
}
257245

258246
Type CompleteGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3963,6 +3963,17 @@ void ConformanceChecker::resolveSingleTypeWitness(
39633963
}
39643964
}
39653965

3966+
// Not all protocol members are requirements.
3967+
static bool isRequirement(ValueDecl *requirement) {
3968+
if (auto *FD = dyn_cast<FuncDecl>(requirement))
3969+
if (FD->isAccessor())
3970+
return false;
3971+
if (isa<TypeAliasDecl>(requirement) ||
3972+
isa<NominalTypeDecl>(requirement))
3973+
return false;
3974+
return true;
3975+
}
3976+
39663977
void ConformanceChecker::resolveSingleWitness(ValueDecl *requirement) {
39673978
assert(!isa<AssociatedTypeDecl>(requirement) && "Not a value witness");
39683979
assert(!Conformance->hasWitness(requirement) && "Already resolved");
@@ -3982,12 +3993,7 @@ void ConformanceChecker::resolveSingleWitness(ValueDecl *requirement) {
39823993
return;
39833994
}
39843995

3985-
// If this is a getter/setter for a funcdecl, ignore it.
3986-
if (auto *FD = dyn_cast<FuncDecl>(requirement))
3987-
if (FD->isAccessor())
3988-
return;
3989-
// If this is a typealias, it does not need a witness check.
3990-
if (isa<TypeAliasDecl>(requirement))
3996+
if (!isRequirement(requirement))
39913997
return;
39923998

39933999
// Resolve all associated types before trying to resolve this witness.
@@ -4132,15 +4138,11 @@ void ConformanceChecker::checkConformance() {
41324138
continue;
41334139

41344140
// Associated type requirements handled above.
4135-
if (isa<AssociatedTypeDecl>(requirement))
4141+
if (isa<TypeDecl>(requirement))
41364142
continue;
41374143

41384144
// Type aliases don't have requirements themselves.
4139-
if (isa<TypeAliasDecl>(requirement))
4140-
continue;
4141-
4142-
// Nominal types nested inside protocols are not requirements.
4143-
if (isa<NominalTypeDecl>(requirement))
4145+
if (!isRequirement(requirement))
41444146
continue;
41454147

41464148
/// Local function to finalize the witness.
@@ -5468,14 +5470,20 @@ void TypeChecker::inferDefaultWitnesses(ProtocolDecl *proto) {
54685470
DefaultWitnessChecker checker(*this, proto);
54695471

54705472
for (auto *requirement : proto->getMembers()) {
5471-
if (isa<AssociatedTypeDecl>(requirement))
5473+
if (requirement->isInvalid())
54725474
continue;
54735475

5474-
if (requirement->isInvalid())
5476+
auto *valueDecl = dyn_cast<ValueDecl>(requirement);
5477+
if (!valueDecl)
5478+
continue;
5479+
5480+
if (isa<TypeDecl>(valueDecl))
5481+
continue;
5482+
5483+
if (!isRequirement(valueDecl))
54755484
continue;
54765485

5477-
if (auto *valueDecl = dyn_cast<ValueDecl>(requirement))
5478-
checker.resolveWitnessViaLookup(valueDecl);
5486+
checker.resolveWitnessViaLookup(valueDecl);
54795487
}
54805488
}
54815489

0 commit comments

Comments
 (0)