Skip to content

Commit 859b31f

Browse files
committed
[AST] Fix printing of *un*constrained generic extensions
We were redundantly printing constraints that were implied by the base type (the type being extended). Rather than special-casing constraints on a protocol's 'Self' type, omit any requirements that are already satisfied by the extended type instead. https://bugs.swift.org/browse/SR-7413
1 parent b9e0d7f commit 859b31f

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,16 +1863,13 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
18631863
printInherited(decl);
18641864

18651865
if (auto *genericSig = decl->getGenericSignature()) {
1866+
auto *baseGenericSig = decl->getExtendedNominal()->getGenericSignature();
1867+
assert(baseGenericSig &&
1868+
"an extension can't be generic if the base type isn't");
18661869
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());
1870+
[baseGenericSig](const Requirement &req) -> bool {
1871+
// Only include constraints that are not satisfied by the base type.
1872+
return !baseGenericSig->isRequirementSatisfied(req);
18761873
});
18771874
}
18781875
}

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: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{
@@ -600,29 +605,58 @@ extension Dictionary.Keys where Key : Hashable {
600605
},
601606
{
602607
key.kind: source.lang.swift.syntaxtype.keyword,
603-
key.offset: 739,
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,
604638
key.length: 5
605639
},
606640
{
607641
key.kind: source.lang.swift.syntaxtype.typeidentifier,
608-
key.offset: 745,
642+
key.offset: 792,
609643
key.length: 3
610644
},
611645
{
612646
key.kind: source.lang.swift.ref.protocol,
613-
key.name: "Hashable",
614-
key.usr: "s:SH",
615-
key.offset: 751,
616-
key.length: 8
647+
key.name: "P1",
648+
key.usr: "s:5cake12P1P",
649+
key.offset: 798,
650+
key.length: 2
617651
},
618652
{
619653
key.kind: source.lang.swift.syntaxtype.keyword,
620-
key.offset: 767,
654+
key.offset: 808,
621655
key.length: 4
622656
},
623657
{
624658
key.kind: source.lang.swift.syntaxtype.identifier,
625-
key.offset: 772,
659+
key.offset: 813,
626660
key.length: 3
627661
}
628662
]
@@ -1015,7 +1049,7 @@ extension Dictionary.Keys where Key : Hashable {
10151049
}
10161050
],
10171051
key.offset: 713,
1018-
key.length: 66,
1052+
key.length: 45,
10191053
key.extends: {
10201054
key.kind: source.lang.swift.ref.struct,
10211055
key.name: "Keys",
@@ -1026,10 +1060,38 @@ extension Dictionary.Keys where Key : Hashable {
10261060
key.kind: source.lang.swift.decl.function.method.instance,
10271061
key.name: "foo()",
10281062
key.usr: "s:SD4KeysV5cake1E3fooyyF",
1029-
key.offset: 767,
1063+
key.offset: 746,
10301064
key.length: 10,
10311065
key.fully_annotated_decl: "<decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>()</decl.function.method.instance>"
10321066
}
10331067
]
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+
]
10341096
}
10351097
]

0 commit comments

Comments
 (0)