@@ -1053,31 +1053,6 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
1053
1053
return { valueType, valueType };
1054
1054
}
1055
1055
1056
- static Type getInnermostConformingType (TypeChecker &TC, DeclContext *DC,
1057
- ProtocolDecl *protocol) {
1058
- do {
1059
- if (DC->isTypeContext ()) {
1060
- if (protocol == DC->getAsProtocolOrProtocolExtensionContext ())
1061
- return DC->mapTypeIntoContext (DC->getProtocolSelfType ());
1062
-
1063
- auto *NTD = DC->getAsNominalTypeOrNominalTypeExtensionContext ();
1064
- auto type = NTD->getDeclaredType ();
1065
-
1066
- ConformanceCheckOptions options;
1067
- options |= ConformanceCheckFlags::InExpression;
1068
- options |= ConformanceCheckFlags::SuppressDependencyTracking;
1069
- options |= ConformanceCheckFlags::SkipConditionalRequirements;
1070
-
1071
- auto result =
1072
- TC.conformsToProtocol (type, protocol, NTD->getDeclContext (), options);
1073
-
1074
- if (result)
1075
- return DC->getDeclaredTypeInContext ();
1076
- }
1077
- } while ((DC = DC->getParent ()));
1078
-
1079
- return nullptr ;
1080
- }
1081
1056
// / Bind type variables for archetypes that are determined from
1082
1057
// / context.
1083
1058
// /
@@ -1093,10 +1068,10 @@ static Type getInnermostConformingType(TypeChecker &TC, DeclContext *DC,
1093
1068
// /
1094
1069
// / A final case we have to handle, even though it is invalid, is
1095
1070
// / when a type is nested inside another protocol. We bind the
1096
- // / protocol type variable for the protocol Self to its archetype
1097
- // / in protocol context . This of course makes no sense, but we
1098
- // / can't leave the type variable dangling, because then we crash
1099
- // / later.
1071
+ // / protocol type variable for the protocol Self to an unresolved
1072
+ // / type, since it will conform to anything . This of course makes
1073
+ // / no sense, but we can't leave the type variable dangling,
1074
+ // / because then we crash later.
1100
1075
// /
1101
1076
// / If we ever do want to allow nominal types to be nested inside
1102
1077
// / protocols, the key is to set their declared type to a
@@ -1117,43 +1092,43 @@ static void bindArchetypesFromContext(
1117
1092
1118
1093
auto *genericEnv = cs.DC ->getGenericEnvironmentOfContext ();
1119
1094
1095
+ auto bindContextArchetype = [&](Type paramTy, Type contextTy) {
1096
+ auto found = replacements.find (cast<GenericTypeParamType>(
1097
+ paramTy->getCanonicalType ()));
1098
+
1099
+ // We might not have a type variable for this generic parameter
1100
+ // because either we're opening up an UnboundGenericType,
1101
+ // in which case we only want to infer the innermost generic
1102
+ // parameters, or because this generic parameter was constrained
1103
+ // away into a concrete type.
1104
+ if (found != replacements.end ()) {
1105
+ auto typeVar = found->second ;
1106
+ cs.addConstraint (ConstraintKind::Bind, typeVar, contextTy,
1107
+ locatorPtr);
1108
+ }
1109
+ };
1110
+
1111
+ // Find the innermost non-type context.
1120
1112
for (const auto *parentDC = outerDC;
1121
1113
!parentDC->isModuleScopeContext ();
1122
1114
parentDC = parentDC->getParent ()) {
1123
- if (parentDC->isTypeContext () &&
1124
- (parentDC == outerDC ||
1125
- !parentDC->getAsProtocolOrProtocolExtensionContext ()))
1115
+ if (parentDC->isTypeContext ()) {
1116
+ if (parentDC != outerDC && parentDC->getAsProtocolOrProtocolExtensionContext ()) {
1117
+ auto selfTy = parentDC->getSelfInterfaceType ();
1118
+ auto contextTy = cs.TC .Context .TheUnresolvedType ;
1119
+ bindContextArchetype (selfTy, contextTy);
1120
+ }
1126
1121
continue ;
1122
+ }
1127
1123
1124
+ // If it's not generic, there's nothing to do.
1128
1125
auto *genericSig = parentDC->getGenericSignatureOfContext ();
1129
1126
if (!genericSig)
1130
1127
break ;
1131
1128
1132
1129
for (auto *paramTy : genericSig->getGenericParams ()) {
1133
- auto found = replacements.find (cast<GenericTypeParamType>(
1134
- paramTy->getCanonicalType ()));
1135
-
1136
- // We might not have a type variable for this generic parameter
1137
- // because either we're opening up an UnboundGenericType,
1138
- // in which case we only want to infer the innermost generic
1139
- // parameters, or because this generic parameter was constrained
1140
- // away into a concrete type.
1141
- if (found != replacements.end ()) {
1142
- Type contextTy;
1143
-
1144
- if (genericEnv) {
1145
- contextTy = genericEnv->mapTypeIntoContext (paramTy);
1146
- } else {
1147
- auto *protocol = parentDC->getAsProtocolOrProtocolExtensionContext ();
1148
- contextTy = getInnermostConformingType (cs.TC , cs.DC , protocol);
1149
- }
1150
-
1151
- assert (contextTy);
1152
-
1153
- auto typeVar = found->second ;
1154
- cs.addConstraint (ConstraintKind::Bind, typeVar, contextTy,
1155
- locatorPtr);
1156
- }
1130
+ Type contextTy = genericEnv->mapTypeIntoContext (paramTy);
1131
+ bindContextArchetype (paramTy, contextTy);
1157
1132
}
1158
1133
1159
1134
break ;
0 commit comments