@@ -486,6 +486,32 @@ Type ConstraintSystem::openUnboundGenericType(
486
486
ConstraintLocatorBuilder locator) {
487
487
assert (!type->hasTypeParameter ());
488
488
489
+ // If this is a generic typealias defined inside of constrainted extension,
490
+ // let's add all of the generic requirements to the constraint system to
491
+ // make sure that it's something we can use.
492
+ if (auto *NAT = dyn_cast<NameAliasType>(type.getPointer ())) {
493
+ auto *decl = NAT->getDecl ();
494
+ auto *extension = dyn_cast<ExtensionDecl>(decl->getDeclContext ());
495
+ auto parentTy = NAT->getParent ();
496
+
497
+ if (parentTy && extension && extension->isConstrainedExtension ()) {
498
+ auto subMap = NAT->getSubstitutionMap ();
499
+ auto contextSubMap = parentTy->getContextSubstitutionMap (
500
+ extension->getParentModule (),
501
+ extension->getAsNominalTypeOrNominalTypeExtensionContext ());
502
+
503
+ if (auto *signature = NAT->getGenericSignature ()) {
504
+ openGenericRequirements (
505
+ extension, signature, /* skipProtocolSelfConstraint*/ true , locator,
506
+ [&](Type type) {
507
+ return type.subst (QuerySubstitutionMap{contextSubMap},
508
+ LookUpConformanceInSubstitutionMap (subMap),
509
+ SubstFlags::UseErrorType);
510
+ });
511
+ }
512
+ }
513
+ }
514
+
489
515
if (!type->hasUnboundGenericType ())
490
516
return type;
491
517
@@ -1133,41 +1159,48 @@ void ConstraintSystem::openGeneric(
1133
1159
bindArchetypesFromContext (*this , outerDC, locatorPtr, replacements);
1134
1160
1135
1161
// Add the requirements as constraints.
1136
- auto requirements = sig->getRequirements ();
1162
+ openGenericRequirements (
1163
+ outerDC, sig, skipProtocolSelfConstraint, locator,
1164
+ [&](Type type) { return openType (type, replacements); });
1165
+ }
1166
+
1167
+ void ConstraintSystem::openGenericRequirements (
1168
+ DeclContext *outerDC, GenericSignature *signature,
1169
+ bool skipProtocolSelfConstraint, ConstraintLocatorBuilder locator,
1170
+ llvm::function_ref<Type(Type)> substFn) {
1171
+ auto requirements = signature->getRequirements ();
1137
1172
for (unsigned pos = 0 , n = requirements.size (); pos != n; ++pos) {
1138
1173
const auto &req = requirements[pos];
1139
1174
1140
- Optional<Requirement> openedReq;
1141
- auto openedFirst = openType (req.getFirstType (), replacements);
1175
+ auto firstType = substFn (req.getFirstType ());
1142
1176
1143
1177
auto kind = req.getKind ();
1178
+ auto requirementLoc =
1179
+ locator.withPathElement (ConstraintLocator::OpenedGeneric)
1180
+ .withPathElement (LocatorPathElt::getTypeRequirementComponent (pos));
1181
+
1144
1182
switch (kind) {
1145
1183
case RequirementKind::Conformance: {
1146
1184
auto proto = req.getSecondType ()->castTo <ProtocolType>();
1147
1185
auto protoDecl = proto->getDecl ();
1148
1186
// Determine whether this is the protocol 'Self' constraint we should
1149
1187
// skip.
1150
- if (skipProtocolSelfConstraint &&
1151
- protoDecl == outerDC &&
1188
+ if (skipProtocolSelfConstraint && protoDecl == outerDC &&
1152
1189
protoDecl->getSelfInterfaceType ()->isEqual (req.getFirstType ()))
1153
1190
continue ;
1154
- openedReq = Requirement (kind, openedFirst , proto);
1191
+ addConstraint ( Requirement (kind, firstType , proto), requirementLoc );
1155
1192
break ;
1156
1193
}
1157
1194
case RequirementKind::Superclass:
1158
1195
case RequirementKind::SameType:
1159
- openedReq = Requirement (kind, openedFirst ,
1160
- openType (req. getSecondType (), replacements) );
1196
+ addConstraint ( Requirement (kind, firstType, substFn (req. getSecondType ())) ,
1197
+ requirementLoc );
1161
1198
break ;
1162
1199
case RequirementKind::Layout:
1163
- openedReq = Requirement (kind, openedFirst, req.getLayoutConstraint ());
1200
+ addConstraint (Requirement (kind, firstType, req.getLayoutConstraint ()),
1201
+ requirementLoc);
1164
1202
break ;
1165
1203
}
1166
-
1167
- addConstraint (
1168
- *openedReq,
1169
- locator.withPathElement (ConstraintLocator::OpenedGeneric)
1170
- .withPathElement (LocatorPathElt::getTypeRequirementComponent (pos)));
1171
1204
}
1172
1205
}
1173
1206
@@ -1246,7 +1279,6 @@ ConstraintSystem::getTypeOfMemberReference(
1246
1279
1247
1280
auto memberTy = TC.substMemberTypeWithBase (DC->getParentModule (),
1248
1281
typeDecl, baseObjTy);
1249
-
1250
1282
// Open the type if it was a reference to a generic type.
1251
1283
memberTy = openUnboundGenericType (memberTy, locator);
1252
1284
0 commit comments