Skip to content

Commit 881a010

Browse files
committed
Sema: Record implicit value conversions in the trail
1 parent ac17292 commit 881a010

File tree

7 files changed

+42
-125
lines changed

7 files changed

+42
-125
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#endif
2020

2121
#ifndef LOCATOR_CHANGE
22-
#define LOCATOR_CHANGE(Name) CHANGE(Name)
22+
#define LOCATOR_CHANGE(Name, Map) CHANGE(Name)
2323
#endif
2424

2525
#ifndef EXPR_CHANGE
@@ -34,13 +34,15 @@
3434
#define LAST_CHANGE(Name)
3535
#endif
3636

37-
LOCATOR_CHANGE(RecordedAppliedDisjunction)
38-
LOCATOR_CHANGE(RecordedMatchCallArgumentResult)
39-
LOCATOR_CHANGE(RecordedOpenedTypes)
40-
LOCATOR_CHANGE(RecordedOpenedExistentialType)
41-
LOCATOR_CHANGE(RecordedPackExpansionEnvironment)
42-
LOCATOR_CHANGE(RecordedDefaultedConstraint)
43-
LOCATOR_CHANGE(ResolvedOverload)
37+
LOCATOR_CHANGE(RecordedDisjunctionChoice, DisjunctionChoices)
38+
LOCATOR_CHANGE(RecordedAppliedDisjunction, AppliedDisjunctions)
39+
LOCATOR_CHANGE(RecordedMatchCallArgumentResult, argumentMatchingChoices)
40+
LOCATOR_CHANGE(RecordedOpenedTypes, OpenedTypes)
41+
LOCATOR_CHANGE(RecordedOpenedExistentialType, OpenedExistentialTypes)
42+
LOCATOR_CHANGE(RecordedPackExpansionEnvironment, PackExpansionEnvironments)
43+
LOCATOR_CHANGE(RecordedDefaultedConstraint, DefaultedConstraints)
44+
LOCATOR_CHANGE(ResolvedOverload, ResolvedOverloads)
45+
LOCATOR_CHANGE(RecordedImplicitValueConversion, ImplicitValueConversions)
4446

4547
EXPR_CHANGE(AppliedPropertyWrapper)
4648
EXPR_CHANGE(RecordedImpliedResult)
@@ -60,7 +62,6 @@ CHANGE(UpdatedTypeVariable)
6062
CHANGE(AddedConversionRestriction)
6163
CHANGE(AddedFix)
6264
CHANGE(AddedFixedRequirement)
63-
CHANGE(RecordedDisjunctionChoice)
6465
CHANGE(RecordedOpenedPackExpansionType)
6566
CHANGE(RecordedPackEnvironment)
6667
CHANGE(RecordedNodeType)

include/swift/Sema/CSTrail.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class SolverTrail {
139139

140140
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
141141

142-
#define LOCATOR_CHANGE(Name) static Change Name(ConstraintLocator *locator);
142+
#define LOCATOR_CHANGE(Name, _) static Change Name(ConstraintLocator *locator);
143143
#define EXPR_CHANGE(Name) static Change Name(Expr *expr);
144144
#define CLOSURE_CHANGE(Name) static Change Name(ClosureExpr *closure);
145145
#include "swift/Sema/CSTrail.def"
@@ -187,10 +187,6 @@ class SolverTrail {
187187
unsigned reqKind,
188188
Type requirementTy);
189189

190-
/// Create a change that recorded a disjunction choice.
191-
static Change RecordedDisjunctionChoice(ConstraintLocator *locator,
192-
unsigned index);
193-
194190
/// Create a change that recorded the opening of a pack expansion type.
195191
static Change RecordedOpenedPackExpansionType(PackExpansionType *expansion);
196192

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,7 +2331,7 @@ class ConstraintSystem {
23312331

23322332
/// The set of implicit value conversions performed by the solver on
23332333
/// a current path to reach a solution.
2334-
llvm::SmallMapVector<ConstraintLocator *, ConversionRestrictionKind, 2>
2334+
llvm::SmallDenseMap<ConstraintLocator *, ConversionRestrictionKind, 2>
23352335
ImplicitValueConversions;
23362336

23372337
/// The worklist of "active" constraints that should be revisited
@@ -2415,11 +2415,6 @@ class ConstraintSystem {
24152415
}
24162416
}
24172417

2418-
void removeDefaultedConstraint(ConstraintLocator *locator) {
2419-
bool erased = DefaultedConstraints.erase(locator);
2420-
ASSERT(erased);
2421-
}
2422-
24232418
/// A cache that stores the @dynamicCallable required methods implemented by
24242419
/// types.
24252420
llvm::DenseMap<NominalTypeDecl *, DynamicCallableMethods>
@@ -2855,9 +2850,6 @@ class ConstraintSystem {
28552850
/// FIXME: Remove this.
28562851
unsigned numFixes;
28572852

2858-
/// The length of \c ImplicitValueConversions.
2859-
unsigned numImplicitValueConversions;
2860-
28612853
/// The length of \c KeyPaths.
28622854
unsigned numKeyPaths;
28632855

@@ -3453,12 +3445,6 @@ class ConstraintSystem {
34533445
void recordOpenedExistentialType(ConstraintLocator *locator,
34543446
OpenedArchetypeType *opened);
34553447

3456-
/// Undo the above change.
3457-
void removeOpenedExistentialType(ConstraintLocator *locator) {
3458-
bool erased = OpenedExistentialTypes.erase(locator);
3459-
ASSERT(erased);
3460-
}
3461-
34623448
/// Get the opened element generic environment for the given locator.
34633449
GenericEnvironment *getPackElementEnvironment(ConstraintLocator *locator,
34643450
CanType shapeClass);
@@ -3467,12 +3453,6 @@ class ConstraintSystem {
34673453
void recordPackExpansionEnvironment(ConstraintLocator *locator,
34683454
std::pair<UUID, Type> uuidAndShape);
34693455

3470-
/// Undo the above change.
3471-
void removePackExpansionEnvironment(ConstraintLocator *locator) {
3472-
bool erased = PackExpansionEnvironments.erase(locator);
3473-
ASSERT(erased);
3474-
}
3475-
34763456
/// Get the opened element generic environment for the given pack element.
34773457
PackExpansionExpr *getPackEnvironment(PackElementExpr *packElement) const;
34783458

@@ -3655,12 +3635,6 @@ class ConstraintSystem {
36553635
void recordMatchCallArgumentResult(ConstraintLocator *locator,
36563636
MatchCallArgumentResult result);
36573637

3658-
/// Undo the above change.
3659-
void removeMatchCallArgumentResult(ConstraintLocator *locator) {
3660-
bool erased = argumentMatchingChoices.erase(locator);
3661-
ASSERT(erased);
3662-
}
3663-
36643638
/// Record implicitly generated `callAsFunction` with root at the
36653639
/// given expression, located at \c locator.
36663640
void recordCallAsFunction(UnresolvedDotExpr *root, ArgumentList *arguments,
@@ -4438,9 +4412,6 @@ class ConstraintSystem {
44384412
void recordOpenedType(
44394413
ConstraintLocator *locator, ArrayRef<OpenedType> openedTypes);
44404414

4441-
/// Undo the above change.
4442-
void removeOpenedType(ConstraintLocator *locator);
4443-
44444415
/// Record the set of opened types for the given locator.
44454416
void recordOpenedTypes(
44464417
ConstraintLocatorBuilder locator,
@@ -4962,8 +4933,6 @@ class ConstraintSystem {
49624933
void recordResolvedOverload(ConstraintLocator *locator,
49634934
SelectedOverload choice);
49644935

4965-
void removeResolvedOverload(ConstraintLocator *locator);
4966-
49674936
/// Resolve the given overload set to the given choice.
49684937
void resolveOverload(ConstraintLocator *locator, Type boundType,
49694938
OverloadChoice choice, DeclContext *useDC);
@@ -5253,6 +5222,10 @@ class ConstraintSystem {
52535222
TypeMatchOptions flags,
52545223
ConstraintLocatorBuilder locator);
52555224

5225+
/// Update ImplicitValueConversions and record a change in the trail.
5226+
void recordImplicitValueConversion(ConstraintLocator *locator,
5227+
ConversionRestrictionKind restriction);
5228+
52565229
/// Simplify a conversion constraint by applying the given
52575230
/// reduction rule, which is known to apply at the outermost level.
52585231
SolutionKind simplifyRestrictedConstraint(
@@ -5393,22 +5366,10 @@ class ConstraintSystem {
53935366
/// Record a particular disjunction choice and add a change to the trail.
53945367
void recordDisjunctionChoice(ConstraintLocator *locator, unsigned index);
53955368

5396-
/// Undo the above change.
5397-
void removeDisjunctionChoice(ConstraintLocator *locator) {
5398-
bool erased = DisjunctionChoices.erase(locator);
5399-
ASSERT(erased);
5400-
}
5401-
54025369
/// Record applied disjunction and add a change to the trail.
54035370
void recordAppliedDisjunction(ConstraintLocator *locator,
54045371
FunctionType *type);
54055372

5406-
/// Undo the above change.
5407-
void removeAppliedDisjunction(ConstraintLocator *locator) {
5408-
bool erased = AppliedDisjunctions.erase(locator);
5409-
ASSERT(erased);
5410-
}
5411-
54125373
/// Filter the set of disjunction terms, keeping only those where the
54135374
/// predicate returns \c true.
54145375
///

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14082,6 +14082,17 @@ void ConstraintSystem::addRestrictedConstraint(
1408214082
TMF_GenerateConstraints, locator);
1408314083
}
1408414084

14085+
void ConstraintSystem::recordImplicitValueConversion(
14086+
ConstraintLocator *locator,
14087+
ConversionRestrictionKind restriction) {
14088+
bool inserted = ImplicitValueConversions.insert(
14089+
{getConstraintLocator(locator), restriction}).second;
14090+
ASSERT(inserted);
14091+
14092+
if (solverState)
14093+
recordChange(SolverTrail::Change::RecordedImplicitValueConversion(locator));
14094+
}
14095+
1408514096
/// Given that we have a conversion constraint between two types, and
1408614097
/// that the given constraint-reduction rule applies between them at
1408714098
/// the top level, apply it and generate any necessary recursive

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
437437
}
438438

439439
for (auto &valueConversion : solution.ImplicitValueConversions) {
440-
ImplicitValueConversions.insert(valueConversion);
440+
if (ImplicitValueConversions.count(valueConversion.first) == 0) {
441+
recordImplicitValueConversion(valueConversion.first,
442+
valueConversion.second);
443+
}
441444
}
442445

443446
// Register the argument lists.
@@ -699,7 +702,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
699702
numTypeVariables = cs.TypeVariables.size();
700703
numFixes = cs.Fixes.size();
701704
numKeyPaths = cs.KeyPaths.size();
702-
numImplicitValueConversions = cs.ImplicitValueConversions.size();
703705
numArgumentLists = cs.ArgumentLists.size();
704706
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
705707
numSynthesizedConformances = cs.SynthesizedConformances.size();
@@ -738,9 +740,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
738740
/// Remove any key path expressions.
739741
truncate(cs.KeyPaths, numKeyPaths);
740742

741-
// Remove any implicit value conversions.
742-
truncate(cs.ImplicitValueConversions, numImplicitValueConversions);
743-
744743
// Remove any argument lists no longer in scope.
745744
truncate(cs.ArgumentLists, numArgumentLists);
746745

lib/Sema/CSTrail.cpp

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ SolverTrail::~SolverTrail() {
4848
ASSERT(Changes.empty() && "Trail corrupted");
4949
}
5050

51-
#define LOCATOR_CHANGE(Name) \
51+
#define LOCATOR_CHANGE(Name, _) \
5252
SolverTrail::Change \
5353
SolverTrail::Change::Name(ConstraintLocator *locator) { \
5454
Change result; \
@@ -184,16 +184,6 @@ SolverTrail::Change::AddedFixedRequirement(GenericTypeParamType *GP,
184184
return result;
185185
}
186186

187-
SolverTrail::Change
188-
SolverTrail::Change::RecordedDisjunctionChoice(ConstraintLocator *locator,
189-
unsigned index) {
190-
Change result;
191-
result.Kind = ChangeKind::RecordedDisjunctionChoice;
192-
result.TheLocator = locator;
193-
result.Options = index;
194-
return result;
195-
}
196-
197187
SolverTrail::Change
198188
SolverTrail::Change::RecordedOpenedPackExpansionType(PackExpansionType *expansionTy) {
199189
Change result;
@@ -360,6 +350,14 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
360350
auto &cg = cs.getConstraintGraph();
361351

362352
switch (Kind) {
353+
#define LOCATOR_CHANGE(Name, Map) \
354+
case ChangeKind::Name: { \
355+
bool erased = cs.Map.erase(TheLocator); \
356+
ASSERT(erased); \
357+
break; \
358+
}
359+
#include "swift/Sema/CSTrail.def"
360+
363361
case ChangeKind::AddedTypeVariable:
364362
cg.removeNode(TypeVar);
365363
break;
@@ -409,42 +407,14 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
409407
FixedRequirement.ReqTy);
410408
break;
411409

412-
case ChangeKind::RecordedDisjunctionChoice:
413-
cs.removeDisjunctionChoice(TheLocator);
414-
break;
415-
416-
case ChangeKind::RecordedAppliedDisjunction:
417-
cs.removeAppliedDisjunction(TheLocator);
418-
break;
419-
420-
case ChangeKind::RecordedMatchCallArgumentResult:
421-
cs.removeMatchCallArgumentResult(TheLocator);
422-
break;
423-
424-
case ChangeKind::RecordedOpenedTypes:
425-
cs.removeOpenedType(TheLocator);
426-
break;
427-
428-
case ChangeKind::RecordedOpenedExistentialType:
429-
cs.removeOpenedExistentialType(TheLocator);
430-
break;
431-
432410
case ChangeKind::RecordedOpenedPackExpansionType:
433411
cs.removeOpenedPackExpansionType(TheExpansion);
434412
break;
435413

436-
case ChangeKind::RecordedPackExpansionEnvironment:
437-
cs.removePackExpansionEnvironment(TheLocator);
438-
break;
439-
440414
case ChangeKind::RecordedPackEnvironment:
441415
cs.removePackEnvironment(TheElement);
442416
break;
443417

444-
case ChangeKind::RecordedDefaultedConstraint:
445-
cs.removeDefaultedConstraint(TheLocator);
446-
break;
447-
448418
case ChangeKind::RecordedNodeType:
449419
cs.restoreType(Node.Node, Node.OldType);
450420
break;
@@ -470,10 +440,6 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
470440
cs.removePropertyWrapper(TheExpr);
471441
break;
472442

473-
case ChangeKind::ResolvedOverload:
474-
cs.removeResolvedOverload(TheLocator);
475-
break;
476-
477443
case ChangeKind::RecordedClosureType:
478444
cs.removeClosureType(TheClosure);
479445
break;
@@ -522,7 +488,7 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
522488

523489
switch (Kind) {
524490

525-
#define LOCATOR_CHANGE(Name) \
491+
#define LOCATOR_CHANGE(Name, _) \
526492
case ChangeKind::Name: \
527493
out << "(" << #Name << " at "; \
528494
TheLocator->dump(&cs.getASTContext().SourceMgr, out); \
@@ -641,13 +607,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
641607
out << ")\n";
642608
break;
643609

644-
case ChangeKind::RecordedDisjunctionChoice:
645-
out << "(RecordedDisjunctionChoice at ";
646-
TheLocator->dump(&cs.getASTContext().SourceMgr, out);
647-
out << " index ";
648-
out << Options << ")\n";
649-
break;
650-
651610
case ChangeKind::RecordedOpenedPackExpansionType:
652611
out << "(RecordedOpenedPackExpansionType for ";
653612
TheExpansion->print(out, PO);

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void ConstraintSystem::recordDisjunctionChoice(
298298
ASSERT(inserted);
299299

300300
if (solverState)
301-
recordChange(SolverTrail::Change::RecordedDisjunctionChoice(locator, index));
301+
recordChange(SolverTrail::Change::RecordedDisjunctionChoice(locator));
302302
}
303303

304304
void ConstraintSystem::recordAppliedDisjunction(
@@ -1691,11 +1691,6 @@ void ConstraintSystem::recordOpenedType(
16911691
recordChange(SolverTrail::Change::RecordedOpenedTypes(locator));
16921692
}
16931693

1694-
void ConstraintSystem::removeOpenedType(ConstraintLocator *locator) {
1695-
bool erased = OpenedTypes.erase(locator);
1696-
ASSERT(erased);
1697-
}
1698-
16991694
void ConstraintSystem::recordOpenedTypes(
17001695
ConstraintLocatorBuilder locator,
17011696
const OpenedTypeMap &replacements,
@@ -3754,11 +3749,6 @@ void ConstraintSystem::recordResolvedOverload(ConstraintLocator *locator,
37543749
recordChange(SolverTrail::Change::ResolvedOverload(locator));
37553750
}
37563751

3757-
void ConstraintSystem::removeResolvedOverload(ConstraintLocator *locator) {
3758-
bool erased = ResolvedOverloads.erase(locator);
3759-
ASSERT(erased);
3760-
}
3761-
37623752
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
37633753
Type boundType,
37643754
OverloadChoice choice,

0 commit comments

Comments
 (0)