Skip to content

Commit 45346c8

Browse files
committed
Sema: Remove ConstraintGraph::TypeVariables
1 parent c4e466d commit 45346c8

File tree

4 files changed

+17
-65
lines changed

4 files changed

+17
-65
lines changed

include/swift/Sema/ConstraintGraph.h

-11
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,6 @@ class ConstraintGraph {
311311
llvm::function_ref<bool(Constraint *)> acceptConstraint =
312312
[](Constraint *constraint) { return true; });
313313

314-
/// Retrieve the type variables that correspond to nodes in the graph.
315-
///
316-
/// The subscript operator can be used to retrieve the nodes that
317-
/// correspond to these type variables.
318-
ArrayRef<TypeVariableType *> getTypeVariables() const {
319-
return TypeVariables;
320-
}
321-
322314
/// Describes a single component, as produced by the connected components
323315
/// algorithm.
324316
struct Component {
@@ -462,9 +454,6 @@ class ConstraintGraph {
462454
/// The constraint system.
463455
ConstraintSystem &CS;
464456

465-
/// The type variables in this graph, in stable order.
466-
std::vector<TypeVariableType *> TypeVariables;
467-
468457
/// Constraints that are "orphaned" because they contain no type variables.
469458
SmallVector<Constraint *, 4> OrphanedConstraints;
470459

include/swift/Sema/ConstraintSystem.h

-15
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

+10-38
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

@@ -372,17 +360,10 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
372360
void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
373361
// Remove this node.
374362
auto &impl = typeVar->getImpl();
375-
unsigned index = impl.getGraphIndex();
376363
auto *node = impl.getGraphNode();
377364
node->reset();
378365
FreeList.push_back(node);
379366
impl.setGraphNode(nullptr);
380-
381-
// Remove this type variable from the list.
382-
unsigned lastIndex = TypeVariables.size()-1;
383-
if (index < lastIndex)
384-
TypeVariables[index] = TypeVariables[lastIndex];
385-
TypeVariables.pop_back();
386367
}
387368

388369
void ConstraintGraph::addConstraint(Constraint *constraint) {
@@ -1648,7 +1629,7 @@ void ConstraintGraph::verify() {
16481629
// Verify that the type variables are either representatives or represented
16491630
// within their representative's equivalence class.
16501631
// FIXME: Also check to make sure the equivalence classes aren't too large?
1651-
for (auto typeVar : TypeVariables) {
1632+
for (auto typeVar : CS.TypeVariables) {
16521633
auto typeVarRep = CS.getRepresentative(typeVar);
16531634
auto &repNode = (*this)[typeVarRep];
16541635
if (typeVar != typeVarRep) {
@@ -1669,24 +1650,15 @@ void ConstraintGraph::verify() {
16691650
}
16701651
}
16711652

1672-
// Verify that our type variable map/vector are in sync.
1673-
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
1674-
auto typeVar = TypeVariables[i];
1675-
auto &impl = typeVar->getImpl();
1676-
requireSameValue(impl.getGraphIndex(), i, "wrong graph node index");
1677-
require(impl.getGraphNode(), "null graph node");
1678-
}
1679-
16801653
// Verify consistency of all of the nodes in the graph.
1681-
for (unsigned i = 0, n = TypeVariables.size(); i != n; ++i) {
1682-
auto typeVar = TypeVariables[i];
1654+
for (auto typeVar : CS.TypeVariables) {
16831655
auto &impl = typeVar->getImpl();
16841656
impl.getGraphNode()->verify(*this);
16851657
}
16861658

16871659
// Collect all of the constraints known to the constraint graph.
16881660
llvm::SmallPtrSet<Constraint *, 4> knownConstraints;
1689-
for (auto typeVar : getTypeVariables()) {
1661+
for (auto typeVar : CS.TypeVariables) {
16901662
for (auto constraint : (*this)[typeVar].getConstraints())
16911663
knownConstraints.insert(constraint);
16921664
}

lib/Sema/ConstraintSystem.cpp

+7-1
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)