Skip to content

Commit 223944b

Browse files
committed
RequirementMachine: Don't diagnose trivial circularity 'protocol P : P' as redundant
Every protocol gets an 'identity conformance' rule [P].[P] => [P]. A trivially-stated circularity is always redundant because of this rule, and we diagnose circular inheritance elsewhere as a hard error, so just add a special case to skip adding such a rule here to avoid the useless warning on top of the existing error.
1 parent 7f460d6 commit 223944b

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,14 @@ void swift::rewriting::realizeInheritedRequirements(
649649
Type());
650650
if (!inheritedType) continue;
651651

652+
// Ignore trivially circular protocol refinement (protocol P : P)
653+
// since we diagnose that elsewhere. Adding a rule here would emit
654+
// a useless redundancy warning.
655+
if (auto *protoDecl = dyn_cast<ProtocolDecl>(decl)) {
656+
if (inheritedType->isEqual(protoDecl->getDeclaredInterfaceType()))
657+
continue;
658+
}
659+
652660
auto *typeRepr = inheritedTypes[index].getTypeRepr();
653661
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc() : SourceLoc());
654662
if (shouldInferRequirements) {

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ void RewriteSystem::computeRedundantRequirementDiagnostics(
761761

762762
// If all rules derived from this structural requirement are redundant,
763763
// then the requirement is unnecessary in the source code.
764+
//
765+
// This means the rules derived from this requirement were all
766+
// determined to be redundant by homotopy reduction.
764767
const auto &ruleIDs = pairIt->second;
765768
if (llvm::all_of(ruleIDs, isRedundantRule)) {
766769
auto requirement = WrittenRequirements[requirementID];

test/decl/class/circular_inheritance.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
22

33
class Left // expected-error {{'Left' inherits from itself}} expected-note {{through reference here}}
44
: Right.Hand { // expected-note {{through reference here}}

test/decl/protocol/conforms/circular_validation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
22

33
// With a bit of effort, we could make this work -- but for now, let's just
44
// not crash.

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-objc-interop -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
1+
// RUN: %target-typecheck-verify-swift -enable-objc-interop -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
22
protocol EmptyProtocol { }
33

44
protocol DefinitionsInProtocols {

0 commit comments

Comments
 (0)