Skip to content

Commit 46aef83

Browse files
committed
Sema: Fix TypeBase::getSuperclass() for types nested in generic functions
This isn't something we care about yet, but for some reason a subsequent patch caused some fixed compiler_crashers to regress and hit this. Tests are in the next commit.
1 parent 59a74b4 commit 46aef83

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

lib/AST/Type.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,10 @@ bool TypeBase::isSpelledLike(Type other) {
16021602
}
16031603

16041604
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.
16051607
Type specializedTy;
1608+
16061609
ClassDecl *classDecl;
16071610
if (auto classTy = getAs<ClassType>()) {
16081611
classDecl = classTy->getDecl();
@@ -1622,19 +1625,31 @@ Type TypeBase::getSuperclass(LazyResolver *resolver) {
16221625
return nullptr;
16231626
}
16241627

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.
16251630
Type superclassTy;
16261631
if (classDecl)
16271632
superclassTy = classDecl->getSuperclass();
16281633

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())
16301637
return superclassTy;
16311638

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+
16321643
// FIXME: Lame to rely on archetypes in the substitution below.
16331644
superclassTy = ArchetypeBuilder::mapTypeIntoContext(classDecl, superclassTy);
16341645

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.
16381653
TypeSubstitutionMap substitutions;
16391654
while (specializedTy) {
16401655
if (auto nominalTy = specializedTy->getAs<NominalType>()) {
@@ -1653,7 +1668,8 @@ Type TypeBase::getSuperclass(LazyResolver *resolver) {
16531668
specializedTy = boundTy->getParent();
16541669
}
16551670

1656-
// Perform substitutions into the base type.
1671+
// Perform substitutions into the superclass type to yield the
1672+
// substituted superclass type.
16571673
Module *module = classDecl->getModuleContext();
16581674
return superclassTy.subst(module, substitutions, None);
16591675
}

0 commit comments

Comments
 (0)