Skip to content

Commit 3c95cac

Browse files
committed
[RequirementMachine] Store a requirement ID for explicit rules in the
rewrite system. This ID can be used to index into the WrittenRequirements array in the rewrite system to retrieve the structural requirement, e.g. for the purpose of diagnostics.
1 parent b18e815 commit 3c95cac

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,21 @@ void RewriteSystem::propagateExplicitBits() {
195195
loop.findRulesAppearingOnceInEmptyContext(*this);
196196

197197
bool sawExplicitRule = false;
198+
Optional<unsigned> requirementID = None;
198199

199200
for (unsigned ruleID : rulesInEmptyContext) {
200201
const auto &rule = getRule(ruleID);
201-
if (rule.isExplicit())
202+
if (rule.isExplicit()) {
202203
sawExplicitRule = true;
204+
requirementID = rule.getRequirementID();
205+
break;
206+
}
203207
}
204208
if (sawExplicitRule) {
205209
for (unsigned ruleID : rulesInEmptyContext) {
206210
auto &rule = getRule(ruleID);
207211
if (!rule.isPermanent() && !rule.isExplicit())
208-
rule.markExplicit();
212+
rule.markExplicit(requirementID);
209213
}
210214
}
211215
}

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ bool RewriteSystem::addExplicitRule(MutableTerm lhs, MutableTerm rhs,
498498
Optional<unsigned> requirementID) {
499499
bool added = addRule(std::move(lhs), std::move(rhs));
500500
if (added) {
501-
Rules.back().markExplicit();
501+
Rules.back().markExplicit(requirementID);
502502
} else if (requirementID.hasValue()) {
503503
auto req = WrittenRequirements[requirementID.getValue()];
504504
Errors.push_back(
@@ -723,7 +723,15 @@ void RewriteSystem::verifyRewriteRules(ValidityPolicy policy) const {
723723
void RewriteSystem::dump(llvm::raw_ostream &out) const {
724724
out << "Rewrite system: {\n";
725725
for (const auto &rule : Rules) {
726-
out << "- " << rule << "\n";
726+
out << "- " << rule;
727+
if (auto ID = rule.getRequirementID()) {
728+
auto requirement = WrittenRequirements[*ID];
729+
out << ", ";
730+
requirement.req.dump(out);
731+
out << " at ";
732+
requirement.loc.print(out, Context.getASTContext().SourceMgr);
733+
}
734+
out << "\n";
727735
}
728736
out << "}\n";
729737
if (!Relations.empty()) {

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Rule final {
4646
Term LHS;
4747
Term RHS;
4848

49+
/// The written requirement ID, which can be used to index into the
50+
/// \c WrittenRequirements array in the rewrite system to retrieve
51+
/// the structural requirement.
52+
Optional<unsigned> requirementID;
53+
4954
/// A 'permanent' rule cannot be deleted by homotopy reduction. These
5055
/// do not correspond to generic requirements and are re-added when the
5156
/// rewrite system is built.
@@ -98,6 +103,10 @@ class Rule final {
98103
const Term &getLHS() const { return LHS; }
99104
const Term &getRHS() const { return RHS; }
100105

106+
Optional<unsigned> getRequirementID() const {
107+
return requirementID;
108+
}
109+
101110
Optional<Symbol> isPropertyRule() const;
102111

103112
const ProtocolDecl *isProtocolConformanceRule() const;
@@ -167,9 +176,10 @@ class Rule final {
167176
Permanent = true;
168177
}
169178

170-
void markExplicit() {
179+
void markExplicit(Optional<unsigned> requirementID) {
171180
assert(!Explicit && !Permanent &&
172181
"Permanent and explicit are mutually exclusive");
182+
this->requirementID = requirementID;
173183
Explicit = true;
174184
}
175185

0 commit comments

Comments
 (0)