Skip to content

Commit 9a8f3ba

Browse files
committed
Sema: Add room for a PreparedOverload to BindOverload constraint
1 parent 83f95ae commit 9a8f3ba

File tree

7 files changed

+71
-28
lines changed

7 files changed

+71
-28
lines changed

include/swift/Sema/Constraint.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace constraints {
4949
class ConstraintFix;
5050
class ConstraintLocator;
5151
class ConstraintSystem;
52+
class PreparedOverload;
5253
enum class TrailingClosureMatching;
5354

5455
/// Describes the kind of constraint placed on one or more types.
@@ -437,6 +438,9 @@ class Constraint final : public llvm::ilist_node<Constraint>,
437438
struct {
438439
/// The first type.
439440
Type First;
441+
442+
/// The prepared overload, if any.
443+
PreparedOverload *Prepared;
440444
} Overload;
441445

442446
struct {
@@ -490,7 +494,9 @@ class Constraint final : public llvm::ilist_node<Constraint>,
490494
SmallPtrSetImpl<TypeVariableType *> &typeVars);
491495

492496
/// Construct a new overload-binding constraint, which might have a fix.
493-
Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
497+
Constraint(Type type, OverloadChoice choice,
498+
PreparedOverload *preparedOverload,
499+
DeclContext *useDC,
494500
ConstraintFix *fix, ConstraintLocator *locator,
495501
SmallPtrSetImpl<TypeVariableType *> &typeVars);
496502

@@ -871,6 +877,13 @@ class Constraint final : public llvm::ilist_node<Constraint>,
871877
return *getTrailingObjects<OverloadChoice>();
872878
}
873879

880+
/// Retrieve the prepared overload choice for an overload-binding
881+
/// constraint.
882+
PreparedOverload *getPreparedOverload() const {
883+
ASSERT(Kind == ConstraintKind::BindOverload);
884+
return Overload.Prepared;
885+
}
886+
874887
FunctionType *getAppliedFunctionType() const {
875888
assert(Kind == ConstraintKind::ApplicableFunction);
876889
return Apply.AppliedFn;

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3791,9 +3791,9 @@ class ConstraintSystem {
37913791

37923792
/// Add a constraint that binds an overload set to a specific choice.
37933793
void addBindOverloadConstraint(Type boundTy, OverloadChoice choice,
3794-
ConstraintLocator *locator,
3795-
DeclContext *useDC) {
3796-
resolveOverload(locator, boundTy, choice, useDC);
3794+
ConstraintLocator *locator, DeclContext *useDC) {
3795+
resolveOverload(locator, boundTy, choice, useDC,
3796+
/*preparedOverload=*/nullptr);
37973797
}
37983798

37993799
/// Add a value member constraint to the constraint system.
@@ -4907,9 +4907,9 @@ class ConstraintSystem {
49074907
SelectedOverload choice);
49084908

49094909
/// Build and allocate a prepared overload in the solver arena.
4910-
const PreparedOverload *prepareOverload(ConstraintLocator *locator,
4911-
OverloadChoice choice,
4912-
DeclContext *useDC);
4910+
PreparedOverload *prepareOverload(ConstraintLocator *locator,
4911+
OverloadChoice choice,
4912+
DeclContext *useDC);
49134913

49144914
/// Populate the prepared overload with all type variables and constraints
49154915
/// that are to be introduced into the constraint system when this choice
@@ -4922,11 +4922,12 @@ class ConstraintSystem {
49224922

49234923
void replayChanges(
49244924
ConstraintLocator *locator,
4925-
const PreparedOverload *preparedOverload);
4925+
PreparedOverload *preparedOverload);
49264926

49274927
/// Resolve the given overload set to the given choice.
49284928
void resolveOverload(ConstraintLocator *locator, Type boundType,
4929-
OverloadChoice choice, DeclContext *useDC);
4929+
OverloadChoice choice, DeclContext *useDC,
4930+
PreparedOverload *preparedOverload);
49304931

49314932
/// Simplify a type, by replacing type variables with either their
49324933
/// fixed types (if available) or their representatives.

include/swift/Sema/OverloadChoice.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ class OverloadChoice {
250250
return (OverloadChoiceKind)kind;
251251
}
252252

253+
bool canBePrepared() const {
254+
switch (getKind()) {
255+
case OverloadChoiceKind::Decl:
256+
case OverloadChoiceKind::DeclViaBridge:
257+
case OverloadChoiceKind::DeclViaDynamic:
258+
case OverloadChoiceKind::DeclViaUnwrappedOptional:
259+
case OverloadChoiceKind::DynamicMemberLookup:
260+
case OverloadChoiceKind::KeyPathDynamicMemberLookup:
261+
return true;
262+
case OverloadChoiceKind::TupleIndex:
263+
case OverloadChoiceKind::MaterializePack:
264+
case OverloadChoiceKind::ExtractFunctionIsolation:
265+
case OverloadChoiceKind::KeyPathApplication:
266+
return false;
267+
}
268+
}
269+
253270
/// Determine whether this choice is for a declaration.
254271
bool isDecl() const {
255272
return DeclOrKind.is<ValueDecl*>();

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ namespace {
16601660

16611661
OverloadChoice choice =
16621662
OverloadChoice(Type(), E->getDecl(), E->getFunctionRefInfo());
1663-
CS.resolveOverload(locator, tv, choice, CurDC);
1663+
CS.addBindOverloadConstraint(tv, choice, locator, CurDC);
16641664
return tv;
16651665
}
16661666

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11834,8 +11834,8 @@ ConstraintSystem::simplifyValueWitnessConstraint(
1183411834
return fail();
1183511835

1183611836
auto choice = OverloadChoice(resolvedBaseType, witness.getDecl(), functionRefInfo);
11837-
resolveOverload(getConstraintLocator(locator), memberType, choice,
11838-
useDC);
11837+
addBindOverloadConstraint(memberType, choice, getConstraintLocator(locator),
11838+
useDC);
1183911839
return SolutionKind::Solved;
1184011840
}
1184111841

@@ -16661,7 +16661,8 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1666116661

1666216662
resolveOverload(constraint.getLocator(), constraint.getFirstType(),
1666316663
constraint.getOverloadChoice(),
16664-
constraint.getDeclContext());
16664+
constraint.getDeclContext(),
16665+
constraint.getPreparedOverload());
1666516666
return SolutionKind::Solved;
1666616667

1666716668
case ConstraintKind::SubclassOf:

lib/Sema/Constraint.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,16 @@ Constraint::Constraint(ConstraintKind kind, Type first, Type second,
245245
*getTrailingObjects<DeclContext *>() = useDC;
246246
}
247247

248-
Constraint::Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
248+
Constraint::Constraint(Type type, OverloadChoice choice,
249+
PreparedOverload *preparedOverload,
250+
DeclContext *useDC,
249251
ConstraintFix *fix, ConstraintLocator *locator,
250252
SmallPtrSetImpl<TypeVariableType *> &typeVars)
251253
: Kind(ConstraintKind::BindOverload), NumTypeVariables(typeVars.size()),
252254
HasFix(fix != nullptr), HasDeclContext(true), HasRestriction(false),
253255
IsActive(false), IsDisabled(bool(fix)), IsDisabledForPerformance(false),
254256
RememberChoice(false), IsFavored(false), IsIsolated(false),
255-
Overload{type}, Locator(locator) {
257+
Overload{type, preparedOverload}, Locator(locator) {
256258
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
257259
if (fix)
258260
*getTrailingObjects<ConstraintFix *>() = fix;
@@ -893,10 +895,22 @@ Constraint *Constraint::createValueWitness(
893895
}
894896

895897
Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
896-
OverloadChoice choice,
898+
OverloadChoice choice,
897899
DeclContext *useDC,
898900
ConstraintFix *fix,
899901
ConstraintLocator *locator) {
902+
// FIXME: Transitional hack.
903+
bool enablePreparedOverloads = false;
904+
905+
PreparedOverload *preparedOverload = nullptr;
906+
907+
// Prepare the overload.
908+
if (enablePreparedOverloads) {
909+
if (choice.canBePrepared()) {
910+
preparedOverload = cs.prepareOverload(locator, choice, useDC);
911+
}
912+
}
913+
900914
// Collect type variables.
901915
SmallPtrSet<TypeVariableType *, 4> typeVars;
902916
if (type->hasTypeVariable())
@@ -912,7 +926,8 @@ Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
912926
typeVars.size(), fix ? 1 : 0, /*hasDeclContext=*/1,
913927
/*hasContextualTypeInfo=*/0, /*hasOverloadChoice=*/1);
914928
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
915-
return new (mem) Constraint(type, choice, useDC, fix, locator, typeVars);
929+
return new (mem) Constraint(type, choice, preparedOverload, useDC,
930+
fix, locator, typeVars);
916931
}
917932

918933
Constraint *Constraint::createRestricted(ConstraintSystem &cs,

lib/Sema/TypeOfReference.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,7 +2499,7 @@ void ConstraintSystem::recordResolvedOverload(ConstraintLocator *locator,
24992499

25002500
void ConstraintSystem::replayChanges(
25012501
ConstraintLocator *locator,
2502-
const PreparedOverload *preparedOverload) {
2502+
PreparedOverload *preparedOverload) {
25032503
for (auto change : preparedOverload->getChanges()) {
25042504
switch (change.Kind) {
25052505
case PreparedOverload::Change::AddedTypeVariable:
@@ -2581,10 +2581,9 @@ ConstraintSystem::prepareOverloadImpl(ConstraintLocator *locator,
25812581
}
25822582
}
25832583

2584-
const PreparedOverload *
2585-
ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2586-
OverloadChoice choice,
2587-
DeclContext *useDC) {
2584+
PreparedOverload *ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2585+
OverloadChoice choice,
2586+
DeclContext *useDC) {
25882587
ASSERT(!PreparingOverload);
25892588
PreparingOverload = true;
25902589

@@ -2602,7 +2601,8 @@ ConstraintSystem::prepareOverload(ConstraintLocator *locator,
26022601

26032602
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
26042603
Type boundType, OverloadChoice choice,
2605-
DeclContext *useDC) {
2604+
DeclContext *useDC,
2605+
PreparedOverload *preparedOverload) {
26062606
// Determine the type to which we'll bind the overload set's type.
26072607
Type openedType;
26082608
Type adjustedOpenedType;
@@ -2617,11 +2617,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
26172617
case OverloadChoiceKind::DeclViaUnwrappedOptional:
26182618
case OverloadChoiceKind::DynamicMemberLookup:
26192619
case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
2620-
// FIXME: Transitional hack
2621-
bool enablePreparedOverloads = true;
2622-
2623-
if (enablePreparedOverloads) {
2624-
auto *preparedOverload = prepareOverload(locator, choice, useDC);
2620+
if (preparedOverload) {
26252621
replayChanges(locator, preparedOverload);
26262622

26272623
openedType = preparedOverload->getOpenedType();

0 commit comments

Comments
 (0)