Skip to content

Commit c5a742b

Browse files
committed
Sema: Remove ConstraintGraph::TypeVariables
1 parent 5200478 commit c5a742b

File tree

4 files changed

+17
-65
lines changed

4 files changed

+17
-65
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,6 @@ class ConstraintGraph {
322322
llvm::function_ref<bool(Constraint *)> acceptConstraint =
323323
[](Constraint *constraint) { return true; });
324324

325-
/// Retrieve the type variables that correspond to nodes in the graph.
326-
///
327-
/// The subscript operator can be used to retrieve the nodes that
328-
/// correspond to these type variables.
329-
ArrayRef<TypeVariableType *> getTypeVariables() const {
330-
return TypeVariables;
331-
}
332-
333325
/// Describes a single component, as produced by the connected components
334326
/// algorithm.
335327
struct Component {
@@ -473,9 +465,6 @@ class ConstraintGraph {
473465
/// The constraint system.
474466
ConstraintSystem &CS;
475467

476-
/// The type variables in this graph, in stable order.
477-
std::vector<TypeVariableType *> TypeVariables;
478-
479468
/// Constraints that are "orphaned" because they contain no type variables.
480469
SmallVector<Constraint *, 4> OrphanedConstraints;
481470

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,6 @@ class TypeVariableType::Implementation {
339339
/// The corresponding node in the constraint graph.
340340
constraints::ConstraintGraphNode *GraphNode = nullptr;
341341

342-
/// Index into the list of type variables, as used by the
343-
/// constraint graph.
344-
unsigned GraphIndex;
345-
346342
friend class constraints::SolverTrail;
347343

348344
public:
@@ -406,17 +402,6 @@ class TypeVariableType::Implementation {
406402
void setGraphNode(constraints::ConstraintGraphNode *newNode) {
407403
GraphNode = newNode;
408404
}
409-
410-
/// Retrieve the index into the constraint graph's list of type variables.
411-
unsigned getGraphIndex() const {
412-
assert(GraphNode && "Graph node isn't set");
413-
return GraphIndex;
414-
}
415-
416-
/// Set the index into the constraint graph's list of type variables.
417-
void setGraphIndex(unsigned newIndex) {
418-
GraphIndex = newIndex;
419-
}
420405

421406
/// Check whether this type variable either has a representative that
422407
/// is not itself or has a fixed type binding.

lib/Sema/ConstraintGraph.cpp

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ using namespace constraints;
3838
ConstraintGraph::ConstraintGraph(ConstraintSystem &cs) : CS(cs) { }
3939

4040
ConstraintGraph::~ConstraintGraph() {
41-
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
42-
auto &impl = TypeVariables[i]->getImpl();
43-
delete impl.getGraphNode();
44-
impl.setGraphNode(nullptr);
41+
#ifndef NDEBUG
42+
for (unsigned i = 0, n = CS.TypeVariables.size(); i != n; ++i) {
43+
auto &impl = CS.TypeVariables[i]->getImpl();
44+
ASSERT(impl.getGraphNode() == nullptr);
4545
}
46+
#endif
47+
4648
for (auto *node : FreeList) {
4749
delete node;
4850
}
@@ -70,30 +72,16 @@ void ConstraintGraph::addTypeVariable(TypeVariableType *typeVar) {
7072
FreeList.pop_back();
7173
nodePtr->initTypeVariable(typeVar);
7274
}
73-
unsigned index = TypeVariables.size();
7475
impl.setGraphNode(nodePtr);
75-
impl.setGraphIndex(index);
76-
77-
// Record this type variable.
78-
TypeVariables.push_back(typeVar);
7976

8077
if (CS.solverState)
8178
CS.recordChange(SolverTrail::Change::AddedTypeVariable(typeVar));
8279
}
8380

8481
ConstraintGraphNode &
8582
ConstraintGraph::operator[](TypeVariableType *typeVar) {
86-
// Check whether we've already created a node for this type variable.
87-
auto &impl = typeVar->getImpl();
88-
auto *nodePtr = impl.getGraphNode();
89-
if (!nodePtr) {
90-
llvm::errs() << "Type variable $T" << impl.getID() << " not in constraint graph\n";
91-
abort();
92-
}
83+
auto *nodePtr = typeVar->getImpl().getGraphNode();
9384
ASSERT(nodePtr->TypeVar == typeVar && "Use-after-free");
94-
DEBUG_ASSERT(impl.getGraphIndex() < TypeVariables.size() && "Out-of-bounds index");
95-
DEBUG_ASSERT(TypeVariables[impl.getGraphIndex()] == typeVar &&
96-
"Type variable mismatch");
9785
return *nodePtr;
9886
}
9987

@@ -357,17 +345,10 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
357345
void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
358346
// Remove this node.
359347
auto &impl = typeVar->getImpl();
360-
unsigned index = impl.getGraphIndex();
361348
auto *node = impl.getGraphNode();
362349
node->reset();
363350
FreeList.push_back(node);
364351
impl.setGraphNode(nullptr);
365-
366-
// Remove this type variable from the list.
367-
unsigned lastIndex = TypeVariables.size()-1;
368-
if (index < lastIndex)
369-
TypeVariables[index] = TypeVariables[lastIndex];
370-
TypeVariables.pop_back();
371352
}
372353

373354
void ConstraintGraph::addConstraint(Constraint *constraint) {
@@ -1633,7 +1614,7 @@ void ConstraintGraph::verify() {
16331614
// Verify that the type variables are either representatives or represented
16341615
// within their representative's equivalence class.
16351616
// FIXME: Also check to make sure the equivalence classes aren't too large?
1636-
for (auto typeVar : TypeVariables) {
1617+
for (auto typeVar : CS.TypeVariables) {
16371618
auto typeVarRep = CS.getRepresentative(typeVar);
16381619
auto &repNode = (*this)[typeVarRep];
16391620
if (typeVar != typeVarRep) {
@@ -1654,24 +1635,15 @@ void ConstraintGraph::verify() {
16541635
}
16551636
}
16561637

1657-
// Verify that our type variable map/vector are in sync.
1658-
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
1659-
auto typeVar = TypeVariables[i];
1660-
auto &impl = typeVar->getImpl();
1661-
requireSameValue(impl.getGraphIndex(), i, "wrong graph node index");
1662-
require(impl.getGraphNode(), "null graph node");
1663-
}
1664-
16651638
// Verify consistency of all of the nodes in the graph.
1666-
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
1667-
auto typeVar = TypeVariables[i];
1639+
for (auto typeVar : CS.TypeVariables) {
16681640
auto &impl = typeVar->getImpl();
16691641
impl.getGraphNode()->verify(*this);
16701642
}
16711643

16721644
// Collect all of the constraints known to the constraint graph.
16731645
llvm::SmallPtrSet<Constraint *, 4> knownConstraints;
1674-
for (auto typeVar : getTypeVariables()) {
1646+
for (auto typeVar : CS.TypeVariables) {
16751647
for (auto constraint : (*this)[typeVar].getConstraints())
16761648
knownConstraints.insert(constraint);
16771649
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ ConstraintSystem::ConstraintSystem(DeclContext *dc,
130130
Options |= ConstraintSystemFlags::UseClangFunctionTypes;
131131
}
132132

133-
ConstraintSystem::~ConstraintSystem() {}
133+
ConstraintSystem::~ConstraintSystem() {
134+
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
135+
auto &impl = TypeVariables[i]->getImpl();
136+
delete impl.getGraphNode();
137+
impl.setGraphNode(nullptr);
138+
}
139+
}
134140

135141
void ConstraintSystem::startExpressionTimer(ExpressionTimer::AnchorType anchor) {
136142
ASSERT(!Timer);

0 commit comments

Comments
 (0)