|
24 | 24 | #include "swift/AST/ConformanceLookup.h" |
25 | 25 | #include "swift/AST/DiagnosticSuppression.h" |
26 | 26 | #include "swift/AST/ExistentialLayout.h" |
| 27 | +#include "swift/AST/GenericEnvironment.h" |
27 | 28 | #include "swift/AST/Initializer.h" |
28 | 29 | #include "swift/AST/PrettyStackTrace.h" |
29 | 30 | #include "swift/AST/SubstitutionMap.h" |
@@ -996,44 +997,65 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC, |
996 | 997 | return false; |
997 | 998 | } |
998 | 999 |
|
| 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 | + |
999 | 1036 | static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs, |
1000 | 1037 | Type t) { |
1001 | 1038 | llvm::DenseMap<SubstitutableType *, TypeVariableType *> types; |
1002 | 1039 |
|
1003 | 1040 | return t.subst( |
1004 | 1041 | [&](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); |
1029 | 1047 | } |
1030 | 1048 |
|
1031 | 1049 | // 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 | + |
1033 | 1055 | auto locator = cs.getConstraintLocator({}); |
1034 | 1056 | auto replacement = cs.createTypeVariable(locator, |
1035 | 1057 | TVO_CanBindToNoEscape); |
1036 | | - types[origType] = replacement; |
| 1058 | + types[paramTy] = replacement; |
1037 | 1059 | return replacement; |
1038 | 1060 | }, |
1039 | 1061 | MakeAbstractConformanceForGenericType(), |
|
0 commit comments