Skip to content

Commit fcd467a

Browse files
committed
RequirementMachine: Factor out TypeDifference::getReplacementSubstitution()
1 parent 2c355de commit fcd467a

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -496,28 +496,6 @@ void RewritePathEvaluator::applyDecomposeConcrete(const RewriteStep &step,
496496

497497
auto substitutions = difference.LHS.getSubstitutions();
498498

499-
auto getReplacementSubstitution = [&](unsigned n) -> MutableTerm {
500-
for (const auto &pair : difference.SameTypes) {
501-
if (pair.first == n) {
502-
// Given a transformation Xn -> Xn', return the term Xn'.
503-
return MutableTerm(pair.second);
504-
}
505-
}
506-
507-
for (const auto &pair : difference.ConcreteTypes) {
508-
if (pair.first == n) {
509-
// Given a transformation Xn -> [concrete: D], return the
510-
// return Xn.[concrete: D].
511-
MutableTerm result(substitutions[n]);
512-
result.add(pair.second);
513-
return result;
514-
}
515-
}
516-
517-
// Otherwise return the original substitution Xn.
518-
return MutableTerm(substitutions[n]);
519-
};
520-
521499
if (!step.Inverse) {
522500
auto &term = getCurrentTerm();
523501

@@ -531,7 +509,7 @@ void RewritePathEvaluator::applyDecomposeConcrete(const RewriteStep &step,
531509
term = newTerm;
532510

533511
for (unsigned n : indices(substitutions))
534-
Primary.push_back(getReplacementSubstitution(n));
512+
Primary.push_back(difference.getReplacementSubstitution(n));
535513

536514
} else {
537515
unsigned numSubstitutions = substitutions.size();
@@ -541,7 +519,7 @@ void RewritePathEvaluator::applyDecomposeConcrete(const RewriteStep &step,
541519

542520
for (unsigned n : indices(substitutions)) {
543521
const auto &otherSubstitution = *(Primary.end() - numSubstitutions + n);
544-
auto expectedSubstitution = getReplacementSubstitution(n);
522+
auto expectedSubstitution = difference.getReplacementSubstitution(n);
545523
if (otherSubstitution != expectedSubstitution) {
546524
llvm::errs() << "Got: " << otherSubstitution << "\n";
547525
llvm::errs() << "Expected: " << expectedSubstitution << "\n";

lib/AST/RequirementMachine/TypeDifference.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@
2525
using namespace swift;
2626
using namespace rewriting;
2727

28+
MutableTerm TypeDifference::getReplacementSubstitution(unsigned index) const {
29+
for (const auto &pair : SameTypes) {
30+
if (pair.first == index) {
31+
// Given a transformation Xn -> Xn', return the term Xn'.
32+
return MutableTerm(pair.second);
33+
}
34+
}
35+
36+
for (const auto &pair : ConcreteTypes) {
37+
if (pair.first == index) {
38+
// Given a transformation Xn -> [concrete: D], return the
39+
// return Xn.[concrete: D].
40+
MutableTerm result(LHS.getSubstitutions()[index]);
41+
result.add(pair.second);
42+
return result;
43+
}
44+
}
45+
46+
// Otherwise return the original substitution Xn.
47+
return MutableTerm(LHS.getSubstitutions()[index]);
48+
}
49+
2850
void TypeDifference::dump(llvm::raw_ostream &out) const {
2951
llvm::errs() << "Base term: " << BaseTerm << "\n";
3052
llvm::errs() << "LHS: " << LHS << "\n";

lib/AST/RequirementMachine/TypeDifference.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct TypeDifference {
5858
: BaseTerm(baseTerm), LHS(lhs), RHS(rhs),
5959
SameTypes(sameTypes), ConcreteTypes(concreteTypes) {}
6060

61+
MutableTerm getReplacementSubstitution(unsigned index) const;
62+
6163
void dump(llvm::raw_ostream &out) const;
6264
void verify(RewriteContext &ctx) const;
6365
};

0 commit comments

Comments
 (0)