Skip to content

Commit e024d96

Browse files
authored
Merge pull request #36009 from xedin/constraint-graph-note-typevar-refactor
[CSBindings] NFC: Refactor some of the repetitive uses of type variab…
2 parents e6a375a + 4ef7a2d commit e024d96

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class ConstraintGraphNode {
7272
ArrayRef<TypeVariableType *> getEquivalenceClass() const;
7373

7474
private:
75+
/// Determines whether the type variable associated with this node
76+
/// is a representative of an equivalence class.
77+
///
78+
/// Note: The smallest equivalence class is of just one variable - itself.
79+
bool forRepresentativeVar() const;
80+
7581
/// Retrieve all of the type variables in the same equivalence class
7682
/// as this type variable.
7783
ArrayRef<TypeVariableType *> getEquivalenceClassUnsafe() const;

lib/Sema/ConstraintGraph.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
8484
return { *nodePtr, index };
8585
}
8686

87+
bool ConstraintGraphNode::forRepresentativeVar() const {
88+
auto *typeVar = getTypeVariable();
89+
return typeVar == typeVar->getImpl().getRepresentative(nullptr);
90+
}
91+
8792
ArrayRef<TypeVariableType *> ConstraintGraphNode::getEquivalenceClass() const{
88-
assert(TypeVar == TypeVar->getImpl().getRepresentative(nullptr) &&
93+
assert(forRepresentativeVar() &&
8994
"Can't request equivalence class from non-representative type var");
9095
return getEquivalenceClassUnsafe();
9196
}
@@ -131,10 +136,10 @@ void ConstraintGraphNode::removeConstraint(Constraint *constraint) {
131136

132137
void ConstraintGraphNode::addToEquivalenceClass(
133138
ArrayRef<TypeVariableType *> typeVars) {
134-
assert(TypeVar == TypeVar->getImpl().getRepresentative(nullptr) &&
139+
assert(forRepresentativeVar() &&
135140
"Can't extend equivalence class of non-representative type var");
136141
if (EquivalenceClass.empty())
137-
EquivalenceClass.push_back(TypeVar);
142+
EquivalenceClass.push_back(getTypeVariable());
138143
EquivalenceClass.append(typeVars.begin(), typeVars.end());
139144
}
140145

@@ -1189,6 +1194,7 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
11891194
SmallVector<Constraint *, 4> sortedConstraints(Constraints.begin(),
11901195
Constraints.end());
11911196
std::sort(sortedConstraints.begin(), sortedConstraints.end());
1197+
11921198
for (auto constraint : sortedConstraints) {
11931199
out.indent(indent + 4);
11941200
constraint->print(out, &TypeVar->getASTContext().SourceMgr);
@@ -1218,8 +1224,7 @@ void ConstraintGraphNode::print(llvm::raw_ostream &out, unsigned indent,
12181224
}
12191225

12201226
// Print equivalence class.
1221-
if (TypeVar->getImpl().getRepresentative(nullptr) == TypeVar &&
1222-
EquivalenceClass.size() > 1) {
1227+
if (forRepresentativeVar() && EquivalenceClass.size() > 1) {
12231228
out.indent(indent + 2);
12241229
out << "Equivalence class:";
12251230
for (unsigned i = 1, n = EquivalenceClass.size(); i != n; ++i) {

0 commit comments

Comments
 (0)