Skip to content

Commit 773d7b3

Browse files
committed
Sema: Remove more methods from GenericTypeResolver
resolveGenericTypeParamType(), resolveTypeOfDecl() and resolveTypeOfContext() would all take the type of a declaration, and optionally map it into context. Replace them with a mapTypeIntoContext() that takes a type and either returns it verbatim or maps it into context.
1 parent d1610a5 commit 773d7b3

7 files changed

+46
-130
lines changed

lib/Sema/GenericTypeResolver.h

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ class GenericTypeResolver {
3636
public:
3737
virtual ~GenericTypeResolver();
3838

39-
/// Resolve the given generic type parameter to its type.
40-
///
41-
/// This routine is used whenever type checking encounters a reference to a
42-
/// generic parameter. It can replace the generic parameter with (for example)
43-
/// a concrete type or an archetype, depending on context.
44-
///
45-
/// \param gp The generic parameter to resolve.
46-
///
47-
/// \returns The resolved generic type parameter type, which may be \c gp.
48-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp) = 0;
39+
/// Resolve the given interface type to a contextual type if necessary.
40+
virtual Type mapTypeIntoContext(Type type) = 0;
4941

5042
/// Resolve a qualified reference to a type member within a dependent type.
5143
///
@@ -60,22 +52,6 @@ class GenericTypeResolver {
6052
SourceRange baseRange,
6153
ComponentIdentTypeRepr *ref) = 0;
6254

63-
/// Resolve the self type within the given context.
64-
///
65-
/// \param dc A context in which type checking occurs, which must be a type
66-
/// context (i.e., nominal type or extension thereof).
67-
///
68-
/// \returns the type of context.
69-
virtual Type resolveTypeOfContext(DeclContext *dc) = 0;
70-
71-
/// Retrieve the type when referring to the given type declaration within
72-
/// its context.
73-
///
74-
/// \param decl A type declaration.
75-
///
76-
/// \returns the type of the declaration in context..
77-
virtual Type resolveTypeOfDecl(TypeDecl *decl) = 0;
78-
7955
/// Determine whether the given types are equivalent within the generic
8056
/// context.
8157
virtual bool areSameType(Type type1, Type type2) = 0;
@@ -90,17 +66,13 @@ class GenericTypeResolver {
9066
/// and only trivially resolves dependent member types.
9167
class DependentGenericTypeResolver : public GenericTypeResolver {
9268
public:
93-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
69+
virtual Type mapTypeIntoContext(Type type);
9470

9571
virtual Type resolveDependentMemberType(Type baseTy,
9672
DeclContext *DC,
9773
SourceRange baseRange,
9874
ComponentIdentTypeRepr *ref);
9975

100-
virtual Type resolveTypeOfContext(DeclContext *dc);
101-
102-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
103-
10476
virtual bool areSameType(Type type1, Type type2);
10577

10678
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -121,16 +93,12 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
12193
explicit GenericTypeToArchetypeResolver(DeclContext *dc)
12294
: GenericEnv(dc->getGenericEnvironmentOfContext()) { }
12395

124-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
96+
virtual Type mapTypeIntoContext(Type type);
12597

12698
virtual Type resolveDependentMemberType(Type baseTy, DeclContext *DC,
12799
SourceRange baseRange,
128100
ComponentIdentTypeRepr *ref);
129101

130-
virtual Type resolveTypeOfContext(DeclContext *dc);
131-
132-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
133-
134102
virtual bool areSameType(Type type1, Type type2);
135103

136104
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -142,22 +110,13 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
142110
/// This should only be used when resolving/validating where clauses in
143111
/// protocols.
144112
class ProtocolRequirementTypeResolver : public GenericTypeResolver {
145-
ProtocolDecl *Proto;
146-
147113
public:
148-
explicit ProtocolRequirementTypeResolver(ProtocolDecl *proto)
149-
: Proto(proto) {}
150-
151-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
114+
virtual Type mapTypeIntoContext(Type type);
152115

153116
virtual Type resolveDependentMemberType(Type baseTy, DeclContext *DC,
154117
SourceRange baseRange,
155118
ComponentIdentTypeRepr *ref);
156119

157-
virtual Type resolveTypeOfContext(DeclContext *dc);
158-
159-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
160-
161120
virtual bool areSameType(Type type1, Type type2);
162121

163122
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -180,17 +139,13 @@ class CompleteGenericTypeResolver : public GenericTypeResolver {
180139
ArrayRef<GenericTypeParamType *> genericParams)
181140
: TC(tc), Builder(builder), GenericParams(genericParams) { }
182141

183-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
142+
virtual Type mapTypeIntoContext(Type type);
184143

185144
virtual Type resolveDependentMemberType(Type baseTy,
186145
DeclContext *DC,
187146
SourceRange baseRange,
188147
ComponentIdentTypeRepr *ref);
189148

190-
virtual Type resolveTypeOfContext(DeclContext *dc);
191-
192-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
193-
194149
virtual bool areSameType(Type type1, Type type2);
195150

196151
virtual void recordParamType(ParamDecl *decl, Type ty);

lib/Sema/ITCDecl.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ void IterativeTypeChecker::processResolveInheritedClauseEntry(
108108

109109
// Validate the type of this inherited clause entry.
110110
// FIXME: Recursion into existing type checker.
111-
Optional<ProtocolRequirementTypeResolver> protoResolver;
112-
Optional<GenericTypeToArchetypeResolver> archetypeResolver;
111+
ProtocolRequirementTypeResolver protoResolver;
112+
GenericTypeToArchetypeResolver archetypeResolver(dc);
113113
GenericTypeResolver *resolver;
114-
if (auto *proto = dyn_cast<ProtocolDecl>(dc)) {
115-
protoResolver.emplace(proto);
116-
resolver = protoResolver.getPointer();
114+
if (isa<ProtocolDecl>(dc)) {
115+
resolver = &protoResolver;
117116
} else {
118-
archetypeResolver.emplace(dc);
119-
resolver = archetypeResolver.getPointer();
117+
resolver = &archetypeResolver;
120118
}
121119

122120
if (TC.validateType(*inherited, dc, options, resolver,

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void TypeChecker::resolveRawType(EnumDecl *enumDecl) {
243243
}
244244

245245
void TypeChecker::validateWhereClauses(ProtocolDecl *protocol) {
246-
ProtocolRequirementTypeResolver resolver(protocol);
246+
ProtocolRequirementTypeResolver resolver;
247247
TypeResolutionOptions options;
248248

249249
if (auto whereClause = protocol->getTrailingWhereClause()) {
@@ -7497,9 +7497,9 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
74977497
return;
74987498

74997499
// Perform earlier validation of typealiases in protocols.
7500-
if (auto proto = dyn_cast<ProtocolDecl>(dc)) {
7500+
if (isa<ProtocolDecl>(dc)) {
75017501
if (!typealias->getGenericParams()) {
7502-
ProtocolRequirementTypeResolver resolver(proto);
7502+
ProtocolRequirementTypeResolver resolver;
75037503
TypeResolutionOptions options;
75047504

75057505
if (typealias->isBeingValidated()) return;

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ using namespace swift;
2929
/// GenericTypeResolver implementations
3030
///
3131

32-
Type DependentGenericTypeResolver::resolveGenericTypeParamType(
33-
GenericTypeParamType *gp) {
34-
assert(gp->getDecl() && "Missing generic parameter declaration");
35-
36-
// Don't resolve generic parameters.
37-
return gp;
32+
Type DependentGenericTypeResolver::mapTypeIntoContext(Type type) {
33+
return type;
3834
}
3935

4036
Type DependentGenericTypeResolver::resolveDependentMemberType(
@@ -45,14 +41,6 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
4541
return DependentMemberType::get(baseTy, ref->getIdentifier());
4642
}
4743

48-
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
49-
return dc->getSelfInterfaceType();
50-
}
51-
52-
Type DependentGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
53-
return decl->getDeclaredInterfaceType();
54-
}
55-
5644
bool DependentGenericTypeResolver::areSameType(Type type1, Type type2) {
5745
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
5846
return type1->isEqual(type2);
@@ -65,9 +53,8 @@ void DependentGenericTypeResolver::recordParamType(ParamDecl *decl, Type type) {
6553
// Do nothing
6654
}
6755

68-
Type GenericTypeToArchetypeResolver::resolveGenericTypeParamType(
69-
GenericTypeParamType *gp) {
70-
return GenericEnv->mapTypeIntoContext(gp);
56+
Type GenericTypeToArchetypeResolver::mapTypeIntoContext(Type type) {
57+
return GenericEnvironment::mapTypeIntoContext(GenericEnv, type);
7158
}
7259

7360
Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
@@ -78,16 +65,6 @@ Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
7865
llvm_unreachable("Dependent type after archetype substitution");
7966
}
8067

81-
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc) {
82-
return GenericEnvironment::mapTypeIntoContext(
83-
GenericEnv, dc->getSelfInterfaceType());
84-
}
85-
86-
Type GenericTypeToArchetypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
87-
return GenericEnvironment::mapTypeIntoContext(
88-
GenericEnv, decl->getDeclaredInterfaceType());
89-
}
90-
9168
bool GenericTypeToArchetypeResolver::areSameType(Type type1, Type type2) {
9269
return type1->isEqual(type2);
9370
}
@@ -106,11 +83,8 @@ void GenericTypeToArchetypeResolver::recordParamType(ParamDecl *decl, Type type)
10683
GenericEnv, type));
10784
}
10885

109-
Type ProtocolRequirementTypeResolver::resolveGenericTypeParamType(
110-
GenericTypeParamType *gp) {
111-
assert(gp->isEqual(Proto->getSelfInterfaceType()) &&
112-
"found non-Self-shaped GTPT when resolving protocol requirement");
113-
return gp;
86+
Type ProtocolRequirementTypeResolver::mapTypeIntoContext(Type type) {
87+
return type;
11488
}
11589

11690
Type ProtocolRequirementTypeResolver::resolveDependentMemberType(
@@ -119,14 +93,6 @@ Type ProtocolRequirementTypeResolver::resolveDependentMemberType(
11993
return DependentMemberType::get(baseTy, ref->getIdentifier());
12094
}
12195

122-
Type ProtocolRequirementTypeResolver::resolveTypeOfContext(DeclContext *dc) {
123-
return dc->getSelfInterfaceType();
124-
}
125-
126-
Type ProtocolRequirementTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
127-
return decl->getDeclaredInterfaceType();
128-
}
129-
13096
bool ProtocolRequirementTypeResolver::areSameType(Type type1, Type type2) {
13197
if (type1->isEqual(type2))
13298
return true;
@@ -150,13 +116,10 @@ void ProtocolRequirementTypeResolver::recordParamType(ParamDecl *decl,
150116
"recording a param type of a protocol requirement doesn't make sense");
151117
}
152118

153-
Type CompleteGenericTypeResolver::resolveGenericTypeParamType(
154-
GenericTypeParamType *gp) {
155-
assert(gp->getDecl() && "Missing generic parameter declaration");
156-
return gp;
119+
Type CompleteGenericTypeResolver::mapTypeIntoContext(Type type) {
120+
return type;
157121
}
158122

159-
160123
Type CompleteGenericTypeResolver::resolveDependentMemberType(
161124
Type baseTy,
162125
DeclContext *DC,
@@ -228,14 +191,6 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
228191
return ErrorType::get(TC.Context);
229192
}
230193

231-
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
232-
return dc->getSelfInterfaceType();
233-
}
234-
235-
Type CompleteGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
236-
return decl->getDeclaredInterfaceType();
237-
}
238-
239194
bool CompleteGenericTypeResolver::areSameType(Type type1, Type type2) {
240195
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
241196
return type1->isEqual(type2);

lib/Sema/TypeCheckType.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ findDeclContextForType(TypeChecker &TC,
252252
// protocol extension, always use the nominal type and
253253
// not the protocol 'Self' type.
254254
if (isa<NominalTypeDecl>(typeDecl))
255-
return resolver->resolveTypeOfDecl(
256-
DC->getAsNominalTypeOrNominalTypeExtensionContext());
255+
return resolver->mapTypeIntoContext(
256+
DC->getDeclaredInterfaceType());
257257

258258
// Otherwise, we want the protocol 'Self' type for
259259
// substituting into alias types and associated types.
260-
return resolver->resolveTypeOfContext(DC);
260+
return resolver->mapTypeIntoContext(
261+
DC->getSelfInterfaceType());
261262
};
262263

263264
// First, check for direct containment in one of our parent contexts.
@@ -357,7 +358,8 @@ findDeclContextForType(TypeChecker &TC,
357358
}
358359
} else {
359360
// Get the substituted superclass type, if any.
360-
superclass = resolver->resolveTypeOfContext(parentDC)->getSuperclass();
361+
superclass = resolver->mapTypeIntoContext(
362+
parentDC->getSelfInterfaceType())->getSuperclass();
361363

362364
// Start with the type of the current context.
363365
auto fromNominal = parentDC->getAsNominalTypeOrNominalTypeExtensionContext();
@@ -435,12 +437,14 @@ Type TypeChecker::resolveTypeInContext(
435437
auto *parentNominal =
436438
parentDC->getAsNominalTypeOrNominalTypeExtensionContext();
437439
if (parentNominal == nominalType)
438-
return resolver->resolveTypeOfContext(parentDC);
440+
return resolver->mapTypeIntoContext(
441+
parentDC->getSelfInterfaceType());
439442
if (isa<ExtensionDecl>(parentDC)) {
440443
auto *extendedType = parentNominal;
441444
while (extendedType != nullptr) {
442445
if (extendedType == nominalType)
443-
return resolver->resolveTypeOfDecl(extendedType);
446+
return resolver->mapTypeIntoContext(
447+
extendedType->getDeclaredInterfaceType());
444448
extendedType = extendedType->getParent()
445449
->getAsNominalTypeOrNominalTypeExtensionContext();
446450
}
@@ -451,9 +455,9 @@ Type TypeChecker::resolveTypeInContext(
451455

452456
// If we found a generic parameter, map to the archetype if there is one.
453457
if (auto genericParam = dyn_cast<GenericTypeParamDecl>(typeDecl)) {
454-
return resolver->resolveGenericTypeParamType(
455-
genericParam->getDeclaredInterfaceType()
456-
->castTo<GenericTypeParamType>());
458+
assert(!selfType);
459+
return resolver->mapTypeIntoContext(
460+
genericParam->getDeclaredInterfaceType());
457461
}
458462

459463
// Simple case -- the type is not nested inside of another type.
@@ -467,7 +471,8 @@ Type TypeChecker::resolveTypeInContext(
467471
return aliasDecl->getUnboundGenericType();
468472

469473
// Otherwise, simply return the underlying type.
470-
return resolver->resolveTypeOfDecl(aliasDecl);
474+
return resolver->mapTypeIntoContext(
475+
aliasDecl->getDeclaredInterfaceType());
471476
}
472477

473478
// When a nominal type used outside its context, return the unbound
@@ -860,7 +865,8 @@ static Type diagnoseUnknownType(TypeChecker &tc, DeclContext *dc,
860865

861866
// Retrieve the nominal type and resolve it within this context.
862867
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");
863-
auto type = resolver->resolveTypeOfContext(dc->getInnermostTypeContext());
868+
auto type = resolver->mapTypeIntoContext(
869+
dc->getInnermostTypeContext()->getSelfInterfaceType());
864870
if (type->hasError())
865871
return type;
866872

@@ -1155,7 +1161,8 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
11551161
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
11561162
// while the 'Self' type is more than just a reference to a TypeDecl.
11571163

1158-
auto selfType = resolver->resolveTypeOfContext(func->getDeclContext());
1164+
auto selfType = resolver->mapTypeIntoContext(
1165+
func->getDeclContext()->getSelfInterfaceType());
11591166
return DynamicSelfType::get(selfType, TC.Context);
11601167
}
11611168

validation-test/IDE/crashers_2/0005-should-have-found-non-type-context-by-now.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// RUN: %target-swift-ide-test -code-completion -code-completion-token=B1 -source-filename=%s
33
// RUN: %target-swift-ide-test -code-completion -code-completion-token=C1 -source-filename=%s
44

5-
// RUN: not --crash %target-swift-ide-test -code-completion -code-completion-token=A2 -source-filename=%s
6-
// RUN: not --crash %target-swift-ide-test -code-completion -code-completion-token=B2 -source-filename=%s
7-
// RUN: not --crash %target-swift-ide-test -code-completion -code-completion-token=C2 -source-filename=%s
5+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=A2 -source-filename=%s
6+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=B2 -source-filename=%s
7+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=C2 -source-filename=%s
88

99
// RUN: %target-swift-ide-test -code-completion -code-completion-token=A3 -source-filename=%s
1010
// RUN: not --crash %target-swift-ide-test -code-completion -code-completion-token=B3 -source-filename=%s
11+
1112
// REQUIRES: asserts
1213

1314
class a1<b#^A1^#> {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
protocol P{typealias e:P.e
1010
protocol P{typealias e:Self

0 commit comments

Comments
 (0)