Skip to content

Commit 9f39c84

Browse files
authored
[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.
1 parent be93fe0 commit 9f39c84

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1762,7 +1762,13 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
17621762
assert(Nominal && "extension of non-nominal type");
17631763
if (auto nt = ExtendedType->getAs<NominalType>()) {
17641764
if (auto ParentType = nt->getParent()) {
1765-
ParentType.print(Printer, Options);
1765+
if (auto *ParentNT = ParentType->getAs<NominalOrBoundGenericNominalType>()) {
1766+
// Avoid using the parent type directly because it can be bound
1767+
// generic type and sugared.
1768+
ParentNT->getDecl()->getDeclaredType().print(Printer, Options);
1769+
} else {
1770+
ParentType.print(Printer, Options);
1771+
}
17661772
Printer << ".";
17671773
}
17681774
}

test/SourceKit/DocSupport/Inputs/cake1.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ public protocol P3 {
2525
public extension P2 where Self : P3 {
2626
func fooConstraint() {}
2727
}
28+
29+
public extension Dictionary.Keys {
30+
public func foo() {}
31+
}

test/SourceKit/DocSupport/doc_swift_module1.swift.response

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ protocol P3 {
4141
func p3Required()
4242
}
4343

44+
extension Dictionary.Keys where Key : Hashable {
45+
46+
func foo()
47+
}
48+
4449

4550
[
4651
{
@@ -383,6 +388,52 @@ protocol P3 {
383388
key.kind: source.lang.swift.syntaxtype.identifier,
384389
key.offset: 439,
385390
key.length: 10
391+
},
392+
{
393+
key.kind: source.lang.swift.syntaxtype.keyword,
394+
key.offset: 455,
395+
key.length: 9
396+
},
397+
{
398+
key.kind: source.lang.swift.ref.struct,
399+
key.name: "Dictionary",
400+
key.usr: "s:s10DictionaryV",
401+
key.offset: 465,
402+
key.length: 10
403+
},
404+
{
405+
key.kind: source.lang.swift.ref.struct,
406+
key.name: "Keys",
407+
key.usr: "s:s10DictionaryV4KeysV",
408+
key.offset: 476,
409+
key.length: 4
410+
},
411+
{
412+
key.kind: source.lang.swift.syntaxtype.keyword,
413+
key.offset: 481,
414+
key.length: 5
415+
},
416+
{
417+
key.kind: source.lang.swift.syntaxtype.typeidentifier,
418+
key.offset: 487,
419+
key.length: 3
420+
},
421+
{
422+
key.kind: source.lang.swift.ref.protocol,
423+
key.name: "Hashable",
424+
key.usr: "s:s8HashableP",
425+
key.offset: 493,
426+
key.length: 8
427+
},
428+
{
429+
key.kind: source.lang.swift.syntaxtype.keyword,
430+
key.offset: 509,
431+
key.length: 4
432+
},
433+
{
434+
key.kind: source.lang.swift.syntaxtype.identifier,
435+
key.offset: 514,
436+
key.length: 3
386437
}
387438
]
388439
[
@@ -610,5 +661,38 @@ protocol P3 {
610661
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>p3Required</decl.name>()</decl.function.method.instance>"
611662
}
612663
]
664+
},
665+
{
666+
key.kind: source.lang.swift.decl.extension.struct,
667+
key.generic_params: [
668+
{
669+
key.name: "Key"
670+
},
671+
{
672+
key.name: "Value"
673+
}
674+
],
675+
key.generic_requirements: [
676+
{
677+
key.description: "Key : Hashable"
678+
}
679+
],
680+
key.offset: 455,
681+
key.length: 66,
682+
key.extends: {
683+
key.kind: source.lang.swift.ref.struct,
684+
key.name: "Keys",
685+
key.usr: "s:s10DictionaryV4KeysV"
686+
},
687+
key.entities: [
688+
{
689+
key.kind: source.lang.swift.decl.function.method.instance,
690+
key.name: "foo()",
691+
key.usr: "s:s10DictionaryV4KeysV5cake1E3fooyyF",
692+
key.offset: 509,
693+
key.length: 10,
694+
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>()</decl.function.method.instance>"
695+
}
696+
]
613697
}
614698
]

0 commit comments

Comments
 (0)