Skip to content

Commit de1920c

Browse files
committed
Sema: Split off introduceToInference() into its own Change
1 parent c0afe3f commit de1920c

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
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,

include/swift/Sema/ConstraintGraph.h

Lines changed: 10 additions & 3 deletions
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();

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 4 deletions
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
}

lib/Sema/CSTrail.cpp

Lines changed: 22 additions & 0 deletions
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);

lib/Sema/ConstraintGraph.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,14 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
414414
}
415415
}
416416

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

422+
SmallPtrSet<TypeVariableType *, 4> referencedVars;
423+
fixedType->getTypeVariables(referencedVars);
424+
423425
// TODO: This might be an overkill but it's (currently)
424426
// the simplest way to reliably ensure that all of the
425427
// no longer related constraints have been retracted.
@@ -559,6 +561,14 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
559561
CS.recordChange(SolverTrail::Change::boundTypeVariable(typeVar, fixed));
560562
}
561563

564+
void ConstraintGraph::introduceToInference(TypeVariableType *typeVar, Type fixed) {
565+
// Record the change, if there are active scopes.
566+
if (CS.isRecordingChanges())
567+
CS.recordChange(SolverTrail::Change::introducedToInference(typeVar, fixed));
568+
569+
(*this)[typeVar].introduceToInference(fixed);
570+
}
571+
562572
void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed) {
563573
auto &node = (*this)[typeVar];
564574

@@ -571,8 +581,10 @@ void ConstraintGraph::unbindTypeVariable(TypeVariableType *typeVar, Type fixed)
571581
otherNode.removeReferencedBy(typeVar);
572582
node.removeReference(otherTypeVar);
573583
}
584+
}
574585

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

578590
#pragma mark Algorithms

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
244244
addTypeVariableConstraintsToWorkList(typeVar);
245245

246246
if (notifyBindingInference)
247-
CG[typeVar].introduceToInference(type);
247+
CG.introduceToInference(typeVar, type);
248248
}
249249

250250
void ConstraintSystem::addTypeVariableConstraintsToWorkList(

0 commit comments

Comments
 (0)