Skip to content

Commit 99c78ad

Browse files
authored
Merge pull request swiftlang#32425 from bitjammer/acgarland/rdar-64425199-extension-where-constraints
[SymbolGraph] Only include where clause constraints in `swiftExtension`
2 parents 1c8ad2f + a9d692c commit 99c78ad

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

lib/SymbolGraphGen/JSON.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ void swift::symbolgraphgen::serialize(const ExtensionDecl *Extension,
6969

7070
SmallVector<Requirement, 4> FilteredRequirements;
7171

72-
filterGenericRequirements(Extension->getGenericRequirements(),
73-
Extension->getExtendedNominal()
74-
->getDeclContext()->getSelfNominalTypeDecl(),
72+
filterGenericRequirements(Extension,
7573
FilteredRequirements);
7674

7775
if (!FilteredRequirements.empty()) {
@@ -122,24 +120,36 @@ void swift::symbolgraphgen::filterGenericRequirements(
122120
const NominalTypeDecl *Self,
123121
SmallVectorImpl<Requirement> &FilteredRequirements) {
124122
for (const auto &Req : Requirements) {
125-
if (Req.getKind() == RequirementKind::Layout) {
126-
continue;
127-
}
128-
/*
129-
Don't serialize constraints that aren't applicable for display.
123+
if (Req.getKind() == RequirementKind::Layout) {
124+
continue;
125+
}
126+
// extension /* protocol */ Q {
127+
// func foo() {}
128+
// }
129+
// ignore Self : Q, obvious
130+
if (Req.getSecondType()->getAnyNominal() == Self) {
131+
continue;
132+
}
133+
FilteredRequirements.push_back(Req);
134+
}
135+
}
130136

131-
For example:
137+
void
138+
swift::symbolgraphgen::filterGenericRequirements(const ExtensionDecl *Extension,
139+
SmallVectorImpl<Requirement> &FilteredRequirements) {
140+
for (const auto &Req : Extension->getGenericRequirements()) {
141+
if (Req.getKind() == RequirementKind::Layout) {
142+
continue;
143+
}
132144

133-
extension Equatable {
134-
func foo(_ thing: Self) {}
135-
}
145+
if (!isa<ProtocolDecl>(Extension->getExtendedNominal()) &&
146+
Req.getFirstType()->isEqual(Extension->getExtendedType())) {
147+
continue;
148+
}
136149

137-
`foo` includes a constraint `Self: Equatable` for the compiler's purposes,
138-
but that's redundant for the purposes of documentation.
139-
This is extending Equatable, after all!
140-
*/
141-
if (Req.getFirstType()->getString() == "Self" &&
142-
Req.getSecondType()->getAnyNominal() == Self) {
150+
// extension /* protocol */ Q
151+
// ignore Self : Q, obvious
152+
if (Req.getSecondType()->isEqual(Extension->getExtendedType())) {
143153
continue;
144154
}
145155
FilteredRequirements.push_back(Req);

lib/SymbolGraphGen/JSON.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ void serialize(const swift::GenericTypeParamType *Param,
4545
llvm::json::OStream &OS);
4646

4747

48-
/// Filter generic requirements that aren't relevant for documentation.
48+
/// Filter generic requirements on an extension that aren't relevant
49+
/// for documentation.
4950
void
5051
filterGenericRequirements(ArrayRef<Requirement> Requirements,
5152
const NominalTypeDecl *Self,
5253
SmallVectorImpl<Requirement> &FilteredRequirements);
54+
55+
/// Filter generic requirements on an extension that aren't relevant
56+
/// for documentation.
57+
void
58+
filterGenericRequirements(const ExtensionDecl *Extension,
59+
SmallVectorImpl<Requirement> &FilteredRequirements);
60+
5361
} // end namespace symbolgraphgen
5462
} // end namespace swift
5563

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ void Symbol::serializeSwiftGenericMixin(llvm::json::OStream &OS) const {
280280
}
281281

282282
filterGenericRequirements(Generics->getRequirements(),
283-
Self,
284-
FilteredRequirements);
283+
Self,
284+
FilteredRequirements);
285285

286286
if (FilteredParams.empty() && FilteredRequirements.empty()) {
287287
return;

test/SymbolGraph/Symbols/Mixins/Extension.swift renamed to test/SymbolGraph/Symbols/Mixins/Extension/GetFromExtension.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift %s -module-name Extension -emit-module-path %t/Extension.swiftmodule
3-
// RUN: %target-swift-symbolgraph-extract -module-name Extension -I %t -pretty-print -output-dir %t
4-
// RUN: %FileCheck %s --input-file %t/Extension.symbols.json
2+
// RUN: %target-build-swift %s -module-name GetFromExtension -emit-module-path %t/GetFromExtension.swiftmodule
3+
// RUN: %target-swift-symbolgraph-extract -module-name GetFromExtension -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/GetFromExtension.symbols.json
55

66
public struct Outer<T> {
77
public var x: T
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name IgnoreInherited -emit-module-path %t/IgnoreInherited.swiftmodule
3+
// RUN: %target-swift-symbolgraph-extract -module-name IgnoreInherited -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/IgnoreInherited.symbols.json
5+
6+
public protocol P {
7+
associatedtype T
8+
static func foo() -> T
9+
}
10+
11+
public struct S<T> {}
12+
13+
extension S: P where T: Sequence, T.Element == Int {
14+
public static func foo() -> AnySequence<Int> {
15+
return AnySequence([0])
16+
}
17+
}
18+
19+
// CHECK-LABEL: "precise": "s:15IgnoreInherited1SVAASTRzSi7ElementRtzlE3foos11AnySequenceVySiGyFZ"
20+
// CHECK: swiftExtension
21+
// CHECK: "constraints": [
22+
// CHECK-NEXT: {
23+
// CHECK-NEXT: "kind": "conformance",
24+
// CHECK-NEXT: "lhs": "T",
25+
// CHECK-NEXT: "rhs": "Sequence"
26+
// CHECK-NEXT: },
27+
// CHECK-NEXT: {
28+
// CHECK-NEXT: "kind": "sameType",
29+
// CHECK-NEXT: "lhs": "T.Element",
30+
// CHECK-NEXT: "rhs": "Int"
31+
// CHECK-NEXT: }
32+
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)