Skip to content

Commit 80c9997

Browse files
committed
NFC: introduce isAddingConformanceToInvertible
Removes duplicated code between Sema and the ASTPrinter.
1 parent fb0a1b9 commit 80c9997

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
@@ -1916,6 +1916,10 @@ class ExtensionDecl final : public GenericContext, public Decl,
19161916
/// \endcode
19171917
bool isWrittenWithConstraints() const;
19181918

1919+
/// Does this extension add conformance to an invertible protocol for the
1920+
/// extended type?
1921+
bool isAddingConformanceToInvertible() const;
1922+
19191923
/// If this extension represents an imported Objective-C category, returns the
19201924
/// category's name. Otherwise returns the empty identifier.
19211925
Identifier getObjCCategoryName() const;

lib/AST/ASTPrinter.cpp

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

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

lib/AST/Decl.cpp

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

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

lib/Sema/TypeCheckGeneric.cpp

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

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

815791
collectAdditionalExtensionRequirements(ext->getExtendedType(), extraReqs);
816792

0 commit comments

Comments
 (0)