Skip to content

Commit 0f7c822

Browse files
committed
[GSB] Add same-type rewrite rules via dependent types.
GenericSignatureBuilder::addSameTypeRewriteRule() was described in terms of an equivalence class and a potential archetype. Rework it in terms of two dependent types (i.e., the anchors of their equivalence classes), so we don't need to rely on the PotentialArchetype mechanism.
1 parent 9552692 commit 0f7c822

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,10 @@ class GenericSignatureBuilder {
445445
/// Note that we have added the nested type nestedPA
446446
void addedNestedType(PotentialArchetype *nestedPA);
447447

448-
/// Add a rewrite rule that makes \c otherPA a part of the given equivalence
449-
/// class.
448+
/// Add a rewrite rule from that makes the two types equivalent.
450449
///
451450
/// \returns true if a new rewrite rule was added, and false otherwise.
452-
bool addSameTypeRewriteRule(EquivalenceClass *equivClass,
453-
PotentialArchetype *otherPA);
451+
bool addSameTypeRewriteRule(CanType type1, CanType type2);
454452

455453
/// \brief Add a new conformance requirement specifying that the given
456454
/// potential archetypes are equivalent.

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,23 +3674,13 @@ void GenericSignatureBuilder::Implementation::removeRewriteTreeRedundancies(
36743674
}
36753675
}
36763676

3677-
bool GenericSignatureBuilder::addSameTypeRewriteRule(
3678-
EquivalenceClass *equivClass,
3679-
PotentialArchetype *otherPA){
3680-
// Simplify both sides in the hope of uncovering a common path.
3681-
Type simplifiedType1 = equivClass->getAnchor(*this, { });
3682-
3683-
Type simplifiedType2;
3684-
if (auto otherEquivClass = otherPA->getEquivalenceClassIfPresent())
3685-
simplifiedType2 = otherEquivClass->getAnchor(*this, { });
3686-
else
3687-
simplifiedType2 = getCanonicalTypeParameter(otherPA->getDependentType({ }));
3688-
3677+
bool GenericSignatureBuilder::addSameTypeRewriteRule(CanType type1,
3678+
CanType type2) {
36893679
// We already effectively have this rewrite rule.
3690-
if (simplifiedType1->isEqual(simplifiedType2)) return false;
3680+
if (type1 == type2) return false;
36913681

3692-
auto path1 = RewritePath::createPath(simplifiedType1);
3693-
auto path2 = RewritePath::createPath(simplifiedType2);
3682+
auto path1 = RewritePath::createPath(type1);
3683+
auto path2 = RewritePath::createPath(type2);
36943684

36953685
// Look for a common prefix. When we have one, form a rewrite rule using
36963686
// relative paths.
@@ -3719,9 +3709,9 @@ bool GenericSignatureBuilder::addSameTypeRewriteRule(
37193709
// Otherwise, form a rewrite rule with absolute paths.
37203710

37213711
// Find the better path and make sure it's in path2.
3722-
if (compareDependentTypes(simplifiedType1, simplifiedType2) < 0) {
3712+
if (compareDependentTypes(type1, type2) < 0) {
37233713
std::swap(path1, path2);
3724-
std::swap(simplifiedType1, simplifiedType2);
3714+
std::swap(type1, type2);
37253715
}
37263716

37273717
// Add the rewrite rule.
@@ -4872,10 +4862,6 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenArchetypes(
48724862
: getCanonicalTypeParameter(T2->getDependentType({ })))
48734863
->getCanonicalType();
48744864

4875-
// Add a rewrite rule to map T2 down to the anchor.
4876-
if (addSameTypeRewriteRule(equivClass, T2))
4877-
++Impl->RewriteGeneration;
4878-
48794865
// Merge the equivalence classes.
48804866
equivClass->modified(*this);
48814867
auto equivClass1Members = equivClass->members;
@@ -4917,6 +4903,10 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenArchetypes(
49174903
}
49184904
}
49194905

4906+
// Add a rewrite rule to map the anchor of T2 down to the anchor of T1.
4907+
if (addSameTypeRewriteRule(anchor2, anchor1))
4908+
++Impl->RewriteGeneration;
4909+
49204910
// Same-type-to-concrete requirements.
49214911
bool t1IsConcrete = !equivClass->concreteType.isNull();
49224912
bool t2IsConcrete = equivClass2 && !equivClass2->concreteType.isNull();

0 commit comments

Comments
 (0)