@@ -943,7 +943,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
943
943
InnermostOnly = 4 ,
944
944
SwapSelfAndDependentMemberType = 8 ,
945
945
PrintInherited = 16 ,
946
- CollapseDefaultReqs = 32 ,
947
946
};
948
947
949
948
void printInheritedFromRequirementSignature (ProtocolDecl *proto,
@@ -953,11 +952,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
953
952
void printInherited (const Decl *decl);
954
953
955
954
void printGenericSignature (GenericSignature genericSig,
956
- ArrayRef<InverseRequirement> inverses,
957
955
unsigned flags);
958
956
void
959
957
printGenericSignature (GenericSignature genericSig,
960
- ArrayRef<InverseRequirement> inverses,
961
958
unsigned flags,
962
959
llvm::function_ref<bool (const Requirement &)> filter);
963
960
void printSingleDepthOfGenericSignature (
@@ -1457,44 +1454,6 @@ static unsigned getDepthOfType(Type ty) {
1457
1454
return depth;
1458
1455
}
1459
1456
1460
- // / Recomputes which inverses must have been written for the given generic
1461
- // / signature.
1462
- static void reconstituteInverses (GenericSignature genericSig,
1463
- ArrayRef<Type> typeParams,
1464
- SmallVectorImpl<InverseRequirement> &inverses) {
1465
- auto &ctx = genericSig->getASTContext ();
1466
-
1467
- if (!ctx.LangOpts .hasFeature (swift::Feature::NoncopyableGenerics))
1468
- return ;
1469
-
1470
- for (auto tp : typeParams) {
1471
- assert (tp);
1472
-
1473
- // Any generic parameter with a superclass bound could not have an inverse.
1474
- if (genericSig->getSuperclassBound (tp))
1475
- continue ;
1476
-
1477
- auto defaults = InverseRequirement::expandDefault (tp);
1478
- for (auto ip : defaults) {
1479
- auto *proto = ctx.getProtocol (getKnownProtocolKind (ip));
1480
-
1481
- // If the generic signature reflects the default requirement,
1482
- // then there was no inverse for this generic parameter.
1483
- if (genericSig->requiresProtocol (tp, proto))
1484
- continue ;
1485
-
1486
- inverses.push_back ({tp, proto, SourceLoc ()});
1487
- }
1488
- }
1489
- }
1490
-
1491
- static void reconstituteInverses (GenericSignature genericSig,
1492
- ArrayRef<GenericTypeParamType *> genericParams,
1493
- SmallVectorImpl<InverseRequirement> &inverses) {
1494
- SmallVector<Type, 4 > asType (genericParams);
1495
- reconstituteInverses (genericSig, asType, inverses);
1496
- }
1497
-
1498
1457
namespace {
1499
1458
struct RequirementPrintLocation {
1500
1459
// / The Decl where the requirement should be attached (whether inherited or in
@@ -1615,13 +1574,9 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1615
1574
else
1616
1575
llvm_unreachable (" nonexhaustive" );
1617
1576
1618
- SmallVector<InverseRequirement, 2 > inverses;
1619
- reconstituteInverses (proto->getGenericSignature (), attachedGP, inverses);
1620
-
1621
1577
printGenericSignature (
1622
1578
proto->getRequirementSignatureAsGenericSignature (),
1623
- inverses,
1624
- PrintInherited | CollapseDefaultReqs,
1579
+ PrintInherited,
1625
1580
[&](const Requirement &req) {
1626
1581
// Skip the inferred 'Self : AnyObject' constraint if this is an
1627
1582
// @objc protocol.
@@ -1640,12 +1595,11 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1640
1595
1641
1596
void PrintAST::printWhereClauseFromRequirementSignature (ProtocolDecl *proto,
1642
1597
TypeDecl *attachingTo) {
1643
- unsigned flags = PrintRequirements | CollapseDefaultReqs ;
1598
+ unsigned flags = PrintRequirements;
1644
1599
if (isa<AssociatedTypeDecl>(attachingTo))
1645
1600
flags |= SwapSelfAndDependentMemberType;
1646
1601
printGenericSignature (
1647
1602
proto->getRequirementSignatureAsGenericSignature (),
1648
- {}, // NOTE: a protocol's inverses are only printed in inheritance clause!
1649
1603
flags,
1650
1604
[&](const Requirement &req) {
1651
1605
auto location = bestRequirementPrintLocation (proto, req);
@@ -1681,31 +1635,20 @@ static unsigned getDepthOfRequirement(const Requirement &req) {
1681
1635
llvm_unreachable (" bad RequirementKind" );
1682
1636
}
1683
1637
1684
- static void getRequirementsAtDepth (GenericSignature genericSig,
1685
- unsigned depth,
1686
- SmallVectorImpl<Requirement> &result) {
1687
- for (auto reqt : genericSig.getRequirements ()) {
1688
- unsigned currentDepth = getDepthOfRequirement (reqt);
1689
- assert (currentDepth != ErrorDepth);
1690
- if (currentDepth == depth)
1691
- result.push_back (reqt);
1692
- }
1693
- }
1694
-
1695
1638
void PrintAST::printGenericSignature (GenericSignature genericSig,
1696
- ArrayRef<InverseRequirement> inverses,
1697
1639
unsigned flags) {
1698
- printGenericSignature (genericSig, inverses, flags,
1640
+ printGenericSignature (genericSig, flags,
1699
1641
// print everything
1700
1642
[&](const Requirement &) { return true ; });
1701
1643
}
1702
1644
1703
1645
void PrintAST::printGenericSignature (
1704
1646
GenericSignature genericSig,
1705
- ArrayRef<InverseRequirement> inverses,
1706
1647
unsigned flags,
1707
1648
llvm::function_ref<bool (const Requirement &)> filter) {
1708
- auto requirements = genericSig.getRequirements ();
1649
+ SmallVector<Requirement, 2 > requirements;
1650
+ SmallVector<InverseRequirement, 2 > inverses;
1651
+ genericSig->getRequirementsWithInverses (requirements, inverses);
1709
1652
1710
1653
if (flags & InnermostOnly) {
1711
1654
auto genericParams = genericSig.getInnermostGenericParams ();
@@ -1741,34 +1684,27 @@ void PrintAST::printGenericSignature(
1741
1684
genericParams.slice (paramIdx, lastParamIdx - paramIdx);
1742
1685
1743
1686
SmallVector<InverseRequirement, 2 > inversesAtDepth;
1744
- if (flags & CollapseDefaultReqs)
1745
- reconstituteInverses (genericSig, genericParamsAtDepth, inversesAtDepth);
1687
+ for (auto inverseReq : inverses) {
1688
+ if (inverseReq.subject ->castTo <GenericTypeParamType>()->getDepth () == depth)
1689
+ inversesAtDepth.push_back (inverseReq);
1690
+ }
1746
1691
1747
1692
SmallVector<Requirement, 2 > requirementsAtDepth;
1748
- getRequirementsAtDepth (genericSig, depth, requirementsAtDepth);
1693
+ for (auto reqt : requirements) {
1694
+ unsigned currentDepth = getDepthOfRequirement (reqt);
1695
+ assert (currentDepth != ErrorDepth);
1696
+ if (currentDepth == depth)
1697
+ requirementsAtDepth.push_back (reqt);
1698
+ }
1749
1699
1750
1700
printSingleDepthOfGenericSignature (
1751
- genericParamsAtDepth,
1752
- requirementsAtDepth, inversesAtDepth, flags, filter);
1701
+ genericParamsAtDepth, requirementsAtDepth, inversesAtDepth,
1702
+ flags, filter);
1753
1703
1754
1704
paramIdx = lastParamIdx;
1755
1705
}
1756
1706
}
1757
1707
1758
- // / \returns the invertible protocol kind iff this requirement is a conformance
1759
- // / to an invertible protocol.
1760
- static llvm::Optional<InvertibleProtocolKind>
1761
- getInvertibleProtocolKind (const Requirement &req) {
1762
- if (req.getKind () != RequirementKind::Conformance)
1763
- return llvm::None;
1764
-
1765
- auto constraintTy = req.getSecondType ();
1766
- if (auto kp = constraintTy->getKnownProtocol ())
1767
- return getInvertibleProtocolKind (*kp);
1768
-
1769
- return llvm::None;
1770
- }
1771
-
1772
1708
void PrintAST::printSingleDepthOfGenericSignature (
1773
1709
ArrayRef<GenericTypeParamType *> genericParams,
1774
1710
ArrayRef<Requirement> requirements,
@@ -1792,7 +1728,6 @@ void PrintAST::printSingleDepthOfGenericSignature(
1792
1728
bool printInherited = (flags & PrintInherited);
1793
1729
bool swapSelfAndDependentMemberType =
1794
1730
(flags & SwapSelfAndDependentMemberType);
1795
- const bool collapseDefaults = (flags & CollapseDefaultReqs);
1796
1731
1797
1732
unsigned typeContextDepth = 0 ;
1798
1733
SubstitutionMap subMap;
@@ -1884,17 +1819,10 @@ void PrintAST::printSingleDepthOfGenericSignature(
1884
1819
}
1885
1820
1886
1821
if (printRequirements || printInherited) {
1887
- // If we're not collapsing defaults, we shouldn't also print inverses!
1888
- assert (collapseDefaults || inverses.empty ());
1889
-
1890
1822
for (const auto &req : requirements) {
1891
1823
if (!filter (req))
1892
1824
continue ;
1893
1825
1894
- // / Skip all requirements for a conformance to an invertible protocol.
1895
- if (collapseDefaults && getInvertibleProtocolKind (req))
1896
- continue ;
1897
-
1898
1826
auto first = req.getFirstType ();
1899
1827
1900
1828
if (dependsOnOpaque (first))
@@ -2678,7 +2606,7 @@ void PrintAST::printGenericDeclGenericParams(GenericContext *decl) {
2678
2606
if (decl->isGeneric ())
2679
2607
if (auto GenericSig = decl->getGenericSignature ()) {
2680
2608
Printer.printStructurePre (PrintStructureKind::DeclGenericParameterClause);
2681
- printGenericSignature (GenericSig, {}, PrintParams | InnermostOnly);
2609
+ printGenericSignature (GenericSig, PrintParams | InnermostOnly);
2682
2610
Printer.printStructurePost (PrintStructureKind::DeclGenericParameterClause);
2683
2611
}
2684
2612
}
@@ -2697,13 +2625,9 @@ void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
2697
2625
SmallVector<Type, 2 > genericParams;
2698
2626
InverseRequirement::enumerateDefaultedParams (decl, genericParams);
2699
2627
2700
- SmallVector<InverseRequirement, 2 > inverses;
2701
- reconstituteInverses (genericSig, genericParams, inverses);
2702
-
2703
2628
Printer.printStructurePre (PrintStructureKind::DeclGenericParameterClause);
2704
2629
printGenericSignature (genericSig,
2705
- inverses,
2706
- PrintRequirements | CollapseDefaultReqs,
2630
+ PrintRequirements,
2707
2631
[parentSig](const Requirement &req) {
2708
2632
if (parentSig)
2709
2633
return !parentSig->isRequirementSatisfied (req);
@@ -2961,11 +2885,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
2961
2885
auto baseGenericSig = decl->getExtendedNominal ()->getGenericSignature ();
2962
2886
assert (baseGenericSig &&
2963
2887
" an extension can't be generic if the base type isn't" );
2964
- // NOTE: We do _not_ CollapseDefaultReqs for the where-clause of an
2965
- // extension, because conditions involving Copyable here are not
2966
- // automatically inferred based on the extension itself.
2967
2888
printGenericSignature (genericSig,
2968
- {},
2969
2889
PrintRequirements,
2970
2890
[baseGenericSig](const Requirement &req) -> bool {
2971
2891
// Only include constraints that are not satisfied by the base type.
@@ -7216,12 +7136,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7216
7136
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
7217
7137
}
7218
7138
7219
- void printGenericSignature (GenericSignature genericSig,
7220
- ArrayRef<InverseRequirement> inverses,
7221
- unsigned flags) {
7222
- PrintAST (Printer, Options).printGenericSignature (genericSig,
7223
- inverses,
7224
- flags);
7139
+ void printGenericSignature (GenericSignature genericSig, unsigned flags) {
7140
+ PrintAST (Printer, Options).printGenericSignature (genericSig, flags);
7225
7141
}
7226
7142
7227
7143
void printSubstitutions (SubstitutionMap subs) {
@@ -7241,17 +7157,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7241
7157
Printer.printStructurePost (PrintStructureKind::FunctionType);
7242
7158
};
7243
7159
7244
- SmallVector<InverseRequirement, 2 > inverses;
7245
- reconstituteInverses (T->getGenericSignature (),
7246
- T->getGenericParams (),
7247
- inverses);
7248
-
7249
7160
printFunctionExtInfo (T);
7250
7161
printGenericSignature (T->getGenericSignature (),
7251
- inverses,
7252
7162
PrintAST::PrintParams |
7253
- PrintAST::PrintRequirements |
7254
- PrintAST::CollapseDefaultReqs);
7163
+ PrintAST::PrintRequirements);
7255
7164
Printer << " " ;
7256
7165
7257
7166
visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ true );
@@ -7328,14 +7237,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7328
7237
printCalleeConvention (T->getCalleeConvention ());
7329
7238
7330
7239
if (GenericSignature sig = T->getInvocationGenericSignature ()) {
7331
- SmallVector<InverseRequirement, 2 > inverses;
7332
- reconstituteInverses (sig, sig.getGenericParams (), inverses);
7333
-
7334
7240
printGenericSignature (sig,
7335
- inverses,
7336
7241
PrintAST::PrintParams |
7337
- PrintAST::PrintRequirements |
7338
- PrintAST::CollapseDefaultReqs);
7242
+ PrintAST::PrintRequirements);
7339
7243
Printer << " " ;
7340
7244
}
7341
7245
@@ -7357,14 +7261,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7357
7261
7358
7262
GenericSignature sig = substitutions.getGenericSignature ();
7359
7263
7360
- SmallVector<InverseRequirement, 2 > inverses;
7361
- reconstituteInverses (sig, sig.getGenericParams (), inverses);
7362
-
7363
7264
sub->Printer << " @substituted " ;
7364
- sub->printGenericSignature (sig, inverses,
7265
+ sub->printGenericSignature (sig,
7365
7266
PrintAST::PrintParams |
7366
- PrintAST::PrintRequirements |
7367
- PrintAST::CollapseDefaultReqs);
7267
+ PrintAST::PrintRequirements);
7368
7268
sub->Printer << " " ;
7369
7269
}
7370
7270
@@ -7474,7 +7374,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7474
7374
// printer, but only the sub-Printer.
7475
7375
[&sub, T]{
7476
7376
if (auto sig = T->getLayout ()->getGenericSignature ()) {
7477
- sub.printGenericSignature (sig, /* inverses= */ {},
7377
+ sub.printGenericSignature (sig,
7478
7378
PrintAST::PrintParams | PrintAST::PrintRequirements);
7479
7379
sub.Printer << " " ;
7480
7380
}
@@ -8029,10 +7929,9 @@ void GenericSignature::print(ASTPrinter &Printer,
8029
7929
Printer << " <null>" ;
8030
7930
return ;
8031
7931
}
8032
- PrintAST (Printer, Opts).printGenericSignature (*this ,
8033
- /* inverses=*/ {},
8034
- PrintAST::PrintParams |
8035
- PrintAST::PrintRequirements);
7932
+
7933
+ PrintAST (Printer, Opts).printGenericSignature (
7934
+ *this , PrintAST::PrintParams | PrintAST::PrintRequirements);
8036
7935
}
8037
7936
8038
7937
void Requirement::print (raw_ostream &os, const PrintOptions &opts) const {
@@ -8230,7 +8129,7 @@ void ProtocolConformance::printName(llvm::raw_ostream &os,
8230
8129
StreamPrinter sPrinter (os);
8231
8130
TypePrinter typePrinter (sPrinter , PO);
8232
8131
typePrinter
8233
- .printGenericSignature (genericSig, /* inverses= */ {},
8132
+ .printGenericSignature (genericSig,
8234
8133
PrintAST::PrintParams |
8235
8134
PrintAST::PrintRequirements);
8236
8135
os << ' ' ;
0 commit comments