Skip to content

Commit c97221f

Browse files
committed
[NFC] TypeResolution: Nuke 'forContextual'
1 parent d7649a0 commit c97221f

File tree

2 files changed

+29
-100
lines changed

2 files changed

+29
-100
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 29 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,6 @@ TypeResolution::forInterface(DeclContext *dc, GenericEnvironment *genericEnv,
8787
return result;
8888
}
8989

90-
TypeResolution
91-
TypeResolution::forContextual(DeclContext *dc, TypeResolutionOptions options,
92-
OpenUnboundGenericTypeFn unboundTyOpener,
93-
HandlePlaceholderTypeReprFn placeholderHandler) {
94-
return forContextual(dc, dc->getGenericEnvironmentOfContext(), options,
95-
unboundTyOpener, placeholderHandler);
96-
}
97-
98-
TypeResolution
99-
TypeResolution::forContextual(DeclContext *dc, GenericEnvironment *genericEnv,
100-
TypeResolutionOptions options,
101-
OpenUnboundGenericTypeFn unboundTyOpener,
102-
HandlePlaceholderTypeReprFn placeholderHandler) {
103-
TypeResolution result(dc, TypeResolutionStage::Contextual, options,
104-
unboundTyOpener, placeholderHandler);
105-
result.genericEnv = genericEnv;
106-
return result;
107-
}
108-
10990
TypeResolution TypeResolution::withOptions(TypeResolutionOptions opts) const {
11091
TypeResolution result(dc, stage, opts, unboundTyOpener, placeholderHandler);
11192
result.genericEnv = genericEnv;
@@ -127,30 +108,6 @@ GenericSignature TypeResolution::getGenericSignature() const {
127108
return dc->getGenericSignatureOfContext();
128109
}
129110

130-
bool TypeResolution::usesArchetypes() const {
131-
switch (stage) {
132-
case TypeResolutionStage::Structural:
133-
case TypeResolutionStage::Interface:
134-
return false;
135-
136-
case TypeResolutionStage::Contextual:
137-
return true;
138-
}
139-
llvm_unreachable("unhandled stage");
140-
}
141-
142-
Type TypeResolution::mapTypeIntoContext(Type type) const {
143-
switch (stage) {
144-
case TypeResolutionStage::Structural:
145-
case TypeResolutionStage::Interface:
146-
return type;
147-
148-
case TypeResolutionStage::Contextual:
149-
return GenericEnvironment::mapTypeIntoContext(genericEnv, type);
150-
}
151-
llvm_unreachable("unhandled stage");
152-
}
153-
154111
// FIXME: It would be nice to have a 'DescriptiveTypeKind' abstraction instead.
155112
static DescriptiveDeclKind describeDeclOfType(Type t) {
156113
if (!t) {
@@ -174,12 +131,12 @@ Type TypeResolution::resolveDependentMemberType(
174131
case TypeResolutionStage::Structural:
175132
return DependentMemberType::get(baseTy, refIdentifier);
176133

177-
case TypeResolutionStage::Contextual:
178-
llvm_unreachable("Dependent type after archetype substitution");
179-
180134
case TypeResolutionStage::Interface:
181135
// Handled below.
182136
break;
137+
138+
case TypeResolutionStage::Contextual:
139+
llvm_unreachable("Use TypeResolution::resolveContextualType instead");
183140
}
184141

185142
assert(stage == TypeResolutionStage::Interface);
@@ -273,12 +230,12 @@ Type TypeResolution::resolveSelfAssociatedType(Type baseTy,
273230
case TypeResolutionStage::Structural:
274231
return DependentMemberType::get(baseTy, name);
275232

276-
case TypeResolutionStage::Contextual:
277-
llvm_unreachable("Dependent type after archetype substitution");
278-
279233
case TypeResolutionStage::Interface:
280234
// Handled below.
281235
break;
236+
237+
case TypeResolutionStage::Contextual:
238+
llvm_unreachable("Use TypeResolution::resolveContextualType instead");
282239
}
283240

284241
assert(stage == TypeResolutionStage::Interface);
@@ -327,9 +284,7 @@ bool TypeResolution::areSameType(Type type1, Type type2) const {
327284
break;
328285

329286
case TypeResolutionStage::Contextual:
330-
// Contextual types have already been uniqued, so the isEqual() result
331-
// above is complete.
332-
return false;
287+
llvm_unreachable("Use TypeResolution::resolveContextualType instead");
333288
}
334289

335290
if (stage == TypeResolutionStage::Interface) {
@@ -395,7 +350,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
395350

396351
// If we found a generic parameter, map to the archetype if there is one.
397352
if (auto genericParam = dyn_cast<GenericTypeParamDecl>(typeDecl)) {
398-
return mapTypeIntoContext(genericParam->getDeclaredInterfaceType());
353+
return genericParam->getDeclaredInterfaceType();
399354
}
400355

401356
if (!isSpecialized) {
@@ -407,13 +362,12 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
407362
parentDC = parentDC->getParentForLookup()) {
408363
auto *parentNominal = parentDC->getSelfNominalTypeDecl();
409364
if (parentNominal == nominalType)
410-
return mapTypeIntoContext(parentDC->getDeclaredInterfaceType());
365+
return parentDC->getDeclaredInterfaceType();
411366
if (isa<ExtensionDecl>(parentDC)) {
412367
auto *extendedType = parentNominal;
413368
while (extendedType != nullptr) {
414369
if (extendedType == nominalType)
415-
return mapTypeIntoContext(
416-
extendedType->getDeclaredInterfaceType());
370+
return extendedType->getDeclaredInterfaceType();
417371
extendedType = extendedType->getParent()->getSelfNominalTypeDecl();
418372
}
419373
}
@@ -436,8 +390,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
436390
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
437391
return aliasDecl->getStructuralType();
438392
}
439-
return mapTypeIntoContext(
440-
aliasDecl->getDeclaredInterfaceType());
393+
return aliasDecl->getDeclaredInterfaceType();
441394
}
442395

443396
extendedType = unboundGeneric->getParent();
@@ -451,7 +404,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
451404
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
452405
return aliasDecl->getStructuralType();
453406
}
454-
return mapTypeIntoContext(aliasDecl->getDeclaredInterfaceType());
407+
return aliasDecl->getDeclaredInterfaceType();
455408
}
456409
extendedType = aliasType->getParent();
457410
continue;
@@ -476,7 +429,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
476429
aliasDecl->getUnderlyingTypeRepr() != nullptr) {
477430
return aliasDecl->getStructuralType();
478431
}
479-
return mapTypeIntoContext(aliasDecl->getDeclaredInterfaceType());
432+
return aliasDecl->getDeclaredInterfaceType();
480433
}
481434

482435
// When a nominal type used outside its context, return the unbound
@@ -503,11 +456,11 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
503456
if (!foundDC->getDeclaredInterfaceType())
504457
return ErrorType::get(ctx);
505458

506-
selfType = mapTypeIntoContext(foundDC->getDeclaredInterfaceType());
459+
selfType = foundDC->getDeclaredInterfaceType();
507460
} else {
508461
// Otherwise, we want the protocol 'Self' type for
509462
// substituting into alias types and associated types.
510-
selfType = mapTypeIntoContext(foundDC->getSelfInterfaceType());
463+
selfType = foundDC->getSelfInterfaceType();
511464

512465
if (selfType->is<GenericTypeParamType>()) {
513466
if (typeDecl->getDeclContext()->getSelfProtocolDecl()) {
@@ -551,7 +504,7 @@ Type TypeResolution::resolveTypeInContext(TypeDecl *typeDecl,
551504

552505
// Finally, substitute the base type into the member type.
553506
return TypeChecker::substMemberTypeWithBase(
554-
fromDC->getParentModule(), typeDecl, selfType, usesArchetypes());
507+
fromDC->getParentModule(), typeDecl, selfType, /*useArchetypes=*/false);
555508
}
556509

557510
static TypeResolutionOptions
@@ -880,16 +833,13 @@ Type TypeResolution::applyUnboundGenericArguments(
880833
} else if (auto genericSig =
881834
decl->getDeclContext()->getGenericSignatureOfContext()) {
882835
for (auto gp : genericSig.getGenericParams()) {
883-
subs[gp->getCanonicalType()->castTo<GenericTypeParamType>()] =
884-
(usesArchetypes()
885-
? genericSig.getGenericEnvironment()->mapTypeIntoContext(gp)
886-
: gp);
836+
subs[gp->getCanonicalType()->castTo<GenericTypeParamType>()] = gp;
887837
}
888838
}
889839

890840
// Realize the types of the generic arguments and add them to the
891841
// substitution map.
892-
for (unsigned i = 0, e = genericArgs.size(); i < e; ++i) {
842+
for (const unsigned i : indices(genericArgs)) {
893843
auto origTy = genericSig.getInnermostGenericParams()[i];
894844
auto substTy = genericArgs[i];
895845

@@ -906,13 +856,15 @@ Type TypeResolution::applyUnboundGenericArguments(
906856
auto *module = getDeclContext()->getParentModule();
907857

908858
if (!skipRequirementsCheck && getStage() > TypeResolutionStage::Structural) {
859+
// Check the generic arguments against the requirements of the declaration's
860+
// generic signature.
861+
862+
// First, map substitutions into context.
909863
TypeSubstitutionMap contextualSubs = subs;
910-
if (getStage() == TypeResolutionStage::Interface) {
911-
if (const auto contextSig = getGenericSignature()) {
912-
auto *genericEnv = contextSig.getGenericEnvironment();
913-
for (auto &pair : contextualSubs) {
914-
pair.second = genericEnv->mapTypeIntoContext(pair.second);
915-
}
864+
if (const auto contextSig = getGenericSignature()) {
865+
auto *genericEnv = contextSig.getGenericEnvironment();
866+
for (auto &pair : contextualSubs) {
867+
pair.second = genericEnv->mapTypeIntoContext(pair.second);
916868
}
917869
}
918870

@@ -1121,12 +1073,10 @@ static Type diagnoseUnknownType(TypeResolution resolution,
11211073
diags.diagnose(comp->getNameLoc(), diag::dynamic_self_invalid, name)
11221074
.fixItReplace(comp->getNameLoc().getSourceRange(), name);
11231075

1124-
auto type = resolution.mapTypeIntoContext(
1125-
dc->getInnermostTypeContext()->getSelfInterfaceType());
1126-
11271076
comp->overwriteNameRef(DeclNameRef(nominal->getName()));
11281077
comp->setValue(nominal, nominalDC->getParent());
1129-
return type;
1078+
1079+
return dc->getInnermostTypeContext()->getSelfInterfaceType();
11301080
} else {
11311081
diags.diagnose(comp->getNameLoc(), diag::cannot_find_type_in_scope,
11321082
comp->getNameRef());
@@ -1438,8 +1388,7 @@ static Type resolveTopLevelIdentTypeComponent(TypeResolution resolution,
14381388
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
14391389
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
14401390
// while the 'Self' type is more than just a reference to a TypeDecl.
1441-
auto selfType = resolution.mapTypeIntoContext(
1442-
typeDC->getSelfInterfaceType());
1391+
auto selfType = typeDC->getSelfInterfaceType();
14431392

14441393
// Check if we can reference 'Self' here, and if so, what kind of Self it is.
14451394
auto selfTypeKind = getSelfTypeKind(DC, options);

lib/Sema/TypeCheckType.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,9 @@ class TypeResolution {
336336
OpenUnboundGenericTypeFn unboundTyOpener,
337337
HandlePlaceholderTypeReprFn placeholderHandler);
338338

339-
/// Form a type resolution for a contextual type, which is a complete
340-
/// description of the type using the archetypes of the given declaration
341-
/// context.
342-
static TypeResolution
343-
forContextual(DeclContext *dc, TypeResolutionOptions opts,
344-
OpenUnboundGenericTypeFn unboundTyOpener,
345-
HandlePlaceholderTypeReprFn placeholderHandler);
346-
347339
/// Form a type resolution for a contextual type, which is a complete
348340
/// description of the type using the archetypes of the given generic
349341
/// environment.
350-
static TypeResolution
351-
forContextual(DeclContext *dc, GenericEnvironment *genericEnv,
352-
TypeResolutionOptions opts,
353-
OpenUnboundGenericTypeFn unboundTyOpener,
354-
HandlePlaceholderTypeReprFn placeholderHandler);
355-
356342
static Type
357343
resolveContextualType(TypeRepr *TyR, DeclContext *dc,
358344
TypeResolutionOptions opts,
@@ -406,12 +392,6 @@ class TypeResolution {
406392
Type resolveType(TypeRepr *TyR,
407393
GenericParamList *silParams=nullptr) const;
408394

409-
/// Whether this type resolution uses archetypes (vs. generic parameters).
410-
bool usesArchetypes() const;
411-
412-
/// Map the given type (that involves generic parameters)
413-
Type mapTypeIntoContext(Type type) const;
414-
415395
/// Resolve a reference to a member type of the given (dependent) base and
416396
/// name.
417397
Type resolveDependentMemberType(Type baseTy, DeclContext *DC,

0 commit comments

Comments
 (0)