Skip to content

Commit 319a7b4

Browse files
authored
Merge pull request #10277 from slavapestov/unqualified-lookup-cleanup
Unqualified lookup cleanup
2 parents a9841fc + aa84dc6 commit 319a7b4

12 files changed

+81
-224
lines changed

lib/Sema/GenericTypeResolver.h

Lines changed: 6 additions & 74 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,33 +52,6 @@ class GenericTypeResolver {
6052
SourceRange baseRange,
6153
ComponentIdentTypeRepr *ref) = 0;
6254

63-
/// Resolve an unqualified reference to an associated type of the 'Self' type
64-
/// of a protocol.
65-
///
66-
/// \param selfTy The base of the member access.
67-
/// \param assocType The associated type.
68-
///
69-
/// \returns A type that refers to the dependent member type, or an error
70-
/// type if such a reference is ill-formed.
71-
virtual Type resolveSelfAssociatedType(Type selfTy,
72-
AssociatedTypeDecl *assocType) = 0;
73-
74-
/// Resolve the self type within the given context.
75-
///
76-
/// \param dc A context in which type checking occurs, which must be a type
77-
/// context (i.e., nominal type or extension thereof).
78-
///
79-
/// \returns the type of context.
80-
virtual Type resolveTypeOfContext(DeclContext *dc) = 0;
81-
82-
/// Retrieve the type when referring to the given type declaration within
83-
/// its context.
84-
///
85-
/// \param decl A type declaration.
86-
///
87-
/// \returns the type of the declaration in context..
88-
virtual Type resolveTypeOfDecl(TypeDecl *decl) = 0;
89-
9055
/// Determine whether the given types are equivalent within the generic
9156
/// context.
9257
virtual bool areSameType(Type type1, Type type2) = 0;
@@ -101,20 +66,13 @@ class GenericTypeResolver {
10166
/// and only trivially resolves dependent member types.
10267
class DependentGenericTypeResolver : public GenericTypeResolver {
10368
public:
104-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
69+
virtual Type mapTypeIntoContext(Type type);
10570

10671
virtual Type resolveDependentMemberType(Type baseTy,
10772
DeclContext *DC,
10873
SourceRange baseRange,
10974
ComponentIdentTypeRepr *ref);
11075

111-
virtual Type resolveSelfAssociatedType(Type selfTy,
112-
AssociatedTypeDecl *assocType);
113-
114-
virtual Type resolveTypeOfContext(DeclContext *dc);
115-
116-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
117-
11876
virtual bool areSameType(Type type1, Type type2);
11977

12078
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -135,19 +93,12 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
13593
explicit GenericTypeToArchetypeResolver(DeclContext *dc)
13694
: GenericEnv(dc->getGenericEnvironmentOfContext()) { }
13795

138-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
96+
virtual Type mapTypeIntoContext(Type type);
13997

14098
virtual Type resolveDependentMemberType(Type baseTy, DeclContext *DC,
14199
SourceRange baseRange,
142100
ComponentIdentTypeRepr *ref);
143101

144-
virtual Type resolveSelfAssociatedType(Type selfTy,
145-
AssociatedTypeDecl *assocType);
146-
147-
virtual Type resolveTypeOfContext(DeclContext *dc);
148-
149-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
150-
151102
virtual bool areSameType(Type type1, Type type2);
152103

153104
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -159,25 +110,13 @@ class GenericTypeToArchetypeResolver : public GenericTypeResolver {
159110
/// This should only be used when resolving/validating where clauses in
160111
/// protocols.
161112
class ProtocolRequirementTypeResolver : public GenericTypeResolver {
162-
ProtocolDecl *Proto;
163-
164113
public:
165-
explicit ProtocolRequirementTypeResolver(ProtocolDecl *proto)
166-
: Proto(proto) {}
167-
168-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
114+
virtual Type mapTypeIntoContext(Type type);
169115

170116
virtual Type resolveDependentMemberType(Type baseTy, DeclContext *DC,
171117
SourceRange baseRange,
172118
ComponentIdentTypeRepr *ref);
173119

174-
virtual Type resolveSelfAssociatedType(Type selfTy,
175-
AssociatedTypeDecl *assocType);
176-
177-
virtual Type resolveTypeOfContext(DeclContext *dc);
178-
179-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
180-
181120
virtual bool areSameType(Type type1, Type type2);
182121

183122
virtual void recordParamType(ParamDecl *decl, Type ty);
@@ -200,20 +139,13 @@ class CompleteGenericTypeResolver : public GenericTypeResolver {
200139
ArrayRef<GenericTypeParamType *> genericParams)
201140
: TC(tc), Builder(builder), GenericParams(genericParams) { }
202141

203-
virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);
142+
virtual Type mapTypeIntoContext(Type type);
204143

205144
virtual Type resolveDependentMemberType(Type baseTy,
206145
DeclContext *DC,
207146
SourceRange baseRange,
208147
ComponentIdentTypeRepr *ref);
209148

210-
virtual Type resolveSelfAssociatedType(Type selfTy,
211-
AssociatedTypeDecl *assocType);
212-
213-
virtual Type resolveTypeOfContext(DeclContext *dc);
214-
215-
virtual Type resolveTypeOfDecl(TypeDecl *decl);
216-
217149
virtual bool areSameType(Type type1, Type type2);
218150

219151
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/TypeCheckConstraints.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,28 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
498498

499499
// FIXME: Need to refactor the way we build an AST node from a lookup result!
500500

501+
// If we have an unambiguous reference to a type decl, form a TypeExpr.
502+
if (Lookup.size() == 1 && UDRE->getRefKind() == DeclRefKind::Ordinary &&
503+
isa<TypeDecl>(Lookup[0].Decl)) {
504+
auto *D = cast<TypeDecl>(Lookup[0].Decl);
505+
// FIXME: This is odd.
506+
if (isa<ModuleDecl>(D)) {
507+
return new (Context) DeclRefExpr(D, UDRE->getNameLoc(),
508+
/*Implicit=*/false,
509+
AccessSemantics::Ordinary,
510+
D->getInterfaceType());
511+
}
512+
513+
return TypeExpr::createForDecl(Loc, D,
514+
UDRE->isImplicit());
515+
}
516+
501517
bool AllDeclRefs = true;
502518
SmallVector<ValueDecl*, 4> ResultValues;
503519
for (auto Result : Lookup) {
504520
// If we find a member, then all of the results aren't non-members.
505521
bool IsMember = Result.Base && !isa<ModuleDecl>(Result.Base);
506-
if (IsMember && !isa<TypeDecl>(Result.Decl)) {
522+
if (IsMember) {
507523
AllDeclRefs = false;
508524
break;
509525
}
@@ -532,21 +548,6 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
532548
if (matchesDeclRefKind(D, UDRE->getRefKind()))
533549
ResultValues.push_back(D);
534550
}
535-
536-
// If we have an unambiguous reference to a type decl, form a TypeExpr.
537-
if (ResultValues.size() == 1 && UDRE->getRefKind() == DeclRefKind::Ordinary &&
538-
isa<TypeDecl>(ResultValues[0])) {
539-
// FIXME: This is odd.
540-
if (isa<ModuleDecl>(ResultValues[0])) {
541-
return new (Context) DeclRefExpr(ResultValues[0], UDRE->getNameLoc(),
542-
/*Implicit=*/false,
543-
AccessSemantics::Ordinary,
544-
ResultValues[0]->getInterfaceType());
545-
}
546-
547-
return TypeExpr::createForDecl(Loc, cast<TypeDecl>(ResultValues[0]),
548-
UDRE->isImplicit());
549-
}
550551

551552
if (AllDeclRefs) {
552553
// Diagnose uses of operators that found no matching candidates.
@@ -607,7 +608,11 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
607608

608609
if (AllMemberRefs) {
609610
Expr *BaseExpr;
610-
if (auto NTD = dyn_cast<NominalTypeDecl>(Base)) {
611+
if (auto PD = dyn_cast<ProtocolDecl>(Base)) {
612+
BaseExpr = TypeExpr::createForDecl(Loc,
613+
PD->getGenericParams()->getParams().front(),
614+
/*isImplicit=*/true);
615+
} else if (auto NTD = dyn_cast<NominalTypeDecl>(Base)) {
611616
BaseExpr = TypeExpr::createForDecl(Loc, NTD, /*isImplicit=*/true);
612617
} else {
613618
BaseExpr = new (Context) DeclRefExpr(Base, UDRE->getNameLoc(),

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()) {
@@ -7520,9 +7520,9 @@ void TypeChecker::validateDeclForNameLookup(ValueDecl *D) {
75207520
return;
75217521

75227522
// Perform earlier validation of typealiases in protocols.
7523-
if (auto proto = dyn_cast<ProtocolDecl>(dc)) {
7523+
if (isa<ProtocolDecl>(dc)) {
75247524
if (!typealias->getGenericParams()) {
7525-
ProtocolRequirementTypeResolver resolver(proto);
7525+
ProtocolRequirementTypeResolver resolver;
75267526
TypeResolutionOptions options;
75277527

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

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 8 additions & 78 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,19 +41,6 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
4541
return DependentMemberType::get(baseTy, ref->getIdentifier());
4642
}
4743

48-
Type DependentGenericTypeResolver::resolveSelfAssociatedType(
49-
Type selfTy, AssociatedTypeDecl *assocType) {
50-
return DependentMemberType::get(selfTy, assocType);
51-
}
52-
53-
Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
54-
return dc->getSelfInterfaceType();
55-
}
56-
57-
Type DependentGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
58-
return decl->getDeclaredInterfaceType();
59-
}
60-
6144
bool DependentGenericTypeResolver::areSameType(Type type1, Type type2) {
6245
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
6346
return type1->isEqual(type2);
@@ -70,9 +53,8 @@ void DependentGenericTypeResolver::recordParamType(ParamDecl *decl, Type type) {
7053
// Do nothing
7154
}
7255

73-
Type GenericTypeToArchetypeResolver::resolveGenericTypeParamType(
74-
GenericTypeParamType *gp) {
75-
return GenericEnv->mapTypeIntoContext(gp);
56+
Type GenericTypeToArchetypeResolver::mapTypeIntoContext(Type type) {
57+
return GenericEnvironment::mapTypeIntoContext(GenericEnv, type);
7658
}
7759

7860
Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
@@ -83,21 +65,6 @@ Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
8365
llvm_unreachable("Dependent type after archetype substitution");
8466
}
8567

86-
Type GenericTypeToArchetypeResolver::resolveSelfAssociatedType(
87-
Type selfTy, AssociatedTypeDecl *assocType) {
88-
llvm_unreachable("Dependent type after archetype substitution");
89-
}
90-
91-
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc) {
92-
return GenericEnvironment::mapTypeIntoContext(
93-
GenericEnv, dc->getSelfInterfaceType());
94-
}
95-
96-
Type GenericTypeToArchetypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
97-
return GenericEnvironment::mapTypeIntoContext(
98-
GenericEnv, decl->getDeclaredInterfaceType());
99-
}
100-
10168
bool GenericTypeToArchetypeResolver::areSameType(Type type1, Type type2) {
10269
return type1->isEqual(type2);
10370
}
@@ -116,11 +83,8 @@ void GenericTypeToArchetypeResolver::recordParamType(ParamDecl *decl, Type type)
11683
GenericEnv, type));
11784
}
11885

119-
Type ProtocolRequirementTypeResolver::resolveGenericTypeParamType(
120-
GenericTypeParamType *gp) {
121-
assert(gp->isEqual(Proto->getSelfInterfaceType()) &&
122-
"found non-Self-shaped GTPT when resolving protocol requirement");
123-
return gp;
86+
Type ProtocolRequirementTypeResolver::mapTypeIntoContext(Type type) {
87+
return type;
12488
}
12589

12690
Type ProtocolRequirementTypeResolver::resolveDependentMemberType(
@@ -129,21 +93,6 @@ Type ProtocolRequirementTypeResolver::resolveDependentMemberType(
12993
return DependentMemberType::get(baseTy, ref->getIdentifier());
13094
}
13195

132-
Type ProtocolRequirementTypeResolver::resolveSelfAssociatedType(
133-
Type selfTy, AssociatedTypeDecl *assocType) {
134-
assert(selfTy->isEqual(Proto->getSelfInterfaceType()));
135-
(void)Proto;
136-
return assocType->getDeclaredInterfaceType();
137-
}
138-
139-
Type ProtocolRequirementTypeResolver::resolveTypeOfContext(DeclContext *dc) {
140-
return dc->getSelfInterfaceType();
141-
}
142-
143-
Type ProtocolRequirementTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
144-
return decl->getDeclaredInterfaceType();
145-
}
146-
14796
bool ProtocolRequirementTypeResolver::areSameType(Type type1, Type type2) {
14897
if (type1->isEqual(type2))
14998
return true;
@@ -167,13 +116,10 @@ void ProtocolRequirementTypeResolver::recordParamType(ParamDecl *decl,
167116
"recording a param type of a protocol requirement doesn't make sense");
168117
}
169118

170-
Type CompleteGenericTypeResolver::resolveGenericTypeParamType(
171-
GenericTypeParamType *gp) {
172-
assert(gp->getDecl() && "Missing generic parameter declaration");
173-
return gp;
119+
Type CompleteGenericTypeResolver::mapTypeIntoContext(Type type) {
120+
return type;
174121
}
175122

176-
177123
Type CompleteGenericTypeResolver::resolveDependentMemberType(
178124
Type baseTy,
179125
DeclContext *DC,
@@ -246,22 +192,6 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
246192
return ErrorType::get(TC.Context);
247193
}
248194

249-
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(
250-
Type selfTy, AssociatedTypeDecl *assocType) {
251-
return Builder.resolveArchetype(selfTy,
252-
ArchetypeResolutionKind::CompleteWellFormed)
253-
->getNestedType(assocType, Builder)
254-
->getDependentType(GenericParams, /*allowUnresolved=*/false);
255-
}
256-
257-
Type CompleteGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
258-
return dc->getSelfInterfaceType();
259-
}
260-
261-
Type CompleteGenericTypeResolver::resolveTypeOfDecl(TypeDecl *decl) {
262-
return decl->getDeclaredInterfaceType();
263-
}
264-
265195
bool CompleteGenericTypeResolver::areSameType(Type type1, Type type2) {
266196
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
267197
return type1->isEqual(type2);

0 commit comments

Comments
 (0)