@@ -1275,6 +1275,20 @@ static bool isSubclass(const Metadata *subclass, const Metadata *superclass) {
1275
1275
});
1276
1276
}
1277
1277
1278
+ static bool isSubclassOrExistential (const Metadata *subclass,
1279
+ const Metadata *superclass) {
1280
+ // If the type which is constrained to a base class is an existential
1281
+ // type, and if that existential type includes a superclass constraint,
1282
+ // just require that the superclass by which the existential is
1283
+ // constrained is a subclass of the base class.
1284
+ if (auto *existential = dyn_cast<ExistentialTypeMetadata>(subclass)) {
1285
+ if (auto *superclassConstraint = existential->getSuperclassConstraint ())
1286
+ subclass = superclassConstraint;
1287
+ }
1288
+
1289
+ return isSubclass (subclass, superclass);
1290
+ }
1291
+
1278
1292
SWIFT_CC (swift)
1279
1293
SWIFT_RUNTIME_STDLIB_SPI
1280
1294
bool swift::_swift_class_isSubclass (const Metadata *subclass,
@@ -1367,16 +1381,7 @@ checkGenericRequirement(const GenericRequirementDescriptor &req,
1367
1381
return *result.getError ();
1368
1382
auto baseType = result.getType ().getMetadata ();
1369
1383
1370
- // If the type which is constrained to a base class is an existential
1371
- // type, and if that existential type includes a superclass constraint,
1372
- // just require that the superclass by which the existential is
1373
- // constrained is a subclass of the base class.
1374
- if (auto *existential = dyn_cast<ExistentialTypeMetadata>(subjectType)) {
1375
- if (auto *superclassConstraint = existential->getSuperclassConstraint ())
1376
- subjectType = superclassConstraint;
1377
- }
1378
-
1379
- if (!isSubclass (subjectType, baseType))
1384
+ if (!isSubclassOrExistential (subjectType, baseType))
1380
1385
return TYPE_LOOKUP_ERROR_FMT (
1381
1386
" %.*s is not subclass of %.*s" , (int )req.getParam ().size (),
1382
1387
req.getParam ().data (), (int )req.getMangledTypeName ().size (),
@@ -1464,7 +1469,26 @@ checkGenericPackRequirement(const GenericRequirementDescriptor &req,
1464
1469
}
1465
1470
1466
1471
case GenericRequirementKind::BaseClass: {
1467
- llvm_unreachable (" Implement me" );
1472
+ // Demangle the base type under the given substitutions.
1473
+ auto result = swift_getTypeByMangledName (
1474
+ MetadataState::Abstract, req.getMangledTypeName (),
1475
+ extraArguments.data (), substGenericParam, substWitnessTable);
1476
+ if (result.getError ())
1477
+ return *result.getError ();
1478
+ auto baseType = result.getType ().getMetadata ();
1479
+
1480
+ // Check that each pack element inherits from the base class.
1481
+ for (unsigned i = 0 , e = subjectType.getNumElements (); i < e; ++i) {
1482
+ const Metadata *elt = subjectType.getElements ()[i];
1483
+
1484
+ if (!isSubclassOrExistential (elt, baseType))
1485
+ return TYPE_LOOKUP_ERROR_FMT (
1486
+ " %.*s is not subclass of %.*s" , (int )req.getParam ().size (),
1487
+ req.getParam ().data (), (int )req.getMangledTypeName ().size (),
1488
+ req.getMangledTypeName ().data ());
1489
+ }
1490
+
1491
+ return llvm::None;
1468
1492
}
1469
1493
1470
1494
case GenericRequirementKind::SameConformance: {
0 commit comments