@@ -943,6 +943,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
943
943
InnermostOnly = 4 ,
944
944
SwapSelfAndDependentMemberType = 8 ,
945
945
PrintInherited = 16 ,
946
+ CollapseDefaultReqs = 32 ,
946
947
};
947
948
948
949
void printInheritedFromRequirementSignature (ProtocolDecl *proto,
@@ -1561,7 +1562,7 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1561
1562
Decl *attachingTo) {
1562
1563
printGenericSignature (
1563
1564
proto->getRequirementSignatureAsGenericSignature (),
1564
- PrintInherited,
1565
+ PrintInherited | CollapseDefaultReqs ,
1565
1566
[&](const Requirement &req) {
1566
1567
// Skip the inferred 'Self : AnyObject' constraint if this is an
1567
1568
// @objc protocol.
@@ -1580,7 +1581,7 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1580
1581
1581
1582
void PrintAST::printWhereClauseFromRequirementSignature (ProtocolDecl *proto,
1582
1583
Decl *attachingTo) {
1583
- unsigned flags = PrintRequirements;
1584
+ unsigned flags = PrintRequirements | CollapseDefaultReqs ;
1584
1585
if (isa<AssociatedTypeDecl>(attachingTo))
1585
1586
flags |= SwapSelfAndDependentMemberType;
1586
1587
printGenericSignature (
@@ -1684,6 +1685,50 @@ void PrintAST::printGenericSignature(
1684
1685
}
1685
1686
}
1686
1687
1688
+ // / \returns the invertible protocol kind iff this requirement was expanded as
1689
+ // / a default conformance to an invertible protocol.
1690
+ static llvm::Optional<InvertibleProtocolKind>
1691
+ extractDefaultedInvertible (const Requirement &req) {
1692
+ if (req.getKind () != RequirementKind::Conformance)
1693
+ return llvm::None;
1694
+
1695
+ auto constraintTy = req.getSecondType ();
1696
+ if (auto kp = constraintTy->getKnownProtocol ())
1697
+ return getInvertibleProtocolKind (*kp);
1698
+
1699
+ return llvm::None;
1700
+ }
1701
+
1702
+ // / Undoes what expandDefaultRequirements does during requirement desugaring
1703
+ // / so that inverses are emitted in interface files and the default requirements
1704
+ // / are cleaned-up.
1705
+ static
1706
+ void collapseDefaultRequirements (ArrayRef<GenericTypeParamType *> genericParams,
1707
+ SmallVectorImpl<Requirement> &reqs) {
1708
+ // Tracks which default conformance requirements were encountered and removed.
1709
+ llvm::SmallDenseMap<CanType, InvertibleProtocolSet> state;
1710
+ for (auto *gtpt : genericParams)
1711
+ state[gtpt->getCanonicalType ()] = InvertibleProtocolSet::full ();
1712
+
1713
+ reqs.erase (llvm::remove_if (reqs, [&](Requirement req) {
1714
+ auto invertible = extractDefaultedInvertible (req);
1715
+ if (!invertible)
1716
+ return false ;
1717
+
1718
+ state[req.getFirstType ()->getCanonicalType ()].remove (*invertible);
1719
+ return true ;
1720
+ }), reqs.end ());
1721
+
1722
+ // The remaining inverses are added as requirements.
1723
+ for (auto *gtpt : genericParams) {
1724
+ auto neededInverses = state[gtpt->getCanonicalType ()];
1725
+ for (auto ip : neededInverses) {
1726
+ reqs.push_back ({RequirementKind::Layout, gtpt,
1727
+ LayoutConstraint::getInverseConstraint (ip)});
1728
+ }
1729
+ }
1730
+ }
1731
+
1687
1732
void PrintAST::printSingleDepthOfGenericSignature (
1688
1733
ArrayRef<GenericTypeParamType *> genericParams,
1689
1734
ArrayRef<Requirement> requirements, unsigned flags,
@@ -1695,14 +1740,20 @@ void PrintAST::printSingleDepthOfGenericSignature(
1695
1740
1696
1741
void PrintAST::printSingleDepthOfGenericSignature (
1697
1742
ArrayRef<GenericTypeParamType *> genericParams,
1698
- ArrayRef<Requirement> requirements , bool &isFirstReq, unsigned flags,
1743
+ ArrayRef<Requirement> origRequirements , bool &isFirstReq, unsigned flags,
1699
1744
llvm::function_ref<bool (const Requirement &)> filter) {
1700
1745
bool printParams = (flags & PrintParams);
1701
1746
bool printRequirements = (flags & PrintRequirements);
1702
1747
printRequirements &= Options.PrintGenericRequirements ;
1703
1748
bool printInherited = (flags & PrintInherited);
1704
1749
bool swapSelfAndDependentMemberType =
1705
1750
(flags & SwapSelfAndDependentMemberType);
1751
+ bool collapseDefaults = (flags & CollapseDefaultReqs);
1752
+
1753
+ // Remove default requirements and reconstitute inverses.
1754
+ SmallVector<Requirement, 8 > requirements (origRequirements);
1755
+ if (collapseDefaults)
1756
+ collapseDefaultRequirements (genericParams, requirements);
1706
1757
1707
1758
unsigned typeContextDepth = 0 ;
1708
1759
SubstitutionMap subMap;
@@ -2567,7 +2618,7 @@ void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
2567
2618
return ;
2568
2619
2569
2620
Printer.printStructurePre (PrintStructureKind::DeclGenericParameterClause);
2570
- printGenericSignature (genericSig, PrintRequirements,
2621
+ printGenericSignature (genericSig, PrintRequirements | CollapseDefaultReqs ,
2571
2622
[parentSig](const Requirement &req) {
2572
2623
if (parentSig)
2573
2624
return !parentSig->isRequirementSatisfied (req);
@@ -2722,7 +2773,8 @@ void PrintAST::printSynthesizedExtensionImpl(Type ExtendedType,
2722
2773
auto Sig = ED->getGenericSignature ();
2723
2774
printSingleDepthOfGenericSignature (Sig.getGenericParams (),
2724
2775
Sig.getRequirements (),
2725
- IsFirst, PrintRequirements,
2776
+ IsFirst,
2777
+ PrintRequirements | CollapseDefaultReqs,
2726
2778
[](const Requirement &Req){
2727
2779
return true ;
2728
2780
});
@@ -2820,6 +2872,9 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
2820
2872
auto baseGenericSig = decl->getExtendedNominal ()->getGenericSignature ();
2821
2873
assert (baseGenericSig &&
2822
2874
" an extension can't be generic if the base type isn't" );
2875
+ // NOTE: We do _not_ CollapseDefaultReqs for the where-clause of an
2876
+ // extension, because conditions involving Copyable here are not
2877
+ // automatically inferred based on the extension itself.
2823
2878
printGenericSignature (genericSig, PrintRequirements,
2824
2879
[baseGenericSig](const Requirement &req) -> bool {
2825
2880
// Only include constraints that are not satisfied by the base type.
@@ -6969,7 +7024,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
6969
7024
printFunctionExtInfo (T);
6970
7025
printGenericSignature (T->getGenericSignature (),
6971
7026
PrintAST::PrintParams |
6972
- PrintAST::PrintRequirements);
7027
+ PrintAST::PrintRequirements |
7028
+ PrintAST::CollapseDefaultReqs);
6973
7029
Printer << " " ;
6974
7030
6975
7031
visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ true );
0 commit comments