Skip to content

Commit 24de22a

Browse files
committed
Sema: Split off ConstraintGraph::addTypeVariable() from ::operator[]
Also, since we add a vertex immediately after introducing a new type variable, the code path to handle the case where the type variable had a parent or fixed type was actually dead.
1 parent c5de105 commit 24de22a

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

include/swift/Sema/ConstraintGraph.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ class ConstraintGraph {
244244
/// Retrieve the constraint system this graph describes.
245245
ConstraintSystem &getConstraintSystem() const { return CS; }
246246

247-
/// Access the node corresponding to the given type variable.
247+
/// Add a new vertex to the graph.
248+
void addTypeVariable(TypeVariableType *typeVar);
249+
250+
/// Look up the vertex associated with the given type variable.
248251
ConstraintGraphNode &operator[](TypeVariableType *typeVar);
249252

250253
/// Add a new constraint to the graph.

lib/Sema/ConstraintGraph.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,16 @@ ConstraintGraph::~ConstraintGraph() {
5050

5151
#pragma mark Graph accessors
5252

53-
ConstraintGraphNode &
54-
ConstraintGraph::operator[](TypeVariableType *typeVar) {
53+
void ConstraintGraph::addTypeVariable(TypeVariableType *typeVar) {
5554
// Check whether we've already created a node for this type variable.
5655
auto &impl = typeVar->getImpl();
57-
if (auto nodePtr = impl.getGraphNode()) {
58-
assert(impl.getGraphIndex() < TypeVariables.size() && "Out-of-bounds index");
59-
assert(TypeVariables[impl.getGraphIndex()] == typeVar &&
60-
"Type variable mismatch");
61-
ASSERT(nodePtr->TypeVar == typeVar &&
62-
"Use-after-free");
63-
return *nodePtr;
64-
}
56+
57+
// ComponentStep::Scope re-introduces type variables that are already
58+
// in the graph, but not in ConstraintSystem::TypeVariables.
59+
if (impl.getGraphNode())
60+
return;
61+
62+
ASSERT(!impl.hasRepresentativeOrFixed());
6563

6664
// Allocate the new node.
6765
ConstraintGraphNode *nodePtr;
@@ -85,19 +83,21 @@ ConstraintGraph::operator[](TypeVariableType *typeVar) {
8583
// create new nodes during an undo.
8684
if (CS.solverState)
8785
CS.recordChange(SolverTrail::Change::AddedTypeVariable(typeVar));
86+
}
8887

89-
// If this type variable is not the representative of its equivalence class,
90-
// add it to its representative's set of equivalences.
91-
auto typeVarRep = CS.getRepresentative(typeVar);
92-
if (typeVar != typeVarRep) {
93-
mergeNodesPre(typeVar);
94-
mergeNodes(typeVarRep, typeVar);
95-
}
96-
else if (auto fixed = CS.getFixedType(typeVarRep)) {
97-
// Bind the type variable.
98-
bindTypeVariable(typeVar, fixed);
88+
ConstraintGraphNode &
89+
ConstraintGraph::operator[](TypeVariableType *typeVar) {
90+
// Check whether we've already created a node for this type variable.
91+
auto &impl = typeVar->getImpl();
92+
auto *nodePtr = impl.getGraphNode();
93+
if (!nodePtr) {
94+
llvm::errs() << "Type variable $T" << impl.getID() << " not in constraint graph\n";
95+
abort();
9996
}
100-
97+
ASSERT(nodePtr->TypeVar == typeVar && "Use-after-free");
98+
DEBUG_ASSERT(impl.getGraphIndex() < TypeVariables.size() && "Out-of-bounds index");
99+
DEBUG_ASSERT(TypeVariables[impl.getGraphIndex()] == typeVar &&
100+
"Type variable mismatch");
101101
return *nodePtr;
102102
}
103103

lib/Sema/ConstraintSystem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void ConstraintSystem::addTypeVariable(TypeVariableType *typeVar) {
162162
TypeVariables.insert(typeVar);
163163

164164
// Notify the constraint graph.
165-
(void)CG[typeVar];
165+
CG.addTypeVariable(typeVar);
166166
}
167167

168168
void ConstraintSystem::mergeEquivalenceClasses(TypeVariableType *typeVar1,

0 commit comments

Comments
 (0)