Skip to content

Commit 6e212de

Browse files
committed
Sema: Lazily build prepared overload when simplifying BindOverload constraint
1 parent 4475011 commit 6e212de

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

include/swift/Sema/Constraint.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ class Constraint final : public llvm::ilist_node<Constraint>,
495495

496496
/// Construct a new overload-binding constraint, which might have a fix.
497497
Constraint(Type type, OverloadChoice choice,
498-
PreparedOverload *preparedOverload,
499498
DeclContext *useDC,
500499
ConstraintFix *fix, ConstraintLocator *locator,
501500
SmallPtrSetImpl<TypeVariableType *> &typeVars);
@@ -884,6 +883,12 @@ class Constraint final : public llvm::ilist_node<Constraint>,
884883
return Overload.Prepared;
885884
}
886885

886+
void setPreparedOverload(PreparedOverload *preparedOverload) {
887+
ASSERT(Kind == ConstraintKind::BindOverload);
888+
ASSERT(!Overload.Prepared);
889+
Overload.Prepared = preparedOverload;
890+
}
891+
887892
FunctionType *getAppliedFunctionType() const {
888893
assert(Kind == ConstraintKind::ApplicableFunction);
889894
return Apply.AppliedFn;

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16648,7 +16648,7 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1664816648
constraint.getFirstType(), constraint.getSecondType(),
1664916649
constraint.getThirdType(), std::nullopt, constraint.getLocator());
1665016650

16651-
case ConstraintKind::BindOverload:
16651+
case ConstraintKind::BindOverload: {
1665216652
if (auto *fix = constraint.getFix()) {
1665316653
// TODO(diagnostics): Impact should be associated with a fix unless
1665416654
// it's a contextual problem, then only solver can decide what the impact
@@ -16659,11 +16659,26 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1665916659
return SolutionKind::Error;
1666016660
}
1666116661

16662+
// FIXME: Transitional hack.
16663+
bool enablePreparedOverloads = false;
16664+
16665+
auto *preparedOverload = constraint.getPreparedOverload();
16666+
if (!preparedOverload) {
16667+
if (enablePreparedOverloads &&
16668+
constraint.getOverloadChoice().canBePrepared()) {
16669+
preparedOverload = prepareOverload(constraint.getLocator(),
16670+
constraint.getOverloadChoice(),
16671+
constraint.getDeclContext());
16672+
const_cast<Constraint &>(constraint).setPreparedOverload(preparedOverload);
16673+
}
16674+
}
16675+
1666216676
resolveOverload(constraint.getLocator(), constraint.getFirstType(),
1666316677
constraint.getOverloadChoice(),
1666416678
constraint.getDeclContext(),
16665-
constraint.getPreparedOverload());
16679+
preparedOverload);
1666616680
return SolutionKind::Solved;
16681+
}
1666716682

1666816683
case ConstraintKind::SubclassOf:
1666916684
return simplifySubclassOfConstraint(constraint.getFirstType(),

lib/Sema/Constraint.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,14 @@ Constraint::Constraint(ConstraintKind kind, Type first, Type second,
246246
}
247247

248248
Constraint::Constraint(Type type, OverloadChoice choice,
249-
PreparedOverload *preparedOverload,
250249
DeclContext *useDC,
251250
ConstraintFix *fix, ConstraintLocator *locator,
252251
SmallPtrSetImpl<TypeVariableType *> &typeVars)
253252
: Kind(ConstraintKind::BindOverload), NumTypeVariables(typeVars.size()),
254253
HasFix(fix != nullptr), HasDeclContext(true), HasRestriction(false),
255254
IsActive(false), IsDisabled(bool(fix)), IsDisabledForPerformance(false),
256255
RememberChoice(false), IsFavored(false), IsIsolated(false),
257-
Overload{type, preparedOverload}, Locator(locator) {
256+
Overload{type, /*preparedOverload=*/nullptr}, Locator(locator) {
258257
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
259258
if (fix)
260259
*getTrailingObjects<ConstraintFix *>() = fix;
@@ -899,18 +898,6 @@ Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
899898
DeclContext *useDC,
900899
ConstraintFix *fix,
901900
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-
914901
// Collect type variables.
915902
SmallPtrSet<TypeVariableType *, 4> typeVars;
916903
if (type->hasTypeVariable())
@@ -926,7 +913,7 @@ Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
926913
typeVars.size(), fix ? 1 : 0, /*hasDeclContext=*/1,
927914
/*hasContextualTypeInfo=*/0, /*hasOverloadChoice=*/1);
928915
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
929-
return new (mem) Constraint(type, choice, preparedOverload, useDC,
916+
return new (mem) Constraint(type, choice, useDC,
930917
fix, locator, typeVars);
931918
}
932919

0 commit comments

Comments
 (0)