Skip to content

Commit cafb0fd

Browse files
committed
ASTPrinter: Don't crash if parent type of member type was concrete
1 parent d579a76 commit cafb0fd

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7407,39 +7407,51 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
74077407
Printer << "each ";
74087408
}
74097409

7410-
void printArchetypeCommon(ArchetypeType *T) {
7411-
if (Options.AlternativeTypeNames) {
7412-
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
7413-
if (found != Options.AlternativeTypeNames->end()) {
7414-
if (T->isParameterPack()) printEach();
7415-
Printer << found->second.str();
7416-
return;
7410+
void printArchetypeCommon(Type interfaceTy, ArchetypeType *archetypeTy) {
7411+
if (auto *paramTy = interfaceTy->getAs<GenericTypeParamType>()) {
7412+
assert(archetypeTy->isRoot());
7413+
7414+
if (Options.AlternativeTypeNames) {
7415+
auto found = Options.AlternativeTypeNames->find(CanType(archetypeTy));
7416+
if (found != Options.AlternativeTypeNames->end()) {
7417+
if (paramTy->isParameterPack()) printEach();
7418+
Printer << found->second.str();
7419+
return;
7420+
}
74177421
}
7422+
7423+
visit(paramTy);
7424+
return;
74187425
}
74197426

7420-
auto interfaceType = T->getInterfaceType();
7421-
if (auto *dependentMember = interfaceType->getAs<DependentMemberType>()) {
7422-
visitParentType(T->getParent());
7423-
printDependentMember(dependentMember);
7424-
} else {
7425-
visit(interfaceType);
7427+
auto *memberTy = interfaceTy->castTo<DependentMemberType>();
7428+
if (memberTy->getBase()->is<GenericTypeParamType>())
7429+
visitParentType(archetypeTy->getRoot());
7430+
else {
7431+
printArchetypeCommon(memberTy->getBase(), archetypeTy->getRoot());
7432+
Printer << ".";
74267433
}
7434+
7435+
printDependentMember(memberTy);
74277436
}
74287437

74297438
void visitPrimaryArchetypeType(PrimaryArchetypeType *T) {
7430-
printArchetypeCommon(T);
7439+
printArchetypeCommon(T->getInterfaceType(), T);
74317440
}
74327441

74337442
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) {
7434-
if (auto parent = T->getParent()) {
7435-
printArchetypeCommon(T);
7443+
auto interfaceTy = T->getInterfaceType();
7444+
auto *paramTy = interfaceTy->getAs<GenericTypeParamType>();
7445+
7446+
if (!paramTy) {
7447+
assert(interfaceTy->is<DependentMemberType>());
7448+
printArchetypeCommon(interfaceTy, T);
74367449
return;
74377450
}
74387451

74397452
// Try to print a named opaque type.
74407453
auto printNamedOpaque = [&] {
7441-
unsigned ordinal =
7442-
T->getInterfaceType()->castTo<GenericTypeParamType>()->getIndex();
7454+
unsigned ordinal = paramTy->getIndex();
74437455
if (auto genericParam = T->getDecl()->getExplicitGenericParam(ordinal)) {
74447456
visit(genericParam->getDeclaredInterfaceType());
74457457
return true;
@@ -7486,9 +7498,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
74867498
Printer.printEscapedStringLiteral(
74877499
decl->getOpaqueReturnTypeIdentifier().str());
74887500

7489-
Printer << ", " << T->getInterfaceType()
7490-
->castTo<GenericTypeParamType>()
7491-
->getIndex();
7501+
Printer << ", " << paramTy->getIndex();
74927502

74937503
// The identifier after the closing parenthesis is irrelevant and can be
74947504
// anything. It just needs to be there for the @_opaqueReturnTypeOf
@@ -7520,7 +7530,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
75207530
}
75217531

75227532
void visitPackArchetypeType(PackArchetypeType *T) {
7523-
printArchetypeCommon(T);
7533+
printArchetypeCommon(T->getInterfaceType(), T);
75247534
}
75257535

75267536
void visitGenericTypeParamType(GenericTypeParamType *T) {

0 commit comments

Comments
 (0)