From 9f39c8494d3f1f93ac393c98c99b62cadb82e4c7 Mon Sep 17 00:00:00 2001 From: Xi Ge Date: Thu, 1 Mar 2018 09:19:44 -0800 Subject: [PATCH] [SourceKitd] Avoid printing parent type directly on extended type. rdar:37965902 (#14892) When printing parent type directly, we may print generic arguments and sugared dictionary type. --- lib/AST/ASTPrinter.cpp | 8 +- test/SourceKit/DocSupport/Inputs/cake1.swift | 4 + .../doc_swift_module1.swift.response | 84 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/lib/AST/ASTPrinter.cpp b/lib/AST/ASTPrinter.cpp index 8e659c5b611..f01ad7caf80 100644 --- a/lib/AST/ASTPrinter.cpp +++ b/lib/AST/ASTPrinter.cpp @@ -1762,7 +1762,13 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer, assert(Nominal && "extension of non-nominal type"); if (auto nt = ExtendedType->getAs()) { if (auto ParentType = nt->getParent()) { - ParentType.print(Printer, Options); + if (auto *ParentNT = ParentType->getAs()) { + // Avoid using the parent type directly because it can be bound + // generic type and sugared. + ParentNT->getDecl()->getDeclaredType().print(Printer, Options); + } else { + ParentType.print(Printer, Options); + } Printer << "."; } } diff --git a/test/SourceKit/DocSupport/Inputs/cake1.swift b/test/SourceKit/DocSupport/Inputs/cake1.swift index 0b535099cd9..1895342e351 100644 --- a/test/SourceKit/DocSupport/Inputs/cake1.swift +++ b/test/SourceKit/DocSupport/Inputs/cake1.swift @@ -25,3 +25,7 @@ public protocol P3 { public extension P2 where Self : P3 { func fooConstraint() {} } + +public extension Dictionary.Keys { + public func foo() {} +} diff --git a/test/SourceKit/DocSupport/doc_swift_module1.swift.response b/test/SourceKit/DocSupport/doc_swift_module1.swift.response index 193fea061de..9de4a197ecd 100644 --- a/test/SourceKit/DocSupport/doc_swift_module1.swift.response +++ b/test/SourceKit/DocSupport/doc_swift_module1.swift.response @@ -41,6 +41,11 @@ protocol P3 { func p3Required() } +extension Dictionary.Keys where Key : Hashable { + + func foo() +} + [ { @@ -383,6 +388,52 @@ protocol P3 { key.kind: source.lang.swift.syntaxtype.identifier, key.offset: 439, key.length: 10 + }, + { + key.kind: source.lang.swift.syntaxtype.keyword, + key.offset: 455, + key.length: 9 + }, + { + key.kind: source.lang.swift.ref.struct, + key.name: "Dictionary", + key.usr: "s:s10DictionaryV", + key.offset: 465, + key.length: 10 + }, + { + key.kind: source.lang.swift.ref.struct, + key.name: "Keys", + key.usr: "s:s10DictionaryV4KeysV", + key.offset: 476, + key.length: 4 + }, + { + key.kind: source.lang.swift.syntaxtype.keyword, + key.offset: 481, + key.length: 5 + }, + { + key.kind: source.lang.swift.syntaxtype.typeidentifier, + key.offset: 487, + key.length: 3 + }, + { + key.kind: source.lang.swift.ref.protocol, + key.name: "Hashable", + key.usr: "s:s8HashableP", + key.offset: 493, + key.length: 8 + }, + { + key.kind: source.lang.swift.syntaxtype.keyword, + key.offset: 509, + key.length: 4 + }, + { + key.kind: source.lang.swift.syntaxtype.identifier, + key.offset: 514, + key.length: 3 } ] [ @@ -610,5 +661,38 @@ protocol P3 { key.fully_annotated_decl: "func p3Required()" } ] + }, + { + key.kind: source.lang.swift.decl.extension.struct, + key.generic_params: [ + { + key.name: "Key" + }, + { + key.name: "Value" + } + ], + key.generic_requirements: [ + { + key.description: "Key : Hashable" + } + ], + key.offset: 455, + key.length: 66, + key.extends: { + key.kind: source.lang.swift.ref.struct, + key.name: "Keys", + key.usr: "s:s10DictionaryV4KeysV" + }, + key.entities: [ + { + key.kind: source.lang.swift.decl.function.method.instance, + key.name: "foo()", + key.usr: "s:s10DictionaryV4KeysV5cake1E3fooyyF", + key.offset: 509, + key.length: 10, + key.fully_annotated_decl: "func foo()" + } + ] } ]