@@ -966,6 +966,22 @@ class PrintAST : public ASTVisitor<PrintAST> {
966
966
IncludeOuterInverses = 64 ,
967
967
};
968
968
969
+ // / The default generic signature flags for printing requirements.
970
+ unsigned defaultGenericRequirementFlags () const {
971
+ return defaultGenericRequirementFlags (Options);
972
+ }
973
+
974
+ // / The default generic signature flags for printing requirements.
975
+ static unsigned
976
+ defaultGenericRequirementFlags (const PrintOptions &options) {
977
+ unsigned flags = PrintRequirements;
978
+
979
+ if (!options.SuppressNoncopyableGenerics )
980
+ flags |= PrintInverseRequirements;
981
+
982
+ return flags;
983
+ }
984
+
969
985
void printInheritedFromRequirementSignature (ProtocolDecl *proto,
970
986
TypeDecl *attachingTo);
971
987
void printWhereClauseFromRequirementSignature (ProtocolDecl *proto,
@@ -1617,7 +1633,8 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1617
1633
// inheritance clause, because they do not gain any default requirements.
1618
1634
// HACK: also exclude Sendable from getting inverses printed.
1619
1635
if (!proto->getInvertibleProtocolKind ()
1620
- && !proto->isSpecificProtocol (KnownProtocolKind::Sendable))
1636
+ && !proto->isSpecificProtocol (KnownProtocolKind::Sendable) &&
1637
+ !Options.SuppressNoncopyableGenerics )
1621
1638
flags |= PrintInverseRequirements;
1622
1639
1623
1640
printRequirementSignature (
@@ -1628,7 +1645,7 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
1628
1645
1629
1646
void PrintAST::printWhereClauseFromRequirementSignature (ProtocolDecl *proto,
1630
1647
TypeDecl *attachingTo) {
1631
- unsigned flags = PrintRequirements | PrintInverseRequirements ;
1648
+ unsigned flags = defaultGenericRequirementFlags () ;
1632
1649
if (isa<AssociatedTypeDecl>(attachingTo))
1633
1650
flags |= SwapSelfAndDependentMemberType;
1634
1651
printRequirementSignature (proto, proto->getRequirementSignature (), flags,
@@ -1670,6 +1687,21 @@ void PrintAST::printGenericSignature(GenericSignature genericSig,
1670
1687
[&](const Requirement &) { return true ; });
1671
1688
}
1672
1689
1690
+ // Erase any requirements involving invertible protocols.
1691
+ static void eraseInvertibleProtocolConformances (
1692
+ SmallVectorImpl<Requirement> &requirements) {
1693
+ llvm::erase_if (requirements, [&](Requirement req) {
1694
+ if (req.getKind () == RequirementKind::Conformance) {
1695
+ if (auto protoType = req.getSecondType ()->getAs <ProtocolType>()) {
1696
+ auto proto = protoType->getDecl ();
1697
+ return proto->getInvertibleProtocolKind ().has_value ();
1698
+ }
1699
+ }
1700
+
1701
+ return false ;
1702
+ });
1703
+ }
1704
+
1673
1705
void PrintAST::printGenericSignature (
1674
1706
GenericSignature genericSig,
1675
1707
unsigned flags,
@@ -1683,6 +1715,9 @@ void PrintAST::printGenericSignature(
1683
1715
} else {
1684
1716
requirements.append (genericSig.getRequirements ().begin (),
1685
1717
genericSig.getRequirements ().end ());
1718
+
1719
+ if (Options.SuppressNoncopyableGenerics )
1720
+ eraseInvertibleProtocolConformances (requirements);
1686
1721
}
1687
1722
1688
1723
// Unless `IncludeOuterInverses` is enabled, limit inverses to the
@@ -1976,6 +2011,9 @@ void PrintAST::printRequirementSignature(ProtocolDecl *owner,
1976
2011
} else {
1977
2012
requirements.append (sig.getRequirements ().begin (),
1978
2013
sig.getRequirements ().end ());
2014
+
2015
+ if (Options.SuppressNoncopyableGenerics )
2016
+ eraseInvertibleProtocolConformances (requirements);
1979
2017
}
1980
2018
1981
2019
if (attachingTo) {
@@ -2724,7 +2762,7 @@ void PrintAST::printDeclGenericRequirements(GenericContext *decl) {
2724
2762
if (parentSig && parentSig->isEqual (genericSig))
2725
2763
return ;
2726
2764
2727
- unsigned flags = PrintRequirements | PrintInverseRequirements ;
2765
+ unsigned flags = defaultGenericRequirementFlags () ;
2728
2766
2729
2767
// In many cases, inverses should not be printed for outer generic parameters.
2730
2768
// Exceptions to that include extensions, as it's valid to write an inverse
@@ -2892,14 +2930,15 @@ void PrintAST::printSynthesizedExtensionImpl(Type ExtendedType,
2892
2930
SmallVector<InverseRequirement, 2 > inverses;
2893
2931
auto Sig = ED->getGenericSignature ();
2894
2932
Sig->getRequirementsWithInverses (requirements, inverses);
2895
- printSingleDepthOfGenericSignature (Sig.getGenericParams (),
2896
- requirements,
2897
- inverses,
2898
- IsFirst,
2899
- PrintRequirements | PrintInverseRequirements,
2900
- [](const Requirement &Req){
2901
- return true ;
2902
- });
2933
+ printSingleDepthOfGenericSignature (
2934
+ Sig.getGenericParams (),
2935
+ requirements,
2936
+ inverses,
2937
+ IsFirst,
2938
+ PrintAST::defaultGenericRequirementFlags (Options),
2939
+ [](const Requirement &Req){
2940
+ return true ;
2941
+ });
2903
2942
};
2904
2943
2905
2944
auto printCombinedRequirementsIfNeeded = [&]() -> bool {
@@ -2995,8 +3034,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
2995
3034
assert (baseGenericSig &&
2996
3035
" an extension can't be generic if the base type isn't" );
2997
3036
printGenericSignature (genericSig,
2998
- PrintRequirements
2999
- | PrintInverseRequirements
3037
+ defaultGenericRequirementFlags ()
3000
3038
| IncludeOuterInverses,
3001
3039
[baseGenericSig](const Requirement &req) -> bool {
3002
3040
// Only include constraints that are not satisfied by the base type.
@@ -3099,6 +3137,14 @@ static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
3099
3137
options.ExcludeAttrList .resize (originalExcludeAttrCount);
3100
3138
}
3101
3139
3140
+ static void suppressingFeatureNoncopyableGenerics (
3141
+ PrintOptions &options,
3142
+ llvm::function_ref<void ()> action) {
3143
+ llvm::SaveAndRestore<bool > scope (
3144
+ options.SuppressNoncopyableGenerics , true );
3145
+ action ();
3146
+ }
3147
+
3102
3148
// / Suppress the printing of a particular feature.
3103
3149
static void suppressingFeature (PrintOptions &options, Feature feature,
3104
3150
llvm::function_ref<void ()> action) {
@@ -6609,8 +6655,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
6609
6655
printFunctionExtInfo (T);
6610
6656
printGenericSignature (T->getGenericSignature (),
6611
6657
PrintAST::PrintParams |
6612
- PrintAST::PrintRequirements |
6613
- PrintAST::PrintInverseRequirements);
6658
+ PrintAST::defaultGenericRequirementFlags (Options));
6614
6659
Printer << " " ;
6615
6660
6616
6661
visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ true );
@@ -6696,8 +6741,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
6696
6741
if (GenericSignature sig = T->getInvocationGenericSignature ()) {
6697
6742
printGenericSignature (sig,
6698
6743
PrintAST::PrintParams |
6699
- PrintAST::PrintRequirements |
6700
- PrintAST::PrintInverseRequirements);
6744
+ PrintAST::defaultGenericRequirementFlags (Options));
6701
6745
Printer << " " ;
6702
6746
}
6703
6747
@@ -6833,12 +6877,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
6833
6877
6834
6878
// Capture list used here to ensure we don't print anything using `this`
6835
6879
// printer, but only the sub-Printer.
6836
- [&sub, T]{
6880
+ [&sub, T, options=Options ]{
6837
6881
if (auto sig = T->getLayout ()->getGenericSignature ()) {
6838
6882
sub.printGenericSignature (sig,
6839
6883
PrintAST::PrintParams |
6840
- PrintAST::PrintRequirements |
6841
- PrintAST::PrintInverseRequirements);
6884
+ PrintAST::defaultGenericRequirementFlags (options));
6842
6885
sub.Printer << " " ;
6843
6886
}
6844
6887
sub.Printer << " {" ;
@@ -7394,8 +7437,8 @@ void GenericSignature::print(ASTPrinter &Printer,
7394
7437
}
7395
7438
7396
7439
auto flags = PrintAST::PrintParams | PrintAST::PrintRequirements;
7397
- if (Opts.PrintInverseRequirements )
7398
- flags |= PrintAST::PrintInverseRequirements;
7440
+ if (Opts.PrintInverseRequirements && !Opts. SuppressNoncopyableGenerics )
7441
+ flags |= PrintAST::PrintInverseRequirements;
7399
7442
PrintAST (Printer, Opts).printGenericSignature (*this , flags);
7400
7443
}
7401
7444
@@ -7410,8 +7453,8 @@ void RequirementSignature::print(ProtocolDecl *owner,
7410
7453
ASTPrinter &Printer,
7411
7454
const PrintOptions &Opts) const {
7412
7455
auto flags = PrintAST::PrintParams | PrintAST::PrintRequirements;
7413
- if (Opts.PrintInverseRequirements )
7414
- flags |= PrintAST::PrintInverseRequirements;
7456
+ if (Opts.PrintInverseRequirements && !Opts. SuppressNoncopyableGenerics )
7457
+ flags |= PrintAST::PrintInverseRequirements;
7415
7458
PrintAST (Printer, Opts).printRequirementSignature (owner, *this , flags, nullptr );
7416
7459
}
7417
7460
@@ -7615,10 +7658,10 @@ void ProtocolConformance::printName(llvm::raw_ostream &os,
7615
7658
StreamPrinter sPrinter (os);
7616
7659
TypePrinter typePrinter (sPrinter , PO);
7617
7660
typePrinter
7618
- .printGenericSignature (genericSig,
7619
- PrintAST::PrintParams |
7620
- PrintAST::PrintRequirements |
7621
- PrintAST::PrintInverseRequirements );
7661
+ .printGenericSignature (
7662
+ genericSig,
7663
+ PrintAST::PrintParams |
7664
+ PrintAST::defaultGenericRequirementFlags (PO) );
7622
7665
os << ' ' ;
7623
7666
}
7624
7667
}
@@ -7787,6 +7830,26 @@ swift::getInheritedForPrinting(
7787
7830
});
7788
7831
if (foundUnprintable)
7789
7832
continue ;
7833
+
7834
+ // Suppress Copyable and ~Copyable.
7835
+ if (options.SuppressNoncopyableGenerics ) {
7836
+ if (auto pct = ty->getAs <ProtocolCompositionType>()) {
7837
+ auto inverses = pct->getInverses ();
7838
+ if (inverses.contains (InvertibleProtocolKind::Copyable)) {
7839
+ inverses.remove (InvertibleProtocolKind::Copyable);
7840
+ ty = ProtocolCompositionType::get (decl->getASTContext (),
7841
+ pct->getMembers (),
7842
+ inverses,
7843
+ pct->hasExplicitAnyObject ());
7844
+ if (ty->isAny ())
7845
+ continue ;
7846
+ }
7847
+ }
7848
+
7849
+ if (auto protoTy = ty->getAs <ProtocolType>())
7850
+ if (protoTy->getDecl ()->isSpecificProtocol (KnownProtocolKind::Copyable))
7851
+ continue ;
7852
+ }
7790
7853
}
7791
7854
7792
7855
Results.push_back (inherited.getEntry (i));
0 commit comments