Skip to content

Commit 0d9e62e

Browse files
Merge pull request #76917 from venkatesh5789/external-conformance-info
[Compile Time Constant Extraction] Add information about where a particular conformance has been defined
2 parents a49d8ff + 9ca55fd commit 0d9e62e

13 files changed

+237
-4
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,26 @@ void writeConformances(llvm::json::OStream &JSON,
13901390
});
13911391
}
13921392

1393+
void writeAllConformances(llvm::json::OStream &JSON,
1394+
const NominalTypeDecl &NomTypeDecl) {
1395+
JSON.attributeArray("allConformances", [&] {
1396+
for (auto *Conformance : NomTypeDecl.getAllConformances()) {
1397+
auto Proto = Conformance->getProtocol();
1398+
// FIXME(noncopyable_generics): Should these be included?
1399+
if (Proto->getInvertibleProtocolKind())
1400+
continue;
1401+
1402+
JSON.object([&] {
1403+
JSON.attribute("protocolName",
1404+
toFullyQualifiedProtocolNameString(*Proto));
1405+
JSON.attribute(
1406+
"conformanceDefiningModule",
1407+
Conformance->getDeclContext()->getParentModule()->getName().str());
1408+
});
1409+
}
1410+
});
1411+
}
1412+
13931413
void writeTypeName(llvm::json::OStream &JSON, const TypeDecl &TypeDecl) {
13941414
JSON.attribute("typeName",
13951415
toFullyQualifiedTypeNameString(
@@ -1423,6 +1443,10 @@ bool writeAsJSONToFile(const std::vector<ConstValueTypeInfo> &ConstValueInfos,
14231443
writeNominalTypeKind(JSON, *NomTypeDecl);
14241444
writeLocationInformation(JSON, SourceLoc, Ctx);
14251445
writeConformances(JSON, *NomTypeDecl);
1446+
1447+
// "conformances" will be removed once all clients move to
1448+
// "allConformances"
1449+
writeAllConformances(JSON, *NomTypeDecl);
14261450
writeAssociatedTypeAliases(JSON, *NomTypeDecl);
14271451
writeProperties(JSON, TypeInfo, *NomTypeDecl);
14281452
writeEnumCases(JSON, TypeInfo.EnumElements);

test/ConstExtraction/ExtractAnnotations.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public struct DeprecatedAnnotations: MyProto {}
3737
// CHECK-NEXT: "conformances": [
3838
// CHECK-NEXT: "ExtractAnnotations.MyProto"
3939
// CHECK-NEXT: ],
40+
// CHECK-NEXT: "allConformances": [
41+
// CHECK-NEXT: {
42+
// CHECK-NEXT: "protocolName": "ExtractAnnotations.MyProto"
43+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractAnnotations"
44+
// CHECK-NEXT: }
45+
// CHECK-NEXT: ],
4046
// CHECK-NEXT: "associatedTypeAliases": [],
4147
// CHECK-NEXT: "properties": [
4248
// CHECK-NEXT: {
@@ -139,6 +145,12 @@ public struct DeprecatedAnnotations: MyProto {}
139145
// CHECK-NEXT: "conformances": [
140146
// CHECK-NEXT: "ExtractAnnotations.MyProto"
141147
// CHECK-NEXT: ],
148+
// CHECK-NEXT: "allConformances": [
149+
// CHECK-NEXT: {
150+
// CHECK-NEXT: "protocolName": "ExtractAnnotations.MyProto"
151+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractAnnotations"
152+
// CHECK-NEXT: }
153+
// CHECK-NEXT: ],
142154
// CHECK-NEXT: "associatedTypeAliases": [],
143155
// CHECK-NEXT: "properties": [],
144156
// CHECK-NEXT: "availabilityAttributes": [

test/ConstExtraction/ExtractCalls.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public struct Bat {
4646
// CHECK-NEXT: "conformances": [
4747
// CHECK-NEXT: "ExtractCalls.MyProto"
4848
// CHECK-NEXT: ],
49+
// CHECK-NEXT: "allConformances": [
50+
// CHECK-NEXT: {
51+
// CHECK-NEXT: "protocolName": "ExtractCalls.MyProto"
52+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractCalls"
53+
// CHECK-NEXT: }
54+
// CHECK-NEXT: ],
4955
// CHECK-NEXT: "associatedTypeAliases": [],
5056
// CHECK-NEXT: "properties": [
5157
// CHECK-NEXT: {

test/ConstExtraction/ExtractEnums.swift

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public struct Enums: MyProto {
4040
// CHECK-NEXT: "Swift.Hashable",
4141
// CHECK-NEXT: "ExtractEnums.MyProto"
4242
// CHECK-NEXT: ],
43+
// CHECK-NEXT: "allConformances": [
44+
// CHECK-NEXT: {
45+
// CHECK-NEXT: "protocolName": "Swift.Equatable",
46+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
47+
// CHECK-NEXT: },
48+
// CHECK-NEXT: {
49+
// CHECK-NEXT: "protocolName": "Swift.Hashable",
50+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
51+
// CHECK-NEXT: },
52+
// CHECK-NEXT: {
53+
// CHECK-NEXT: "protocolName": "ExtractEnums.MyProto",
54+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
55+
// CHECK-NEXT: }
56+
// CHECK-NEXT: ],
4357
// CHECK-NEXT: "associatedTypeAliases": [],
4458
// CHECK-NEXT: "properties": [
4559
// CHECK-NEXT: {
@@ -82,6 +96,24 @@ public struct Enums: MyProto {
8296
// CHECK-NEXT: "Swift.RawRepresentable",
8397
// CHECK-NEXT: "ExtractEnums.MyProto"
8498
// CHECK-NEXT: ],
99+
// CHECK-NEXT: "allConformances": [
100+
// CHECK-NEXT: {
101+
// CHECK-NEXT: "protocolName": "Swift.Equatable",
102+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
103+
// CHECK-NEXT: },
104+
// CHECK-NEXT: {
105+
// CHECK-NEXT: "protocolName": "Swift.Hashable",
106+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
107+
// CHECK-NEXT: },
108+
// CHECK-NEXT: {
109+
// CHECK-NEXT: "protocolName": "Swift.RawRepresentable",
110+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
111+
// CHECK-NEXT: },
112+
// CHECK-NEXT: {
113+
// CHECK-NEXT: "protocolName": "ExtractEnums.MyProto",
114+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
115+
// CHECK-NEXT: }
116+
// CHECK-NEXT: ],
85117
// CHECK-NEXT: "associatedTypeAliases": [
86118
// CHECK-NEXT: {
87119
// CHECK-NEXT: "typeAliasName": "RawValue",
@@ -123,7 +155,13 @@ public struct Enums: MyProto {
123155
// CHECK-NEXT: "conformances": [
124156
// CHECK-NEXT: "ExtractEnums.MyProto"
125157
// CHECK-NEXT: ],
126-
// CHECK-NEXT: "associatedTypeAliases": [],
158+
// CHECK-NEXT: "allConformances": [
159+
// CHECK-NEXT: {
160+
// CHECK-NEXT: "protocolName": "ExtractEnums.MyProto",
161+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
162+
// CHECK-NEXT: }
163+
// CHECK-NEXT: ],
164+
// CHECK-NEXT: "associatedTypeAliases": [],
127165
// CHECK-NEXT: "properties": [],
128166
// CHECK-NEXT: "cases": [
129167
// CHECK-NEXT: {
@@ -158,7 +196,13 @@ public struct Enums: MyProto {
158196
// CHECK-NEXT: "conformances": [
159197
// CHECK-NEXT: "ExtractEnums.MyProto"
160198
// CHECK-NEXT: ],
161-
// CHECK-NEXT: "associatedTypeAliases": [],
199+
// CHECK-NEXT: "allConformances": [
200+
// CHECK-NEXT: {
201+
// CHECK-NEXT: "protocolName": "ExtractEnums.MyProto",
202+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractEnums"
203+
// CHECK-NEXT: }
204+
// CHECK-NEXT: ],
205+
// CHECK-NEXT: "associatedTypeAliases": [],
162206
// CHECK-NEXT: "properties": [
163207
// CHECK-NEXT: {
164208
// CHECK-NEXT: "label": "enum1",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/includes)
4+
// RUN: echo "[MyProto]" > %t/protocols.json
5+
6+
// Build external Swift library/module to also check conformances to external protocols
7+
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 %S/../Reflection/Inputs/swiftmodules/testModB.swift -parse-as-library -emit-module -emit-library -module-name testModB -o %t/includes/testModB.o
8+
9+
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx10.15 -typecheck -emit-const-values-path %t/ExtractExternalConformances.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s -I %t/includes
10+
// RUN: cat %t/ExtractExternalConformances.swiftconstvalues 2>&1 | %FileCheck %s
11+
import testModB
12+
13+
public protocol MyProto { }
14+
15+
extension TestExternalConformanceStruct: MyProto {}
16+
17+
extension TestExternalConformanceStruct: TestExternalConformanceProtocol {}
18+
19+
// CHECK: [
20+
// CHECK-NEXT: {
21+
// CHECK-NEXT: "typeName": "testModB.TestExternalConformanceStruct",
22+
// CHECK-NEXT: "mangledTypeName": "8testModB29TestExternalConformanceStructV",
23+
// CHECK-NEXT: "kind": "struct",
24+
// CHECK-NEXT: "conformances": [
25+
// CHECK-NEXT: "testModB.testModBProtocol",
26+
// CHECK-NEXT: "ExtractExternalConformances.MyProto",
27+
// CHECK-NEXT: "testModB.TestExternalConformanceProtocol"
28+
// CHECK-NEXT: ],
29+
// CHECK-NEXT: "allConformances": [
30+
// CHECK-NEXT: {
31+
// CHECK-NEXT: "protocolName": "testModB.testModBProtocol",
32+
// CHECK-NEXT: "conformanceDefiningModule": "testModB"
33+
// CHECK-NEXT: },
34+
// CHECK-NEXT: {
35+
// CHECK-NEXT: "protocolName": "ExtractExternalConformances.MyProto",
36+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractExternalConformances"
37+
// CHECK-NEXT: },
38+
// CHECK-NEXT: {
39+
// CHECK-NEXT: "protocolName": "testModB.TestExternalConformanceProtocol",
40+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractExternalConformances"
41+
// CHECK-NEXT: }
42+
// CHECK-NEXT: ],
43+
// CHECK-NEXT: "associatedTypeAliases": [],
44+
// CHECK-NEXT: "properties": []
45+
// CHECK-NEXT: }
46+
// CHECK-NEXT: ]

test/ConstExtraction/ExtractGroups.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ extension String: Foo {}
4545
// CHECK-NEXT: "conformances": [
4646
// CHECK-NEXT: "ExtractGroups.MyProto"
4747
// CHECK-NEXT: ],
48+
// CHECK-NEXT: "allConformances": [
49+
// CHECK-NEXT: {
50+
// CHECK-NEXT: "protocolName": "ExtractGroups.MyProto"
51+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractGroups"
52+
// CHECK-NEXT: }
53+
// CHECK-NEXT: ],
4854
// CHECK-NEXT: "associatedTypeAliases": [],
4955
// CHECK-NEXT: "properties": [
5056
// CHECK-NEXT: {
@@ -128,7 +134,13 @@ extension String: Foo {}
128134
// CHECK-NEXT: "conformances": [
129135
// CHECK-NEXT: "ExtractGroups.MyProto"
130136
// CHECK-NEXT: ],
131-
// CHECK-NEXT: "associatedTypeAliases": [],
137+
// CHECK-NEXT: "allConformances": [
138+
// CHECK-NEXT: {
139+
// CHECK-NEXT: "protocolName": "ExtractGroups.MyProto"
140+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractGroups"
141+
// CHECK-NEXT: }
142+
// CHECK-NEXT: ],
143+
// CHECK-NEXT: "associatedTypeAliases": [],
132144
// CHECK-NEXT: "properties": [
133145
// CHECK-NEXT: {
134146
// CHECK-NEXT: "label": "dict1",
@@ -268,6 +280,12 @@ extension String: Foo {}
268280
// CHECK-NEXT: "conformances": [
269281
// CHECK-NEXT: "ExtractGroups.MyProto"
270282
// CHECK-NEXT: ],
283+
// CHECK-NEXT: "allConformances": [
284+
// CHECK-NEXT: {
285+
// CHECK-NEXT: "protocolName": "ExtractGroups.MyProto"
286+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractGroups"
287+
// CHECK-NEXT: }
288+
// CHECK-NEXT: ],
271289
// CHECK-NEXT: "associatedTypeAliases": [],
272290
// CHECK-NEXT: "properties": [
273291
// CHECK-NEXT: {

test/ConstExtraction/ExtractInterpolatedStringLiterals.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public struct External: MyProto {
3636
// CHECK-NEXT: "conformances": [
3737
// CHECK-NEXT: "ExtractInterpolatedStringLiterals.MyProto"
3838
// CHECK-NEXT: ],
39+
// CHECK-NEXT: "allConformances": [
40+
// CHECK-NEXT: {
41+
// CHECK-NEXT: "protocolName": "ExtractInterpolatedStringLiterals.MyProto"
42+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractInterpolatedStringLiterals"
43+
// CHECK-NEXT: }
44+
// CHECK-NEXT: ],
3945
// CHECK-NEXT: "associatedTypeAliases": [],
4046
// CHECK-NEXT: "properties": [
4147
// CHECK-NEXT: {
@@ -60,6 +66,12 @@ public struct External: MyProto {
6066
// CHECK-NEXT: "conformances": [
6167
// CHECK-NEXT: "ExtractInterpolatedStringLiterals.MyProto"
6268
// CHECK-NEXT: ],
69+
// CHECK-NEXT: "allConformances": [
70+
// CHECK-NEXT: {
71+
// CHECK-NEXT: "protocolName": "ExtractInterpolatedStringLiterals.MyProto"
72+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractInterpolatedStringLiterals"
73+
// CHECK-NEXT: }
74+
// CHECK-NEXT: ],
6375
// CHECK-NEXT: "associatedTypeAliases": [],
6476
// CHECK-NEXT: "properties": [
6577
// CHECK-NEXT: {

test/ConstExtraction/ExtractKeyPaths.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ public struct KeyPaths: MyProto {
3636
// CHECK-NEXT: "conformances": [
3737
// CHECK-NEXT: "ExtractKeyPaths.MyProto"
3838
// CHECK-NEXT: ],
39-
// CHECK-NEXT: "associatedTypeAliases": [],
39+
// CHECK-NEXT: "allConformances": [
40+
// CHECK-NEXT: {
41+
// CHECK-NEXT: "protocolName": "ExtractKeyPaths.MyProto"
42+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractKeyPaths"
43+
// CHECK-NEXT: }
44+
// CHECK-NEXT: ],
45+
// CHECK-NEXT: "associatedTypeAliases": [],
4046
// CHECK-NEXT: "properties": [
4147
// CHECK-NEXT: {
4248
// CHECK-NEXT: "label": "nestedVariable",

test/ConstExtraction/ExtractLiterals.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public struct PropertyWrappers : MyProto {
104104
// CHECK-NEXT: "conformances": [
105105
// CHECK-NEXT: "ExtractLiterals.MyProto"
106106
// CHECK-NEXT: ],
107+
// CHECK-NEXT: "allConformances": [
108+
// CHECK-NEXT: {
109+
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto"
110+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
111+
// CHECK-NEXT: }
112+
// CHECK-NEXT: ],
107113
// CHECK-NEXT: "associatedTypeAliases": [],
108114
// CHECK-NEXT: "properties": [
109115
// CHECK-NEXT: {
@@ -139,6 +145,12 @@ public struct PropertyWrappers : MyProto {
139145
// CHECK-NEXT: "conformances": [
140146
// CHECK-NEXT: "ExtractLiterals.MyProto"
141147
// CHECK-NEXT: ],
148+
// CHECK-NEXT: "allConformances": [
149+
// CHECK-NEXT: {
150+
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto"
151+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
152+
// CHECK-NEXT: }
153+
// CHECK-NEXT: ],
142154
// CHECK-NEXT: "associatedTypeAliases": [],
143155
// CHECK-NEXT: "properties": [
144156
// CHECK-NEXT: {
@@ -185,6 +197,12 @@ public struct PropertyWrappers : MyProto {
185197
// CHECK-NEXT: "conformances": [
186198
// CHECK-NEXT: "ExtractLiterals.MyProto"
187199
// CHECK-NEXT: ],
200+
// CHECK-NEXT: "allConformances": [
201+
// CHECK-NEXT: {
202+
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto"
203+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
204+
// CHECK-NEXT: }
205+
// CHECK-NEXT: ],
188206
// CHECK-NEXT: "associatedTypeAliases": [],
189207
// CHECK-NEXT: "properties": [
190208
// CHECK-NEXT: {
@@ -230,6 +248,12 @@ public struct PropertyWrappers : MyProto {
230248
// CHECK-NEXT: "conformances": [
231249
// CHECK-NEXT: "ExtractLiterals.MyProto"
232250
// CHECK-NEXT: ],
251+
// CHECK-NEXT: "allConformances": [
252+
// CHECK-NEXT: {
253+
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto"
254+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
255+
// CHECK-NEXT: }
256+
// CHECK-NEXT: ],
233257
// CHECK-NEXT: "associatedTypeAliases": [],
234258
// CHECK-NEXT: "properties": [
235259
// CHECK-NEXT: {
@@ -276,6 +300,12 @@ public struct PropertyWrappers : MyProto {
276300
// CHECK-NEXT: "conformances": [
277301
// CHECK-NEXT: "ExtractLiterals.MyProto"
278302
// CHECK-NEXT: ],
303+
// CHECK-NEXT: "allConformances": [
304+
// CHECK-NEXT: {
305+
// CHECK-NEXT: "protocolName": "ExtractLiterals.MyProto"
306+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractLiterals"
307+
// CHECK-NEXT: }
308+
// CHECK-NEXT: ],
279309
// CHECK-NEXT: "associatedTypeAliases": [],
280310
// CHECK-NEXT: "properties": [
281311
// CHECK-NEXT: {

test/ConstExtraction/ExtractOpaqueGenericTypealias.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ struct Foo<L : Hashable> : myProto {
3636
// CHECK-NEXT: "Swift.Sendable",
3737
// CHECK-NEXT: "Swift.BitwiseCopyable"
3838
// CHECK-NEXT: ],
39+
// CHECK-NEXT: "allConformances": [
40+
// CHECK-NEXT: {
41+
// CHECK-NEXT: "protocolName": "ExtractOpaqueGenericTypealias.myProto",
42+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpaqueGenericTypealias"
43+
// CHECK-NEXT: },
44+
// CHECK-NEXT: {
45+
// CHECK-NEXT: "protocolName": "Swift.Sendable",
46+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpaqueGenericTypealias"
47+
// CHECK-NEXT: },
48+
// CHECK-NEXT: {
49+
// CHECK-NEXT: "protocolName": "Swift.BitwiseCopyable",
50+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpaqueGenericTypealias"
51+
// CHECK-NEXT: }
52+
// CHECK-NEXT: ],
3953
// CHECK-NEXT: "associatedTypeAliases": [
4054
// CHECK-NEXT: {
4155
// CHECK-NEXT: "typeAliasName": "T",

test/ConstExtraction/ExtractOpaqueTypealias.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ private func baz() -> some protoA<testModBStruct> & protoB<Float> & testModBProt
4444
// CHECK-NEXT: "conformances": [
4545
// CHECK-NEXT: "ExtractOpaqueTypealias.myProto"
4646
// CHECK-NEXT: ],
47+
// CHECK-NEXT: "allConformances": [
48+
// CHECK-NEXT: {
49+
// CHECK-NEXT: "protocolName": "ExtractOpaqueTypealias.myProto"
50+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpaqueTypealias"
51+
// CHECK-NEXT: }
52+
// CHECK-NEXT: ],
4753
// CHECK-NEXT: "associatedTypeAliases": [
4854
// CHECK-NEXT: {
4955
// CHECK-NEXT: "typeAliasName": "PerformReturn",

0 commit comments

Comments
 (0)