Skip to content

Commit 94425eb

Browse files
committed
NFC: introduce isAddingConformanceToInvertible
Removes duplicated code between Sema and the ASTPrinter. (cherry picked from commit 80c9997)
1 parent 6985590 commit 94425eb

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,10 @@ class ExtensionDecl final : public GenericContext, public Decl,
19121912
/// \endcode
19131913
bool isWrittenWithConstraints() const;
19141914

1915+
/// Does this extension add conformance to an invertible protocol for the
1916+
/// extended type?
1917+
bool isAddingConformanceToInvertible() const;
1918+
19151919
/// Returns the name of the category specified by the \c \@_objcImplementation
19161920
/// attribute, or \c None if the name is invalid or
19171921
/// \c isObjCImplementation() is false.

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,20 +2913,6 @@ void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
29132913
printTypeLoc(TypeLoc(ExtendedTypeLoc.getTypeRepr(), Ty));
29142914
}
29152915

2916-
/// If an extension adds a conformance for an invertible protocol, then we
2917-
/// should not print inverses for its generic signature, because no conditional
2918-
/// requirements are inferred by default for such an extension.
2919-
static bool isExtensionAddingInvertibleConformance(const ExtensionDecl *ext) {
2920-
auto conformances = ext->getLocalConformances();
2921-
for (auto *conf : conformances) {
2922-
if (conf->getProtocol()->getInvertibleProtocolKind()) {
2923-
assert(conformances.size() == 1 && "expected solo conformance");
2924-
return true;
2925-
}
2926-
}
2927-
return false;
2928-
}
2929-
29302916
void PrintAST::printSynthesizedExtension(Type ExtendedType,
29312917
ExtensionDecl *ExtDecl) {
29322918
if (Options.PrintCompatibilityFeatureChecks &&
@@ -3058,7 +3044,7 @@ void PrintAST::printExtension(ExtensionDecl *decl) {
30583044
// for an invertible protocol itself, as we do not infer any requirements
30593045
// in such an extension. We need to print the whole signature:
30603046
// extension S: Copyable where T: Copyable
3061-
if (isExtensionAddingInvertibleConformance(decl)) {
3047+
if (decl->isAddingConformanceToInvertible()) {
30623048
genSigFlags &= ~PrintInverseRequirements;
30633049
genSigFlags &= ~IncludeOuterInverses;
30643050
}

lib/AST/Decl.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,30 @@ Type ExtensionDecl::getExtendedType() const {
18941894
return ErrorType::get(ctx);
18951895
}
18961896

1897+
bool ExtensionDecl::isAddingConformanceToInvertible() const {
1898+
const unsigned numEntries = getInherited().size();
1899+
for (unsigned i = 0; i < numEntries; ++i) {
1900+
InheritedTypeRequest request{this, i, TypeResolutionStage::Structural};
1901+
auto result = evaluateOrDefault(getASTContext().evaluator, request,
1902+
InheritedTypeResult::forDefault());
1903+
Type inheritedTy;
1904+
switch (result) {
1905+
case InheritedTypeResult::Inherited:
1906+
inheritedTy = result.getInheritedType();
1907+
break;
1908+
case InheritedTypeResult::Suppressed:
1909+
case InheritedTypeResult::Default:
1910+
continue;
1911+
}
1912+
1913+
if (inheritedTy)
1914+
if (auto kp = inheritedTy->getKnownProtocol())
1915+
if (getInvertibleProtocolKind(*kp))
1916+
return true;
1917+
}
1918+
return false;
1919+
}
1920+
18971921
bool Decl::isObjCImplementation() const {
18981922
return getAttrs().hasAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
18991923
}

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -785,34 +785,10 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
785785
} else if (auto *ext = dyn_cast<ExtensionDecl>(GC)) {
786786
loc = ext->getLoc();
787787

788-
// The inherited entries influence the generic signature of the extension,
789-
// because if it introduces conformance to invertible protocol IP, we do not
790-
// we do not infer any requirements that the generic parameters to conform
788+
// If the extension introduces conformance to invertible protocol IP, do not
789+
// infer any conditional requirements that the generic parameters to conform
791790
// to invertible protocols. This forces people to write out the conditions.
792-
const unsigned numEntries = ext->getInherited().size();
793-
for (unsigned i = 0; i < numEntries; ++i) {
794-
InheritedTypeRequest request{ext, i, TypeResolutionStage::Structural};
795-
auto result = evaluateOrDefault(ctx.evaluator, request,
796-
InheritedTypeResult::forDefault());
797-
Type inheritedTy;
798-
switch (result) {
799-
case InheritedTypeResult::Inherited:
800-
inheritedTy = result.getInheritedType();
801-
break;
802-
case InheritedTypeResult::Suppressed:
803-
case InheritedTypeResult::Default:
804-
continue;
805-
}
806-
807-
if (inheritedTy) {
808-
if (auto kp = inheritedTy->getKnownProtocol()) {
809-
if (getInvertibleProtocolKind(*kp)) {
810-
inferInvertibleReqs = false;
811-
break;
812-
}
813-
}
814-
}
815-
}
791+
inferInvertibleReqs = !ext->isAddingConformanceToInvertible();
816792

817793
collectAdditionalExtensionRequirements(ext->getExtendedType(), extraReqs);
818794

0 commit comments

Comments
 (0)