Skip to content

Commit 1480339

Browse files
committed
Sema: Split off introduceToInference() into its own Change
Previously, retractFromInference() was the last step in unbindTypeVariable(). This doesn't really make sense, because bindTypeVariable() doesn't call introduceToInference(); its two callers do it later. Start untangling this by splitting off introduceToInference() into its own Change, but for now, record this change at the incorrect place to maintain the same behavior as before.
1 parent c0afe3f commit 1480339

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

Diff for: include/swift/Sema/CSTrail.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class SolverTrail {
4747
ExtendedEquivalenceClass,
4848
/// Added a fixed binding for a type variable in the constraint graph.
4949
BoundTypeVariable,
50+
/// Introduced a type variable's fixed type to inference.
51+
IntroducedToInference,
5052
/// Set the fixed type or parent and flags for a type variable.
5153
UpdatedTypeVariable,
5254
};
@@ -110,6 +112,9 @@ class SolverTrail {
110112
/// Create a change that bound a type variable to a fixed type.
111113
static Change boundTypeVariable(TypeVariableType *typeVar, Type fixed);
112114

115+
/// Create a change that introduced a type variable to inference.
116+
static Change introducedToInference(TypeVariableType *typeVar, Type fixed);
117+
113118
/// Create a change that updated a type variable.
114119
static Change updatedTypeVariable(
115120
TypeVariableType *typeVar,

Diff for: include/swift/Sema/ConstraintGraph.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ class ConstraintGraphNode {
154154
void introduceToInference(Type fixedType);
155155

156156
/// Opposite of \c introduceToInference(Type)
157-
void
158-
retractFromInference(Type fixedType,
159-
SmallPtrSetImpl<TypeVariableType *> &referencedVars);
157+
void retractFromInference(Type fixedType);
160158

161159
/// Drop all previously collected bindings and re-infer based on the
162160
/// current set constraints associated with this equivalence class.
@@ -273,6 +271,9 @@ class ConstraintGraph {
273271
/// Bind the given type variable to the given fixed type.
274272
void bindTypeVariable(TypeVariableType *typeVar, Type fixedType);
275273

274+
/// Introduce the type variable's fixed type to inference.
275+
void introduceToInference(TypeVariableType *typeVar, Type fixedType);
276+
276277
/// Describes which constraints \c gatherConstraints should gather.
277278
enum class GatheringKind {
278279
/// Gather constraints associated with all of the variables within the
@@ -429,6 +430,12 @@ class ConstraintGraph {
429430
/// caution.
430431
void unbindTypeVariable(TypeVariableType *typeVar, Type fixedType);
431432

433+
/// Retract the given type variable from inference.
434+
///
435+
/// Note that this change is not recorded and cannot be undone. Use with
436+
/// caution.
437+
void retractFromInference(TypeVariableType *typeVar, Type fixedType);
438+
432439
/// Perform edge contraction on the constraint graph, merging equivalence
433440
/// classes until a fixed point is reached.
434441
bool contractEdges();

Diff for: lib/Sema/CSBindings.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -2746,10 +2746,7 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
27462746
// If all of the re-activated constraints where simplified,
27472747
// let's notify binding inference about the fact that type
27482748
// variable has been bound successfully.
2749-
{
2750-
auto &CG = cs.getConstraintGraph();
2751-
CG[TypeVar].introduceToInference(type);
2752-
}
2749+
cs.getConstraintGraph().introduceToInference(TypeVar, type);
27532750

27542751
return true;
27552752
}

Diff for: lib/Sema/CSTrail.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ SolverTrail::Change::boundTypeVariable(TypeVariableType *typeVar,
8585
return result;
8686
}
8787

88+
SolverTrail::Change
89+
SolverTrail::Change::introducedToInference(TypeVariableType *typeVar,
90+
Type fixed) {
91+
Change result;
92+
result.Kind = ChangeKind::IntroducedToInference;
93+
result.Binding.TypeVar = typeVar;
94+
result.Binding.FixedType = fixed.getPointer();
95+
return result;
96+
}
97+
8898
SolverTrail::Change
8999
SolverTrail::Change::updatedTypeVariable(
90100
TypeVariableType *typeVar,
@@ -124,6 +134,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
124134
cg.unbindTypeVariable(Binding.TypeVar, Binding.FixedType);
125135
break;
126136

137+
case ChangeKind::IntroducedToInference:
138+
cg.retractFromInference(Binding.TypeVar, Binding.FixedType);
139+
break;
140+
127141
case ChangeKind::UpdatedTypeVariable:
128142
Update.TypeVar->getImpl().setRawOptions(Update.Options);
129143
Update.TypeVar->getImpl().ParentOrFixed = Update.ParentOrFixed;
@@ -173,6 +187,14 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
173187
out << ")\n";
174188
break;
175189

190+
case ChangeKind::IntroducedToInference:
191+
out << "(introduced type variable ";
192+
Binding.TypeVar->print(out, PO);
193+
out << " with fixed type ";
194+
Binding.FixedType->print(out, PO);
195+
out << " to inference)\n";
196+
break;
197+
176198
case ChangeKind::UpdatedTypeVariable:
177199
out << "(updated type variable ";
178200
Update.TypeVar->print(out, PO);

Diff for: lib/Sema/ConstraintGraph.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
9292
if (typeVar != typeVarRep)
9393
mergeNodes(typeVar, typeVarRep);
9494
else if (auto fixed = CS.getFixedType(typeVarRep)) {
95+
// FIXME: This is totally the wrong place to do it. We should do this in
96+
// introduceToInference().
97+
if (CS.isRecordingChanges())
98+
CS.recordChange(SolverTrail::Change::introducedToInference(typeVar, fixed));
99+
95100
// Bind the type variable.
96101
bindTypeVariable(typeVar, fixed);
97102
}
@@ -414,12 +419,14 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
414419
}
415420
}
416421

417-
void ConstraintGraphNode::retractFromInference(
418-
Type fixedType, SmallPtrSetImpl<TypeVariableType *> &referencedVars) {
422+
void ConstraintGraphNode::retractFromInference(Type fixedType) {
419423
// Notify referencing variables (just like in bound case) that this
420424
// type variable has been modified.
421425
notifyReferencingVars();
422426

427+
SmallPtrSet<TypeVariableType *, 4> referencedVars;
428+
fixedType->getTypeVariables(referencedVars);
429+
423430
// TODO: This might be an overkill but it's (currently)
424431
// the simplest way to reliably ensure that all of the
425432
// no longer related constraints have been retracted.
@@ -559,6 +566,10 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
559566
CS.recordChange(SolverTrail::Change::boundTypeVariable(typeVar, fixed));
560567
}
561568

569+
void ConstraintGraph::introduceToInference(TypeVariableType *typeVar, Type fixed) {
570+
(*this)[typeVar].introduceToInference(fixed);
571+
}
572+
562573
void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed) {
563574
auto &node = (*this)[typeVar];
564575

@@ -571,8 +582,10 @@ void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed)
571582
otherNode.removeReferencedBy(typeVar);
572583
node.removeReference(otherTypeVar);
573584
}
585+
}
574586

575-
node.retractFromInference(fixed, referencedVars);
587+
void ConstraintGraph::retractFromInference(TypeVariableType *typeVar, Type fixed) {
588+
return (*this)[typeVar].retractFromInference(fixed);
576589
}
577590

578591
#pragma mark Algorithms

Diff for: lib/Sema/ConstraintSystem.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,18 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
239239
}
240240
}
241241

242+
// FIXME: This is totally the wrong place to do it. We should do this in
243+
// introduceToInference().
244+
if (isRecordingChanges())
245+
recordChange(SolverTrail::Change::introducedToInference(typeVar, type));
246+
242247
// Notify the constraint graph.
243248
CG.bindTypeVariable(typeVar, type);
249+
244250
addTypeVariableConstraintsToWorkList(typeVar);
245251

246252
if (notifyBindingInference)
247-
CG[typeVar].introduceToInference(type);
253+
CG.introduceToInference(typeVar, type);
248254
}
249255

250256
void ConstraintSystem::addTypeVariableConstraintsToWorkList(

0 commit comments

Comments
 (0)