Skip to content

Commit 5ac51b9

Browse files
committed
IDE: Refactor replaceArchetypesWithTypeVariables() to handle member types
1 parent db6d3e0 commit 5ac51b9

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/ConformanceLookup.h"
2525
#include "swift/AST/DiagnosticSuppression.h"
2626
#include "swift/AST/ExistentialLayout.h"
27+
#include "swift/AST/GenericEnvironment.h"
2728
#include "swift/AST/Initializer.h"
2829
#include "swift/AST/PrettyStackTrace.h"
2930
#include "swift/AST/SubstitutionMap.h"
@@ -996,44 +997,65 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
996997
return false;
997998
}
998999

1000+
static Type openTypeParameter(ConstraintSystem &cs,
1001+
Type interfaceTy,
1002+
GenericEnvironment *env,
1003+
llvm::DenseMap<SubstitutableType *, TypeVariableType *> &types) {
1004+
if (auto *memberTy = interfaceTy->getAs<DependentMemberType>()) {
1005+
return DependentMemberType::get(
1006+
openTypeParameter(cs, memberTy->getBase(), env, types),
1007+
memberTy->getAssocType());
1008+
}
1009+
1010+
auto *paramTy = interfaceTy->castTo<GenericTypeParamType>();
1011+
auto archetypeTy = env->mapTypeIntoContext(paramTy)->castTo<ArchetypeType>();
1012+
1013+
auto found = types.find(archetypeTy);
1014+
if (found != types.end())
1015+
return found->second;
1016+
1017+
auto locator = cs.getConstraintLocator({});
1018+
auto replacement = cs.createTypeVariable(locator,
1019+
TVO_CanBindToNoEscape);
1020+
1021+
// FIXME: This doesn't capture all requirements imposed on the archetype!
1022+
// We really need to be opening the generic signature instead.
1023+
if (auto superclass = archetypeTy->getSuperclass()) {
1024+
cs.addConstraint(ConstraintKind::Subtype, replacement,
1025+
superclass, locator);
1026+
}
1027+
for (auto proto : archetypeTy->getConformsTo()) {
1028+
cs.addConstraint(ConstraintKind::ConformsTo, replacement,
1029+
proto->getDeclaredInterfaceType(), locator);
1030+
}
1031+
1032+
types[archetypeTy] = replacement;
1033+
return replacement;
1034+
}
1035+
9991036
static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
10001037
Type t) {
10011038
llvm::DenseMap<SubstitutableType *, TypeVariableType *> types;
10021039

10031040
return t.subst(
10041041
[&](SubstitutableType *origType) -> Type {
1005-
auto found = types.find(origType);
1006-
if (found != types.end())
1007-
return found->second;
1008-
1009-
if (auto archetypeType = dyn_cast<ArchetypeType>(origType)) {
1010-
// For nested types, fail here so the default logic in subst()
1011-
// for nested types applies.
1012-
if (!archetypeType->getInterfaceType()->is<GenericTypeParamType>())
1013-
return Type();
1014-
1015-
auto locator = cs.getConstraintLocator({});
1016-
auto replacement = cs.createTypeVariable(locator,
1017-
TVO_CanBindToNoEscape);
1018-
1019-
if (auto superclass = archetypeType->getSuperclass()) {
1020-
cs.addConstraint(ConstraintKind::Subtype, replacement,
1021-
superclass, locator);
1022-
}
1023-
for (auto proto : archetypeType->getConformsTo()) {
1024-
cs.addConstraint(ConstraintKind::ConformsTo, replacement,
1025-
proto->getDeclaredInterfaceType(), locator);
1026-
}
1027-
types[origType] = replacement;
1028-
return replacement;
1042+
if (auto archetypeTy = dyn_cast<ArchetypeType>(origType)) {
1043+
return openTypeParameter(cs,
1044+
archetypeTy->getInterfaceType(),
1045+
archetypeTy->getGenericEnvironment(),
1046+
types);
10291047
}
10301048

10311049
// FIXME: Remove this case
1032-
assert(cast<GenericTypeParamType>(origType));
1050+
auto *paramTy = cast<GenericTypeParamType>(origType);
1051+
auto found = types.find(paramTy);
1052+
if (found != types.end())
1053+
return found->second;
1054+
10331055
auto locator = cs.getConstraintLocator({});
10341056
auto replacement = cs.createTypeVariable(locator,
10351057
TVO_CanBindToNoEscape);
1036-
types[origType] = replacement;
1058+
types[paramTy] = replacement;
10371059
return replacement;
10381060
},
10391061
MakeAbstractConformanceForGenericType(),

0 commit comments

Comments
 (0)