@@ -1602,7 +1602,10 @@ bool TypeBase::isSpelledLike(Type other) {
1602
1602
}
1603
1603
1604
1604
Type TypeBase::getSuperclass (LazyResolver *resolver) {
1605
+ // If this type is either a bound generic type, or a nested type inside a
1606
+ // bound generic type, we will need to fish out the generic parameters.
1605
1607
Type specializedTy;
1608
+
1606
1609
ClassDecl *classDecl;
1607
1610
if (auto classTy = getAs<ClassType>()) {
1608
1611
classDecl = classTy->getDecl ();
@@ -1622,19 +1625,31 @@ Type TypeBase::getSuperclass(LazyResolver *resolver) {
1622
1625
return nullptr ;
1623
1626
}
1624
1627
1628
+ // Get the superclass type. If the class is generic, the superclass type may
1629
+ // contain generic type parameters from the signature of the class.
1625
1630
Type superclassTy;
1626
1631
if (classDecl)
1627
1632
superclassTy = classDecl->getSuperclass ();
1628
1633
1629
- if (!specializedTy || !superclassTy)
1634
+ // If there's no superclass, return a null type. If the class is not in a
1635
+ // generic context, return the original superclass type.
1636
+ if (!superclassTy || !classDecl->isGenericContext ())
1630
1637
return superclassTy;
1631
1638
1639
+ // The class is defined in a generic context, so its superclass type may refer
1640
+ // to generic parameters of the class or some parent type of the class. Map
1641
+ // it to a contextual type.
1642
+
1632
1643
// FIXME: Lame to rely on archetypes in the substitution below.
1633
1644
superclassTy = ArchetypeBuilder::mapTypeIntoContext (classDecl, superclassTy);
1634
1645
1635
- // If the type is specialized, we need to gather all of the substitutions.
1636
- // We've already dealt with the top level, but continue gathering
1637
- // specializations from the parent types.
1646
+ // If the type does not bind any generic parameters, return the superclass
1647
+ // type as-is.
1648
+ if (!specializedTy)
1649
+ return superclassTy;
1650
+
1651
+ // If the type is specialized, we need to gather all of the substitutions
1652
+ // for the type and any parent types.
1638
1653
TypeSubstitutionMap substitutions;
1639
1654
while (specializedTy) {
1640
1655
if (auto nominalTy = specializedTy->getAs <NominalType>()) {
@@ -1653,7 +1668,8 @@ Type TypeBase::getSuperclass(LazyResolver *resolver) {
1653
1668
specializedTy = boundTy->getParent ();
1654
1669
}
1655
1670
1656
- // Perform substitutions into the base type.
1671
+ // Perform substitutions into the superclass type to yield the
1672
+ // substituted superclass type.
1657
1673
Module *module = classDecl->getModuleContext ();
1658
1674
return superclassTy.subst (module , substitutions, None);
1659
1675
}
0 commit comments