Skip to content

Commit 3c56626

Browse files
authored
Merge pull request #67123 from artemcm/59SimplifyConstExtractOpaqueTypes
[5.9 🍒][Compile Time Constant Extraction] Refactor collection of opaque type requirements
2 parents 18d8d9b + d553dce commit 3c56626

File tree

3 files changed

+91
-72
lines changed

3 files changed

+91
-72
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -811,79 +811,41 @@ void writeAttrInformation(llvm::json::OStream &JSON,
811811
});
812812
}
813813

814-
void writeParameterizedProtocolSameTypeRequirements(
815-
llvm::json::OStream &JSON,
816-
const ParameterizedProtocolType &ParameterizedProtoTy) {
817-
auto Protocol = ParameterizedProtoTy.getProtocol();
818-
auto ProtocolTy = ParameterizedProtoTy.getBaseType();
819-
auto Requirements = Protocol->getProtocolRequirements();
820-
auto ParameterTypeNames = Protocol->getPrimaryAssociatedTypeNames();
821-
auto ProtocolArguments = ParameterizedProtoTy.getArgs();
822-
llvm::dbgs() << Requirements.size() << "\n";
823-
assert(ProtocolArguments.size() >= ParameterTypeNames.size());
824-
825-
for (size_t i = 0; i < ProtocolArguments.size(); ++i) {
826-
auto ProtocolArgumentTy = ProtocolArguments[i];
827-
std::string ArgumentName = ParameterTypeNames.size() > i
828-
? ParameterTypeNames[i].first.str().str()
829-
: "unknown";
830-
831-
JSON.object([&] {
832-
auto QualifiedTypeAliasName = toFullyQualifiedProtocolNameString(
833-
*ParameterizedProtoTy.getProtocol()) +
834-
"." + ArgumentName;
835-
JSON.attribute("typeAliasName", QualifiedTypeAliasName);
836-
JSON.attribute("substitutedTypeName",
837-
toFullyQualifiedTypeNameString(ProtocolArgumentTy));
838-
JSON.attribute("substitutedMangledTypeName",
839-
toMangledTypeNameString(ProtocolArgumentTy));
840-
});
841-
}
842-
}
843-
844-
void writeOpaqueTypeProtocolCompositionSameTypeRequirements(
845-
llvm::json::OStream &JSON,
846-
const ProtocolCompositionType &ProtocolCompositionTy) {
847-
for (auto CompositionMemberProto : ProtocolCompositionTy.getMembers()) {
848-
if (auto ParameterizedProtoTy =
849-
CompositionMemberProto->getAs<ParameterizedProtocolType>()) {
850-
writeParameterizedProtocolSameTypeRequirements(JSON,
851-
*ParameterizedProtoTy);
852-
}
853-
}
854-
}
855-
856814
void writeSubstitutedOpaqueTypeAliasDetails(
857815
llvm::json::OStream &JSON, const OpaqueTypeArchetypeType &OpaqueTy) {
816+
auto Signature = OpaqueTy.getDecl()->getOpaqueInterfaceGenericSignature();
817+
858818
JSON.attributeArray("opaqueTypeProtocolRequirements", [&] {
859-
auto ConformsToProtocols = OpaqueTy.getConformsTo();
860-
for (auto Proto : ConformsToProtocols) {
861-
JSON.value(toFullyQualifiedProtocolNameString(*Proto));
819+
for (const auto Requirement : Signature.getRequirements()) {
820+
// Ignore requirements whose subject type is that of the owner decl
821+
if (!Requirement.getFirstType()->isEqual(OpaqueTy.getInterfaceType()))
822+
continue;
823+
if (Requirement.getKind() == RequirementKind::Conformance)
824+
JSON.value(
825+
toFullyQualifiedProtocolNameString(*Requirement.getProtocolDecl()));
862826
}
863827
});
828+
864829
JSON.attributeArray("opaqueTypeSameTypeRequirements", [&] {
865-
auto GenericSig = OpaqueTy.getDecl()
866-
->getNamingDecl()
867-
->getInnermostDeclContext()
868-
->getGenericSignatureOfContext();
869-
auto ConstraintTy = OpaqueTy.getExistentialType();
870-
if (auto existential = ConstraintTy->getAs<ExistentialType>())
871-
ConstraintTy = existential->getConstraintType();
872-
873-
// Opaque archetype substitutions are always canonical, so
874-
// re-sugar the constraint type using the owning
875-
// declaration's generic parameter names.
876-
if (GenericSig)
877-
ConstraintTy = GenericSig->getSugaredType(ConstraintTy);
878-
879-
if (auto ParameterizedProtoTy =
880-
ConstraintTy->getAs<ParameterizedProtocolType>()) {
881-
writeParameterizedProtocolSameTypeRequirements(JSON,
882-
*ParameterizedProtoTy);
883-
} else if (auto ProtocolCompositionTy =
884-
ConstraintTy->getAs<ProtocolCompositionType>()) {
885-
writeOpaqueTypeProtocolCompositionSameTypeRequirements(
886-
JSON, *ProtocolCompositionTy);
830+
for (const auto Requirement : Signature.getRequirements()) {
831+
if (Requirement.getKind() == RequirementKind::SameType) {
832+
auto TypeAliasType = Requirement.getFirstType();
833+
auto TypeWitness = Requirement.getSecondType();
834+
JSON.object([&] {
835+
auto TypeAliasName = toFullyQualifiedTypeNameString(TypeAliasType);
836+
if (auto DependentMemberTy =
837+
TypeAliasType->getAs<DependentMemberType>())
838+
if (const auto *Assoc = DependentMemberTy->getAssocType())
839+
TypeAliasName =
840+
toFullyQualifiedProtocolNameString(*Assoc->getProtocol()) +
841+
"." + DependentMemberTy->getName().str().str();
842+
JSON.attribute("typeAliasName", TypeAliasName);
843+
JSON.attribute("substitutedTypeName",
844+
toFullyQualifiedTypeNameString(TypeWitness));
845+
JSON.attribute("substitutedMangledTypeName",
846+
toMangledTypeNameString(TypeWitness));
847+
});
848+
}
887849
}
888850
});
889851
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/includes)
4+
// RUN: echo "[myProto]" > %t/protocols.json
5+
6+
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx10.15 -typecheck -emit-const-values-path %t/ExtractOpaqueGenericTypealias.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s -I %t/includes
7+
// RUN: cat %t/ExtractOpaqueGenericTypealias.swiftconstvalues 2>&1 | %FileCheck %s
8+
9+
protocol myProto {
10+
associatedtype T
11+
func foo() -> T
12+
}
13+
14+
protocol protoA<T> {
15+
associatedtype T
16+
}
17+
18+
struct A<K> : protoA {
19+
typealias T = K
20+
}
21+
22+
struct Foo<L : Hashable> : myProto {
23+
func foo() -> some protoA<Int> { return A() }
24+
func bar(_ param : L) {}
25+
}
26+
27+
// CHECK: [
28+
// CHECK-NEXT: {
29+
// CHECK-NEXT: "typeName": "ExtractOpaqueGenericTypealias.Foo<L>",
30+
// CHECK-NEXT: "mangledTypeName": "29ExtractOpaqueGenericTypealias3FooVyxG",
31+
// CHECK-NEXT: "kind": "generic struct",
32+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractOpaqueGenericTypealias.swift",
33+
// CHECK-NEXT: "line": 22,
34+
// CHECK-NEXT: "conformances": [
35+
// CHECK-NEXT: "ExtractOpaqueGenericTypealias.myProto",
36+
// CHECK-NEXT: "Swift.Sendable"
37+
// CHECK-NEXT: ],
38+
// CHECK-NEXT: "associatedTypeAliases": [
39+
// CHECK-NEXT: {
40+
// CHECK-NEXT: "typeAliasName": "T",
41+
// CHECK-NEXT: "substitutedTypeName": "some ExtractOpaqueGenericTypealias.protoA<Swift.Int>",
42+
// CHECK-NEXT: "substitutedMangledTypeName": "29ExtractOpaqueGenericTypealias3FooV3fooQryFQOyx_Qo_",
43+
// CHECK-NEXT: "opaqueTypeProtocolRequirements": [
44+
// CHECK-NEXT: "ExtractOpaqueGenericTypealias.protoA"
45+
// CHECK-NEXT: ],
46+
// CHECK-NEXT: "opaqueTypeSameTypeRequirements": [
47+
// CHECK-NEXT: {
48+
// CHECK-NEXT: "typeAliasName": "ExtractOpaqueGenericTypealias.protoA.T",
49+
// CHECK-NEXT: "substitutedTypeName": "Swift.Int",
50+
// CHECK-NEXT: "substitutedMangledTypeName": "Si"
51+
// CHECK-NEXT: }
52+
// CHECK-NEXT: ]
53+
// CHECK-NEXT: }
54+
// CHECK-NEXT: ],
55+
// CHECK-NEXT: "properties": []
56+
// CHECK-NEXT: }
57+
// CHECK-NEXT: ]

test/ConstExtraction/ExtractOpaqueTypealias.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ private func baz() -> some protoA<testModBStruct> & protoB<Float> & testModBProt
5656
// CHECK-NEXT: ],
5757
// CHECK-NEXT: "opaqueTypeSameTypeRequirements": [
5858
// CHECK-NEXT: {
59-
// CHECK-NEXT: "typeAliasName": "ExtractOpaqueTypealias.protoA.T",
60-
// CHECK-NEXT: "substitutedTypeName": "testModB.testModBStruct",
61-
// CHECK-NEXT: "substitutedMangledTypeName": "8testModB0aB7BStructV"
62-
// CHECK-NEXT: },
63-
// CHECK-NEXT: {
6459
// CHECK-NEXT: "typeAliasName": "ExtractOpaqueTypealias.protoB.K",
6560
// CHECK-NEXT: "substitutedTypeName": "Swift.Float",
6661
// CHECK-NEXT: "substitutedMangledTypeName": "Sf"
6762
// CHECK-NEXT: }
63+
// CHECK-NEXT: {
64+
// CHECK-NEXT: "typeAliasName": "ExtractOpaqueTypealias.protoA.T",
65+
// CHECK-NEXT: "substitutedTypeName": "testModB.testModBStruct",
66+
// CHECK-NEXT: "substitutedMangledTypeName": "8testModB0aB7BStructV"
67+
// CHECK-NEXT: }
6868
// CHECK-NEXT: ]
6969
// CHECK-NEXT: }
7070
// CHECK-NEXT: ],

0 commit comments

Comments
 (0)