Skip to content

Commit 67740fd

Browse files
committed
ASTPrinter: Don't crash if parent type of member type was concrete
1 parent d005743 commit 67740fd

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6055,7 +6055,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60556055
switch (Options.OpaqueReturnTypePrinting) {
60566056
case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
60576057
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
6058-
return true;
6058+
return false;
60596059
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
60606060
return opaque->getDecl()->hasExplicitGenericParams();
60616061
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword:
@@ -7413,39 +7413,51 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
74137413
Printer << "each ";
74147414
}
74157415

7416-
void printArchetypeCommon(ArchetypeType *T) {
7417-
if (Options.AlternativeTypeNames) {
7418-
auto found = Options.AlternativeTypeNames->find(T->getCanonicalType());
7419-
if (found != Options.AlternativeTypeNames->end()) {
7420-
if (T->isParameterPack()) printEach();
7421-
Printer << found->second.str();
7422-
return;
7416+
void printArchetypeCommon(Type interfaceTy, ArchetypeType *archetypeTy) {
7417+
if (auto *paramTy = interfaceTy->getAs<GenericTypeParamType>()) {
7418+
assert(archetypeTy->isRoot());
7419+
7420+
if (Options.AlternativeTypeNames) {
7421+
auto found = Options.AlternativeTypeNames->find(CanType(archetypeTy));
7422+
if (found != Options.AlternativeTypeNames->end()) {
7423+
if (paramTy->isParameterPack()) printEach();
7424+
Printer << found->second.str();
7425+
return;
7426+
}
74237427
}
7428+
7429+
visit(paramTy);
7430+
return;
74247431
}
74257432

7426-
auto interfaceType = T->getInterfaceType();
7427-
if (auto *dependentMember = interfaceType->getAs<DependentMemberType>()) {
7428-
visitParentType(T->getParent());
7429-
printDependentMember(dependentMember);
7430-
} else {
7431-
visit(interfaceType);
7433+
auto *memberTy = interfaceTy->castTo<DependentMemberType>();
7434+
if (memberTy->getBase()->is<GenericTypeParamType>())
7435+
visitParentType(archetypeTy->getRoot());
7436+
else {
7437+
printArchetypeCommon(memberTy->getBase(), archetypeTy->getRoot());
7438+
Printer << ".";
74327439
}
7440+
7441+
printDependentMember(memberTy);
74337442
}
74347443

74357444
void visitPrimaryArchetypeType(PrimaryArchetypeType *T) {
7436-
printArchetypeCommon(T);
7445+
printArchetypeCommon(T->getInterfaceType(), T);
74377446
}
74387447

74397448
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) {
7440-
if (auto parent = T->getParent()) {
7441-
printArchetypeCommon(T);
7449+
auto interfaceTy = T->getInterfaceType();
7450+
auto *paramTy = interfaceTy->getAs<GenericTypeParamType>();
7451+
7452+
if (!paramTy) {
7453+
assert(interfaceTy->is<DependentMemberType>());
7454+
printArchetypeCommon(interfaceTy, T);
74427455
return;
74437456
}
74447457

74457458
// Try to print a named opaque type.
74467459
auto printNamedOpaque = [&] {
7447-
unsigned ordinal =
7448-
T->getInterfaceType()->castTo<GenericTypeParamType>()->getIndex();
7460+
unsigned ordinal = paramTy->getIndex();
74497461
if (auto genericParam = T->getDecl()->getExplicitGenericParam(ordinal)) {
74507462
visit(genericParam->getDeclaredInterfaceType());
74517463
return true;
@@ -7492,9 +7504,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
74927504
Printer.printEscapedStringLiteral(
74937505
decl->getOpaqueReturnTypeIdentifier().str());
74947506

7495-
Printer << ", " << T->getInterfaceType()
7496-
->castTo<GenericTypeParamType>()
7497-
->getIndex();
7507+
Printer << ", " << paramTy->getIndex();
74987508

74997509
// The identifier after the closing parenthesis is irrelevant and can be
75007510
// anything. It just needs to be there for the @_opaqueReturnTypeOf
@@ -7526,7 +7536,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
75267536
}
75277537

75287538
void visitPackArchetypeType(PackArchetypeType *T) {
7529-
printArchetypeCommon(T);
7539+
printArchetypeCommon(T->getInterfaceType(), T);
75307540
}
75317541

75327542
void visitGenericTypeParamType(GenericTypeParamType *T) {

test/Generics/opaque_archetype_concrete_requirement_recursive_rejected.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ protocol HasRecursiveP {
3131

3232
extension HasRecursiveP where T == DefinesRecursiveP.T {}
3333
// expected-error@-1 {{cannot build rewrite system for generic signature; rule length limit exceeded}}
34-
// expected-note@-2 {{failed rewrite rule is τ_0_0.[HasRecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[concrete: ((((((((((((@_opaqueReturnTypeOf("$s56opaque_archetype_concrete_requirement_recursive_rejected17DefinesRecursivePV1tQrvp", 0) __.T).T).T).T).T).T).T).T).T).T).T).T).T] => τ_0_0.[HasRecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T]}}
34+
// expected-note@-2 {{failed rewrite rule is τ_0_0.[HasRecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[concrete: (@_opaqueReturnTypeOf("$s56opaque_archetype_concrete_requirement_recursive_rejected17DefinesRecursivePV1tQrvp", 0) __).T.T.T.T.T.T.T.T.T.T.T.T.T] => τ_0_0.[HasRecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T].[RecursiveP:T]}}
3535

test/SILGen/opaque_result_type_private_assoc_type.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import opaque_result_type_private_assoc_type_other
66

77
// CHECK-LABEL: sil hidden [ossa] @$s033opaque_result_type_private_assoc_C0028usesAssocTypeOfPrivateResultH0yyF : $@convention(thin) () -> () {
88
func usesAssocTypeOfPrivateResultType() {
9-
// CHECK: [[BOX:%.*]] = alloc_stack $Optional<@_opaqueReturnTypeOf("$s033opaque_result_type_private_assoc_C6_other11doSomethingQryF", 0) __.Element>
9+
// CHECK: [[BOX:%.*]] = alloc_stack $Optional<(@_opaqueReturnTypeOf("$s033opaque_result_type_private_assoc_C6_other11doSomethingQryF", 0) __).Element>
1010
// CHECK: [[METHOD:%.*]] = witness_method $@_opaqueReturnTypeOf("$s033opaque_result_type_private_assoc_C6_other11doSomethingQryF", 0) __, #IteratorProtocol.next : <Self where Self : opaque_result_type_private_assoc_type_other.IteratorProtocol> (Self) -> () -> Self.Element? : $@convention(witness_method: IteratorProtocol) <τ_0_0 where τ_0_0 : IteratorProtocol> (@in_guaranteed τ_0_0) -> @out Optional<τ_0_0.Element>
1111
// CHECK: [[RESULT:%.*]] = apply [[METHOD]]<@_opaqueReturnTypeOf("$s033opaque_result_type_private_assoc_C6_other11doSomethingQryF", 0) __>([[BOX]], {{%.*}}) : $@convention(witness_method: IteratorProtocol) <τ_0_0 where τ_0_0 : IteratorProtocol> (@in_guaranteed τ_0_0) -> @out Optional<τ_0_0.Element>
1212
let iterator = doSomething()

0 commit comments

Comments
 (0)