Skip to content

Commit bdbf50e

Browse files
committed
RequirementMachine: Replace RewriteStep::SameTypeWitness with a relation
1 parent 6064671 commit bdbf50e

File tree

7 files changed

+42
-114
lines changed

7 files changed

+42
-114
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ RewriteLoop::findRulesAppearingOnceInEmptyContext(
9898
case RewriteStep::Shift:
9999
case RewriteStep::Decompose:
100100
case RewriteStep::Relation:
101-
case RewriteStep::SameTypeWitness:
102101
break;
103102
}
104103

@@ -218,7 +217,6 @@ RewritePath RewritePath::splitCycleAtRule(unsigned ruleID) const {
218217
case RewriteStep::Shift:
219218
case RewriteStep::Decompose:
220219
case RewriteStep::Relation:
221-
case RewriteStep::SameTypeWitness:
222220
break;
223221
}
224222

@@ -316,7 +314,6 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
316314
case RewriteStep::Shift:
317315
case RewriteStep::Decompose:
318316
case RewriteStep::Relation:
319-
case RewriteStep::SameTypeWitness:
320317
newSteps.push_back(step);
321318
break;
322319
}

lib/AST/RequirementMachine/MinimalConformances.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ void RewriteLoop::findProtocolConformanceRules(
138138
case RewriteStep::Shift:
139139
case RewriteStep::Decompose:
140140
case RewriteStep::Relation:
141-
case RewriteStep::SameTypeWitness:
142141
break;
143142
}
144143
}

lib/AST/RequirementMachine/PropertyRelations.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,33 @@ unsigned RewriteSystem::recordConcreteTypeWitnessRelation(
118118
Term::get(rhsTerm, Context));
119119
}
120120

121+
/// Record a relation ([concrete: C : P].[P:X].[concrete: C] =>
122+
/// [concrete: C : P].[P:X]) which "ties off" a nested type C.X that is
123+
/// equivalent to C.
124+
unsigned RewriteSystem::recordSameTypeWitnessRelation(
125+
Symbol concreteConformanceSymbol,
126+
Symbol associatedTypeSymbol) {
127+
assert(concreteConformanceSymbol.getKind() ==
128+
Symbol::Kind::ConcreteConformance);
129+
assert(associatedTypeSymbol.getKind() ==
130+
Symbol::Kind::AssociatedType);
131+
132+
MutableTerm rhsTerm;
133+
rhsTerm.add(concreteConformanceSymbol);
134+
135+
auto concreteSymbol = Symbol::forConcreteType(
136+
concreteConformanceSymbol.getConcreteType(),
137+
concreteConformanceSymbol.getSubstitutions(),
138+
Context);
139+
MutableTerm lhsTerm(rhsTerm);
140+
lhsTerm.add(associatedTypeSymbol);
141+
lhsTerm.add(concreteSymbol);
142+
143+
return recordRelation(
144+
Term::get(lhsTerm, Context),
145+
Term::get(rhsTerm, Context));
146+
}
147+
121148
RewriteSystem::TypeWitness::TypeWitness(
122149
Term lhs, llvm::PointerUnion<Symbol, Term> rhs)
123150
: LHS(lhs), RHS(rhs) {

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,15 +894,11 @@ MutableTerm PropertyMap::computeConstraintTermForTypeWitness(
894894
auto typeWitnessSymbol =
895895
Symbol::forConcreteType(typeWitnessSchema, result, Context);
896896

897-
RewriteSystem::TypeWitness witness(Term::get(subjectType, Context),
898-
typeWitnessSymbol);
899-
unsigned witnessID = System.recordTypeWitness(witness);
900-
901897
auto concreteConformanceSymbol = *(subjectType.end() - 2);
902898
auto associatedTypeSymbol = *(subjectType.end() - 1);
903899

904900
// Record the relation before simplifying typeWitnessSymbol below.
905-
unsigned relationID = System.recordConcreteTypeWitnessRelation(
901+
unsigned concreteRelationID = System.recordConcreteTypeWitnessRelation(
906902
concreteConformanceSymbol,
907903
associatedTypeSymbol,
908904
typeWitnessSymbol);
@@ -925,16 +921,21 @@ MutableTerm PropertyMap::computeConstraintTermForTypeWitness(
925921
MutableTerm result(key);
926922
result.add(concreteConformanceSymbol);
927923

924+
unsigned sameRelationID = System.recordSameTypeWitnessRelation(
925+
concreteConformanceSymbol,
926+
associatedTypeSymbol);
927+
928928
// ([concrete: C : P] => [concrete: C : P].[P:X].[concrete: C])
929-
path.add(RewriteStep::forSameTypeWitness(
930-
witnessID, /*inverse=*/true));
929+
path.add(RewriteStep::forRelation(
930+
/*startOffset=*/key.size(), sameRelationID,
931+
/*inverse=*/true));
931932

932933
// [concrete: C : P].[P:X].([concrete: C] => [concrete: C.X])
933934
path.append(substPath);
934935

935936
// T.([concrete: C : P].[P:X].[concrete: C.X] => [concrete: C : P].[P:X])
936937
path.add(RewriteStep::forRelation(
937-
/*startOffset=*/key.size(), relationID,
938+
/*startOffset=*/key.size(), concreteRelationID,
938939
/*inverse=*/false));
939940

940941
return result;
@@ -955,7 +956,7 @@ MutableTerm PropertyMap::computeConstraintTermForTypeWitness(
955956

956957
// T.([concrete: C : P].[P:X].[concrete: C.X] => [concrete: C : P].[P:X])
957958
path.add(RewriteStep::forRelation(
958-
/*startOffset=*/key.size(), relationID,
959+
/*startOffset=*/key.size(), concreteRelationID,
959960
/*inverse=*/false));
960961

961962
return constraintType;

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ void RewriteStep::dump(llvm::raw_ostream &out,
7979

8080
break;
8181
}
82-
case SameTypeWitness: {
83-
evaluator.applySameTypeWitness(*this, system);
84-
85-
out << (Inverse ? "SameTypeWitness⁻¹"
86-
: "SameTypeWitness");
87-
break;
88-
}
8982
}
9083
}
9184

@@ -382,78 +375,6 @@ RewritePathEvaluator::applyRelation(const RewriteStep &step,
382375
return {lhs, rhs, prefix, suffix};
383376
}
384377

385-
void RewritePathEvaluator::applySameTypeWitness(const RewriteStep &step,
386-
const RewriteSystem &system) {
387-
checkPrimary();
388-
auto &term = Primary.back();
389-
390-
const auto &witness = system.getTypeWitness(step.Arg);
391-
auto fail = [&]() {
392-
llvm::errs() << "Bad same-type witness term:\n";
393-
llvm::errs() << term << "\n\n";
394-
witness.dump(llvm::errs());
395-
abort();
396-
};
397-
398-
auto concreteConformanceSymbol = witness.getConcreteConformance();
399-
400-
#ifndef NDEBUG
401-
if (witness.getConcreteType().getConcreteType() !=
402-
concreteConformanceSymbol.getConcreteType()) {
403-
fail();
404-
}
405-
#endif
406-
407-
auto witnessConcreteType = Symbol::forConcreteType(
408-
concreteConformanceSymbol.getConcreteType(),
409-
concreteConformanceSymbol.getSubstitutions(),
410-
system.getRewriteContext());
411-
412-
if (!step.Inverse) {
413-
// Make sure the term takes the following form, where |V| == EndOffset:
414-
//
415-
// U.[concrete: C : P].[P:X].[concrete: C].V
416-
if (term.size() <= step.EndOffset + 3 ||
417-
*(term.end() - step.EndOffset - 3) != concreteConformanceSymbol ||
418-
*(term.end() - step.EndOffset - 2) != witness.getAssocType() ||
419-
*(term.end() - step.EndOffset - 1) != witnessConcreteType) {
420-
fail();
421-
}
422-
423-
// Get the subterm U.[concrete: C : P].
424-
MutableTerm newTerm(term.begin(), term.end() - step.EndOffset - 2);
425-
426-
// Add the subterm V, to get U.[concrete: C : P].V.
427-
newTerm.append(term.end() - step.EndOffset, term.end());
428-
429-
term = newTerm;
430-
} else {
431-
// Make sure the term takes the following form, where |V| == EndOffset:
432-
//
433-
// U.[concrete: C : P].V
434-
if (term.size() <= step.EndOffset + 1 ||
435-
*(term.end() - step.EndOffset - 1) != concreteConformanceSymbol) {
436-
fail();
437-
}
438-
439-
// Get the subterm U.[concrete: C : P].
440-
MutableTerm newTerm(term.begin(), term.end() - step.EndOffset);
441-
442-
// Add the symbol [P:X].
443-
newTerm.add(witness.getAssocType());
444-
445-
// Add the symbol [concrete: C].
446-
newTerm.add(witnessConcreteType);
447-
448-
// Add the subterm V, to get
449-
//
450-
// U.[concrete: C : P].[P:X].[concrete: C].V
451-
newTerm.append(term.end() - step.EndOffset, term.end());
452-
453-
term = newTerm;
454-
}
455-
}
456-
457378
void RewritePathEvaluator::apply(const RewriteStep &step,
458379
const RewriteSystem &system) {
459380
switch (step.Kind) {
@@ -476,9 +397,5 @@ void RewritePathEvaluator::apply(const RewriteStep &step,
476397
case RewriteStep::Relation:
477398
applyRelation(step, system);
478399
break;
479-
480-
case RewriteStep::SameTypeWitness:
481-
applySameTypeWitness(step, system);
482-
break;
483400
}
484401
}

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,7 @@ struct RewriteStep {
9898
///
9999
/// The Arg field stores the result of calling
100100
/// RewriteSystem::recordRelation().
101-
Relation,
102-
103-
/// If not inverted: the top of the primary stack must be a term ending in a
104-
/// concrete conformance symbol [concrete: C : P] followed by an associated
105-
/// type symbol [P:X]. The associated type symbol is eliminated.
106-
///
107-
/// If inverted: the associated type symbol [P:X] is introduced.
108-
///
109-
/// The Arg field stores the result of RewriteSystem::recordTypeWitness().
110-
SameTypeWitness,
101+
Relation
111102
};
112103

113104
/// The rewrite step kind.
@@ -173,11 +164,6 @@ struct RewriteStep {
173164
/*arg=*/relationID, inverse);
174165
}
175166

176-
static RewriteStep forSameTypeWitness(unsigned witnessID, bool inverse) {
177-
return RewriteStep(SameTypeWitness, /*startOffset=*/0, /*endOffset=*/0,
178-
/*arg=*/witnessID, inverse);
179-
}
180-
181167
bool isInContext() const {
182168
return StartOffset > 0 || EndOffset > 0;
183169
}
@@ -370,9 +356,6 @@ struct RewritePathEvaluator {
370356
void applyConcreteConformance(const RewriteStep &step,
371357
const RewriteSystem &system);
372358

373-
void applySameTypeWitness(const RewriteStep &step,
374-
const RewriteSystem &system);
375-
376359
void dump(llvm::raw_ostream &out) const;
377360
};
378361

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ class RewriteSystem final {
356356
Symbol associatedTypeSymbol,
357357
Symbol typeWitnessSymbol);
358358

359+
unsigned recordSameTypeWitnessRelation(
360+
Symbol concreteConformanceSymbol,
361+
Symbol associatedTypeSymbol);
362+
359363
/// A type witness has a subject type, stored in LHS, which takes the form:
360364
///
361365
/// T.[concrete: C : P].[P:X]

0 commit comments

Comments
 (0)