Skip to content

Commit 98beadf

Browse files
authored
Merge pull request #41833 from hborla/redundant-requirement-tweak
[RequirementMachine] Suppress redundant requirement warnings for inferred requirements.
2 parents 9fd97d5 + 6b64ac2 commit 98beadf

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ void RewriteSystem::propagateRedundantRequirementIDs() {
136136
MutableTerm lhs(rule.getLHS());
137137
for (auto ruleID : rewritePath.getRulesInEmptyContext(lhs, *this)) {
138138
auto &replacement = Rules[ruleID];
139-
if (!replacement.isPermanent() &&
140-
!replacement.getRequirementID().hasValue()) {
139+
if (!replacement.isPermanent()) {
140+
// If the replacement rule already has a requirementID, overwrite
141+
// it if the existing ID corresponds to an inferred requirement.
142+
// This effectively makes the inferred requirement the redundant
143+
// one, which makes it easier to suppress redundancy warnings for
144+
// inferred requirements later on.
145+
auto existingID = replacement.getRequirementID();
146+
if (existingID.hasValue() && !WrittenRequirements[*existingID].inferred)
147+
continue;
148+
141149
if (Debug.contains(DebugFlags::PropagateRequirementIDs)) {
142150
llvm::dbgs() << "\n- propagating ID = "
143151
<< requirementID

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,10 @@ void RewriteSystem::computeRedundantRequirementDiagnostics(
634634
auto requirement = WrittenRequirements[requirementID];
635635
auto pairIt = rulesPerRequirement.find(requirementID);
636636

637+
// Inferred requirements can be re-stated without warning.
638+
if (requirement.inferred)
639+
continue;
640+
637641
// If there are no rules for this structural requirement, then
638642
// the requirement was never added to the rewrite system because
639643
// it is trivially redundant.

lib/AST/RequirementMachine/Rule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class Rule final {
9797
}
9898

9999
void setRequirementID(Optional<unsigned> requirementID) {
100-
assert(!getRequirementID().hasValue());
101100
this->requirementID = requirementID;
102101
}
103102

test/Generics/requirement_machine_diagnostics.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,19 @@ class X3 { }
178178

179179
func sameTypeConcrete2<T : P9 & P10>(_: T) where T.B : X3, T.C == T.B, T.C == X3 { }
180180
// expected-warning@-1{{redundant superclass constraint 'T.B' : 'X3'}}
181+
182+
183+
// Redundant requirement warnings are suppressed for inferred requirements.
184+
185+
protocol P11 {
186+
associatedtype X
187+
associatedtype Y
188+
associatedtype Z
189+
}
190+
191+
func inferred1<T : Hashable>(_: Set<T>) {}
192+
func inferred2<T>(_: Set<T>) where T: Hashable {}
193+
func inferred3<T : P11>(_: T) where T.X : Hashable, T.Z == Set<T.Y>, T.X == T.Y {}
194+
func inferred4<T : P11>(_: T) where T.Z == Set<T.Y>, T.X : Hashable, T.X == T.Y {}
195+
func inferred5<T : P11>(_: T) where T.Z == Set<T.X>, T.Y : Hashable, T.X == T.Y {}
196+
func inferred6<T : P11>(_: T) where T.Y : Hashable, T.Z == Set<T.X>, T.X == T.Y {}

0 commit comments

Comments
 (0)