Skip to content

Commit b9e0d7f

Browse files
committed
[AST] Fix printing of constrained protocol extensions
We wanted to omit the 'Self: TheProtocolBeingExtension' part of the generic signature, but the logic that was there accidentally omitted /all/ constraints on Self.
1 parent fa8ac95 commit b9e0d7f

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
638638
PrintParams = 1,
639639
PrintRequirements = 2,
640640
InnermostOnly = 4,
641-
SkipSelfRequirement = 8,
642-
SwapSelfAndDependentMemberType = 16,
643-
PrintInherited = 32,
641+
SwapSelfAndDependentMemberType = 8,
642+
PrintInherited = 16,
644643
};
645644

646645
void printInheritedFromRequirementSignature(ProtocolDecl *proto,
@@ -1227,13 +1226,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
12271226
if (req.getKind() != RequirementKind::Layout)
12281227
second = req.getSecondType();
12291228

1230-
if ((flags & SkipSelfRequirement) &&
1231-
req.getKind() == RequirementKind::Conformance) {
1232-
auto proto = cast<ProtocolDecl>(second->getAnyNominal());
1233-
if (first->isEqual(proto->getSelfInterfaceType()))
1234-
continue;
1235-
}
1236-
12371229
if (!subMap.empty()) {
12381230
if (Type subFirst = substParam(first))
12391231
first = subFirst;
@@ -1870,14 +1862,19 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
18701862
});
18711863
printInherited(decl);
18721864

1873-
if (decl->getGenericParams())
1874-
if (auto *genericSig = decl->getGenericSignature()) {
1875-
// For protocol extensions, don't print the 'Self : ...' requirement.
1876-
unsigned flags = PrintRequirements | InnermostOnly;
1877-
if (decl->getExtendedProtocolDecl())
1878-
flags |= SkipSelfRequirement;
1879-
printGenericSignature(genericSig, flags);
1880-
}
1865+
if (auto *genericSig = decl->getGenericSignature()) {
1866+
printGenericSignature(genericSig, PrintRequirements | InnermostOnly,
1867+
[decl](const Requirement &req) -> bool {
1868+
// For protocol extensions, omit the implicit
1869+
// "Self: TheProtoBeingExtended" requirement.
1870+
if (req.getKind() != RequirementKind::Conformance)
1871+
return true;
1872+
auto proto = cast<ProtocolDecl>(req.getSecondType()->getAnyNominal());
1873+
if (proto != decl->getExtendedNominal())
1874+
return true;
1875+
return !req.getFirstType()->isEqual(proto->getSelfInterfaceType());
1876+
});
1877+
}
18811878
}
18821879
if (Options.TypeDefinitions) {
18831880
printMembersOfDecl(decl, false,

test/SourceKit/DocSupport/doc_swift_module1.swift.response

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension P2 {
5555
subscript(_ a: Int) -> Int
5656
}
5757

58-
extension P2 {
58+
extension P2 where Self : P3 {
5959

6060
func fooConstraint()
6161
}
@@ -532,78 +532,97 @@ extension Dictionary.Keys where Key : Hashable {
532532
},
533533
{
534534
key.kind: source.lang.swift.syntaxtype.keyword,
535-
key.offset: 633,
535+
key.offset: 626,
536+
key.length: 5
537+
},
538+
{
539+
key.kind: source.lang.swift.ref.generic_type_param,
540+
key.name: "Self",
541+
key.usr: "s:5cake12P2PA2A2P3RzrlE4Selfxmfp",
542+
key.offset: 632,
543+
key.length: 4
544+
},
545+
{
546+
key.kind: source.lang.swift.ref.protocol,
547+
key.name: "P3",
548+
key.usr: "s:5cake12P3P",
549+
key.offset: 639,
550+
key.length: 2
551+
},
552+
{
553+
key.kind: source.lang.swift.syntaxtype.keyword,
554+
key.offset: 649,
536555
key.length: 4
537556
},
538557
{
539558
key.kind: source.lang.swift.syntaxtype.identifier,
540-
key.offset: 638,
559+
key.offset: 654,
541560
key.length: 13
542561
},
543562
{
544563
key.kind: source.lang.swift.syntaxtype.keyword,
545-
key.offset: 657,
564+
key.offset: 673,
546565
key.length: 8
547566
},
548567
{
549568
key.kind: source.lang.swift.syntaxtype.identifier,
550-
key.offset: 666,
569+
key.offset: 682,
551570
key.length: 2
552571
},
553572
{
554573
key.kind: source.lang.swift.syntaxtype.keyword,
555-
key.offset: 676,
574+
key.offset: 692,
556575
key.length: 4
557576
},
558577
{
559578
key.kind: source.lang.swift.syntaxtype.identifier,
560-
key.offset: 681,
579+
key.offset: 697,
561580
key.length: 10
562581
},
563582
{
564583
key.kind: source.lang.swift.syntaxtype.keyword,
565-
key.offset: 697,
584+
key.offset: 713,
566585
key.length: 9
567586
},
568587
{
569588
key.kind: source.lang.swift.ref.struct,
570589
key.name: "Dictionary",
571590
key.usr: "s:SD",
572-
key.offset: 707,
591+
key.offset: 723,
573592
key.length: 10
574593
},
575594
{
576595
key.kind: source.lang.swift.ref.struct,
577596
key.name: "Keys",
578597
key.usr: "s:SD4KeysV",
579-
key.offset: 718,
598+
key.offset: 734,
580599
key.length: 4
581600
},
582601
{
583602
key.kind: source.lang.swift.syntaxtype.keyword,
584-
key.offset: 723,
603+
key.offset: 739,
585604
key.length: 5
586605
},
587606
{
588607
key.kind: source.lang.swift.syntaxtype.typeidentifier,
589-
key.offset: 729,
608+
key.offset: 745,
590609
key.length: 3
591610
},
592611
{
593612
key.kind: source.lang.swift.ref.protocol,
594613
key.name: "Hashable",
595614
key.usr: "s:SH",
596-
key.offset: 735,
615+
key.offset: 751,
597616
key.length: 8
598617
},
599618
{
600619
key.kind: source.lang.swift.syntaxtype.keyword,
601-
key.offset: 751,
620+
key.offset: 767,
602621
key.length: 4
603622
},
604623
{
605624
key.kind: source.lang.swift.syntaxtype.identifier,
606-
key.offset: 756,
625+
key.offset: 772,
607626
key.length: 3
608627
}
609628
]
@@ -944,7 +963,7 @@ extension Dictionary.Keys where Key : Hashable {
944963
}
945964
],
946965
key.offset: 613,
947-
key.length: 42,
966+
key.length: 58,
948967
key.extends: {
949968
key.kind: source.lang.swift.ref.protocol,
950969
key.name: "P2",
@@ -956,7 +975,7 @@ extension Dictionary.Keys where Key : Hashable {
956975
key.name: "fooConstraint()",
957976
key.usr: "s:5cake12P2PA2A2P3RzrlE13fooConstraintyyF",
958977
key.default_implementation_of: "s:5cake12P1P13fooConstraintyyF",
959-
key.offset: 633,
978+
key.offset: 649,
960979
key.length: 20,
961980
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>fooConstraint</decl.name>()</decl.function.method.instance>"
962981
}
@@ -966,15 +985,15 @@ extension Dictionary.Keys where Key : Hashable {
966985
key.kind: source.lang.swift.decl.protocol,
967986
key.name: "P3",
968987
key.usr: "s:5cake12P3P",
969-
key.offset: 657,
988+
key.offset: 673,
970989
key.length: 38,
971990
key.fully_annotated_decl: "<decl.protocol><syntaxtype.keyword>protocol</syntaxtype.keyword> <decl.name>P3</decl.name></decl.protocol>",
972991
key.entities: [
973992
{
974993
key.kind: source.lang.swift.decl.function.method.instance,
975994
key.name: "p3Required()",
976995
key.usr: "s:5cake12P3P10p3RequiredyyF",
977-
key.offset: 676,
996+
key.offset: 692,
978997
key.length: 17,
979998
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>p3Required</decl.name>()</decl.function.method.instance>"
980999
}
@@ -995,7 +1014,7 @@ extension Dictionary.Keys where Key : Hashable {
9951014
key.description: "Key : Hashable"
9961015
}
9971016
],
998-
key.offset: 697,
1017+
key.offset: 713,
9991018
key.length: 66,
10001019
key.extends: {
10011020
key.kind: source.lang.swift.ref.struct,
@@ -1007,7 +1026,7 @@ extension Dictionary.Keys where Key : Hashable {
10071026
key.kind: source.lang.swift.decl.function.method.instance,
10081027
key.name: "foo()",
10091028
key.usr: "s:SD4KeysV5cake1E3fooyyF",
1010-
key.offset: 751,
1029+
key.offset: 767,
10111030
key.length: 10,
10121031
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>()</decl.function.method.instance>"
10131032
}

0 commit comments

Comments
 (0)