Skip to content

Commit 6652e34

Browse files
committed
Sema: Add SolverTrail::Change::InferredBindings and ::RetractedBindings
1 parent 94494b6 commit 6652e34

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class SolverTrail {
4949
RelatedTypeVariables,
5050
/// Introduced a type variable's fixed type to inference.
5151
IntroducedToInference,
52+
/// Inferred potential bindings from a constraint.
53+
InferredBindings,
54+
/// Retracted potential bindings from a constraint.
55+
RetractedBindings,
5256
/// Set the fixed type or parent and flags for a type variable.
5357
UpdatedTypeVariable,
5458
};
@@ -132,6 +136,14 @@ class SolverTrail {
132136
/// Create a change that introduced a type variable to inference.
133137
static Change introducedToInference(TypeVariableType *typeVar, Type fixed);
134138

139+
/// Create a change that inferred bindings from a constraint.
140+
static Change inferredBindings(TypeVariableType *typeVar,
141+
Constraint *constraint);
142+
143+
/// Create a change that retracted bindings from a constraint.
144+
static Change retractedBindings(TypeVariableType *typeVar,
145+
Constraint *constraint);
146+
135147
/// Create a change that updated a type variable.
136148
static Change updatedTypeVariable(
137149
TypeVariableType *typeVar,

include/swift/Sema/ConstraintGraph.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ class ConstraintGraph {
436436
void unrelateTypeVariables(TypeVariableType *typeVar,
437437
TypeVariableType *otherTypeVar);
438438

439+
/// Infer bindings from the given constraint.
440+
///
441+
/// Note that this it only meant to be called by SolverTrail::Change::undo().
442+
void inferBindings(TypeVariableType *typeVar, Constraint *constraint);
443+
444+
/// Retract bindings from the given constraint.
445+
///
446+
/// Note that this it only meant to be called by SolverTrail::Change::undo().
447+
void retractBindings(TypeVariableType *typeVar, Constraint *constraint);
448+
439449
/// Retract the given type variable from inference.
440450
///
441451
/// Note that this it only meant to be called by SolverTrail::Change::undo().

lib/Sema/CSBindings.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,10 @@ void PotentialBindings::infer(Constraint *constraint) {
17721772
if (!Constraints.insert(constraint).second)
17731773
return;
17741774

1775+
// Record the change, if there are active scopes.
1776+
if (CS.isRecordingChanges())
1777+
CS.recordChange(SolverTrail::Change::inferredBindings(TypeVar, constraint));
1778+
17751779
switch (constraint->getKind()) {
17761780
case ConstraintKind::Bind:
17771781
case ConstraintKind::Equal:
@@ -1943,6 +1947,10 @@ void PotentialBindings::retract(Constraint *constraint) {
19431947
if (!Constraints.erase(constraint))
19441948
return;
19451949

1950+
// Record the change, if there are active scopes.
1951+
if (CS.isRecordingChanges())
1952+
CS.recordChange(SolverTrail::Change::retractedBindings(TypeVar, constraint));
1953+
19461954
Bindings.erase(
19471955
llvm::remove_if(Bindings,
19481956
[&constraint](const PotentialBinding &binding) {

lib/Sema/CSTrail.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ SolverTrail::Change::introducedToInference(TypeVariableType *typeVar,
9999
return result;
100100
}
101101

102+
SolverTrail::Change
103+
SolverTrail::Change::inferredBindings(TypeVariableType *typeVar,
104+
Constraint *constraint) {
105+
Change result;
106+
result.Kind = ChangeKind::InferredBindings;
107+
result.TheConstraint.TypeVar = typeVar;
108+
result.TheConstraint.Constraint = constraint;
109+
return result;
110+
}
111+
112+
SolverTrail::Change
113+
SolverTrail::Change::retractedBindings(TypeVariableType *typeVar,
114+
Constraint *constraint) {
115+
Change result;
116+
result.Kind = ChangeKind::RetractedBindings;
117+
result.TheConstraint.TypeVar = typeVar;
118+
result.TheConstraint.Constraint = constraint;
119+
return result;
120+
}
121+
102122
SolverTrail::Change
103123
SolverTrail::Change::updatedTypeVariable(
104124
TypeVariableType *typeVar,
@@ -142,6 +162,14 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
142162
cg.retractFromInference(Binding.TypeVar, Binding.FixedType);
143163
break;
144164

165+
case ChangeKind::InferredBindings:
166+
cg.retractBindings(TheConstraint.TypeVar, TheConstraint.Constraint);
167+
break;
168+
169+
case ChangeKind::RetractedBindings:
170+
cg.inferBindings(TheConstraint.TypeVar, TheConstraint.Constraint);
171+
break;
172+
145173
case ChangeKind::UpdatedTypeVariable:
146174
Update.TypeVar->getImpl().setRawOptions(Update.Options);
147175
Update.TypeVar->getImpl().ParentOrFixed = Update.ParentOrFixed;
@@ -205,6 +233,24 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
205233
out << " to inference)\n";
206234
break;
207235

236+
case ChangeKind::InferredBindings:
237+
out << "(inferred bindings from ";
238+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
239+
indent + 2);
240+
out << " for type variable ";
241+
TheConstraint.TypeVar->print(out, PO);
242+
out << ")\n";
243+
break;
244+
245+
case ChangeKind::RetractedBindings:
246+
out << "(retracted bindings from ";
247+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
248+
indent + 2);
249+
out << " for type variable ";
250+
TheConstraint.TypeVar->print(out, PO);
251+
out << ")\n";
252+
break;
253+
208254
case ChangeKind::UpdatedTypeVariable:
209255
out << "(updated type variable ";
210256
Update.TypeVar->print(out, PO);

lib/Sema/ConstraintGraph.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,17 @@ void ConstraintGraph::unrelateTypeVariables(TypeVariableType *typeVar,
609609
}
610610

611611
void ConstraintGraph::retractFromInference(TypeVariableType *typeVar, Type fixed) {
612-
return (*this)[typeVar].retractFromInference(fixed);
612+
(*this)[typeVar].retractFromInference(fixed);
613+
}
614+
615+
void ConstraintGraph::inferBindings(TypeVariableType *typeVar,
616+
Constraint *constraint) {
617+
(*this)[typeVar].getCurrentBindings().infer(constraint);
618+
}
619+
620+
void ConstraintGraph::retractBindings(TypeVariableType *typeVar,
621+
Constraint *constraint) {
622+
(*this)[typeVar].getCurrentBindings().retract(constraint);
613623
}
614624

615625
#pragma mark Algorithms

0 commit comments

Comments
 (0)