Skip to content

Commit 6b0e173

Browse files
authored
Merge pull request swiftlang#18779 from jrose-apple/overconstrained
[AST] Fix printing of extensions' generic signatures
2 parents c0114ea + 859b31f commit 6b0e173

File tree

3 files changed

+123
-44
lines changed

3 files changed

+123
-44
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
636636
PrintParams = 1,
637637
PrintRequirements = 2,
638638
InnermostOnly = 4,
639-
SkipSelfRequirement = 8,
640-
SwapSelfAndDependentMemberType = 16,
641-
PrintInherited = 32,
639+
SwapSelfAndDependentMemberType = 8,
640+
PrintInherited = 16,
642641
};
643642

644643
void printInheritedFromRequirementSignature(ProtocolDecl *proto,
@@ -1225,13 +1224,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
12251224
if (req.getKind() != RequirementKind::Layout)
12261225
second = req.getSecondType();
12271226

1228-
if ((flags & SkipSelfRequirement) &&
1229-
req.getKind() == RequirementKind::Conformance) {
1230-
auto proto = cast<ProtocolDecl>(second->getAnyNominal());
1231-
if (first->isEqual(proto->getSelfInterfaceType()))
1232-
continue;
1233-
}
1234-
12351227
if (!subMap.empty()) {
12361228
if (Type subFirst = substParam(first))
12371229
first = subFirst;
@@ -1868,14 +1860,16 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
18681860
});
18691861
printInherited(decl);
18701862

1871-
if (decl->getGenericParams())
1872-
if (auto *genericSig = decl->getGenericSignature()) {
1873-
// For protocol extensions, don't print the 'Self : ...' requirement.
1874-
unsigned flags = PrintRequirements | InnermostOnly;
1875-
if (decl->getExtendedProtocolDecl())
1876-
flags |= SkipSelfRequirement;
1877-
printGenericSignature(genericSig, flags);
1878-
}
1863+
if (auto *genericSig = decl->getGenericSignature()) {
1864+
auto *baseGenericSig = decl->getExtendedNominal()->getGenericSignature();
1865+
assert(baseGenericSig &&
1866+
"an extension can't be generic if the base type isn't");
1867+
printGenericSignature(genericSig, PrintRequirements | InnermostOnly,
1868+
[baseGenericSig](const Requirement &req) -> bool {
1869+
// Only include constraints that are not satisfied by the base type.
1870+
return !baseGenericSig->isRequirementSatisfied(req);
1871+
});
1872+
}
18791873
}
18801874
if (Options.TypeDefinitions) {
18811875
printMembersOfDecl(decl, false,

test/SourceKit/DocSupport/Inputs/cake1.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public extension Dictionary.Keys {
3030
public func foo() {}
3131
}
3232

33+
public extension Dictionary.Keys where Key: P1 {
34+
public func bar() {}
35+
}
36+
3337
public protocol InitProto {
3438
init(x: Int)
3539
}

test/SourceKit/DocSupport/doc_swift_module1.swift.response

Lines changed: 107 additions & 26 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
}
@@ -65,11 +65,16 @@ protocol P3 {
6565
func p3Required()
6666
}
6767

68-
extension Dictionary.Keys where Key : Hashable {
68+
extension Dictionary.Keys {
6969

7070
func foo()
7171
}
7272

73+
extension Dictionary.Keys where Key : P1 {
74+
75+
func bar()
76+
}
77+
7378

7479
[
7580
{
@@ -532,78 +537,126 @@ extension Dictionary.Keys where Key : Hashable {
532537
},
533538
{
534539
key.kind: source.lang.swift.syntaxtype.keyword,
535-
key.offset: 633,
540+
key.offset: 626,
541+
key.length: 5
542+
},
543+
{
544+
key.kind: source.lang.swift.ref.generic_type_param,
545+
key.name: "Self",
546+
key.usr: "s:5cake12P2PA2A2P3RzrlE4Selfxmfp",
547+
key.offset: 632,
548+
key.length: 4
549+
},
550+
{
551+
key.kind: source.lang.swift.ref.protocol,
552+
key.name: "P3",
553+
key.usr: "s:5cake12P3P",
554+
key.offset: 639,
555+
key.length: 2
556+
},
557+
{
558+
key.kind: source.lang.swift.syntaxtype.keyword,
559+
key.offset: 649,
536560
key.length: 4
537561
},
538562
{
539563
key.kind: source.lang.swift.syntaxtype.identifier,
540-
key.offset: 638,
564+
key.offset: 654,
541565
key.length: 13
542566
},
543567
{
544568
key.kind: source.lang.swift.syntaxtype.keyword,
545-
key.offset: 657,
569+
key.offset: 673,
546570
key.length: 8
547571
},
548572
{
549573
key.kind: source.lang.swift.syntaxtype.identifier,
550-
key.offset: 666,
574+
key.offset: 682,
551575
key.length: 2
552576
},
553577
{
554578
key.kind: source.lang.swift.syntaxtype.keyword,
555-
key.offset: 676,
579+
key.offset: 692,
556580
key.length: 4
557581
},
558582
{
559583
key.kind: source.lang.swift.syntaxtype.identifier,
560-
key.offset: 681,
584+
key.offset: 697,
561585
key.length: 10
562586
},
563587
{
564588
key.kind: source.lang.swift.syntaxtype.keyword,
565-
key.offset: 697,
589+
key.offset: 713,
566590
key.length: 9
567591
},
568592
{
569593
key.kind: source.lang.swift.ref.struct,
570594
key.name: "Dictionary",
571595
key.usr: "s:SD",
572-
key.offset: 707,
596+
key.offset: 723,
573597
key.length: 10
574598
},
575599
{
576600
key.kind: source.lang.swift.ref.struct,
577601
key.name: "Keys",
578602
key.usr: "s:SD4KeysV",
579-
key.offset: 718,
603+
key.offset: 734,
580604
key.length: 4
581605
},
582606
{
583607
key.kind: source.lang.swift.syntaxtype.keyword,
584-
key.offset: 723,
608+
key.offset: 746,
609+
key.length: 4
610+
},
611+
{
612+
key.kind: source.lang.swift.syntaxtype.identifier,
613+
key.offset: 751,
614+
key.length: 3
615+
},
616+
{
617+
key.kind: source.lang.swift.syntaxtype.keyword,
618+
key.offset: 760,
619+
key.length: 9
620+
},
621+
{
622+
key.kind: source.lang.swift.ref.struct,
623+
key.name: "Dictionary",
624+
key.usr: "s:SD",
625+
key.offset: 770,
626+
key.length: 10
627+
},
628+
{
629+
key.kind: source.lang.swift.ref.struct,
630+
key.name: "Keys",
631+
key.usr: "s:SD4KeysV",
632+
key.offset: 781,
633+
key.length: 4
634+
},
635+
{
636+
key.kind: source.lang.swift.syntaxtype.keyword,
637+
key.offset: 786,
585638
key.length: 5
586639
},
587640
{
588641
key.kind: source.lang.swift.syntaxtype.typeidentifier,
589-
key.offset: 729,
642+
key.offset: 792,
590643
key.length: 3
591644
},
592645
{
593646
key.kind: source.lang.swift.ref.protocol,
594-
key.name: "Hashable",
595-
key.usr: "s:SH",
596-
key.offset: 735,
597-
key.length: 8
647+
key.name: "P1",
648+
key.usr: "s:5cake12P1P",
649+
key.offset: 798,
650+
key.length: 2
598651
},
599652
{
600653
key.kind: source.lang.swift.syntaxtype.keyword,
601-
key.offset: 751,
654+
key.offset: 808,
602655
key.length: 4
603656
},
604657
{
605658
key.kind: source.lang.swift.syntaxtype.identifier,
606-
key.offset: 756,
659+
key.offset: 813,
607660
key.length: 3
608661
}
609662
]
@@ -944,7 +997,7 @@ extension Dictionary.Keys where Key : Hashable {
944997
}
945998
],
946999
key.offset: 613,
947-
key.length: 42,
1000+
key.length: 58,
9481001
key.extends: {
9491002
key.kind: source.lang.swift.ref.protocol,
9501003
key.name: "P2",
@@ -956,7 +1009,7 @@ extension Dictionary.Keys where Key : Hashable {
9561009
key.name: "fooConstraint()",
9571010
key.usr: "s:5cake12P2PA2A2P3RzrlE13fooConstraintyyF",
9581011
key.default_implementation_of: "s:5cake12P1P13fooConstraintyyF",
959-
key.offset: 633,
1012+
key.offset: 649,
9601013
key.length: 20,
9611014
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>fooConstraint</decl.name>()</decl.function.method.instance>"
9621015
}
@@ -966,15 +1019,15 @@ extension Dictionary.Keys where Key : Hashable {
9661019
key.kind: source.lang.swift.decl.protocol,
9671020
key.name: "P3",
9681021
key.usr: "s:5cake12P3P",
969-
key.offset: 657,
1022+
key.offset: 673,
9701023
key.length: 38,
9711024
key.fully_annotated_decl: "<decl.protocol><syntaxtype.keyword>protocol</syntaxtype.keyword> <decl.name>P3</decl.name></decl.protocol>",
9721025
key.entities: [
9731026
{
9741027
key.kind: source.lang.swift.decl.function.method.instance,
9751028
key.name: "p3Required()",
9761029
key.usr: "s:5cake12P3P10p3RequiredyyF",
977-
key.offset: 676,
1030+
key.offset: 692,
9781031
key.length: 17,
9791032
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>p3Required</decl.name>()</decl.function.method.instance>"
9801033
}
@@ -995,8 +1048,8 @@ extension Dictionary.Keys where Key : Hashable {
9951048
key.description: "Key : Hashable"
9961049
}
9971050
],
998-
key.offset: 697,
999-
key.length: 66,
1051+
key.offset: 713,
1052+
key.length: 45,
10001053
key.extends: {
10011054
key.kind: source.lang.swift.ref.struct,
10021055
key.name: "Keys",
@@ -1007,10 +1060,38 @@ extension Dictionary.Keys where Key : Hashable {
10071060
key.kind: source.lang.swift.decl.function.method.instance,
10081061
key.name: "foo()",
10091062
key.usr: "s:SD4KeysV5cake1E3fooyyF",
1010-
key.offset: 751,
1063+
key.offset: 746,
10111064
key.length: 10,
10121065
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>()</decl.function.method.instance>"
10131066
}
10141067
]
1068+
},
1069+
{
1070+
key.kind: source.lang.swift.decl.extension.struct,
1071+
key.generic_requirements: [
1072+
{
1073+
key.description: "Key : Hashable"
1074+
},
1075+
{
1076+
key.description: "Key : P1"
1077+
}
1078+
],
1079+
key.offset: 760,
1080+
key.length: 60,
1081+
key.extends: {
1082+
key.kind: source.lang.swift.ref.struct,
1083+
key.name: "Keys",
1084+
key.usr: "s:SD4KeysV"
1085+
},
1086+
key.entities: [
1087+
{
1088+
key.kind: source.lang.swift.decl.function.method.instance,
1089+
key.name: "bar()",
1090+
key.usr: "s:SD4KeysV5cake1AC2P1RzrlE3baryyF",
1091+
key.offset: 808,
1092+
key.length: 10,
1093+
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>bar</decl.name>()</decl.function.method.instance>"
1094+
}
1095+
]
10151096
}
10161097
]

0 commit comments

Comments
 (0)