Skip to content

Commit 957e05c

Browse files
committed
[Constraint] Allow constraints to be disabled only in "performance" mode
1 parent 8447424 commit 957e05c

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

include/swift/Sema/Constraint.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ class Constraint final : public llvm::ilist_node<Constraint>,
324324
/// constraint graph during constraint propagation?
325325
unsigned IsDisabled : 1;
326326

327+
/// Constraint is disabled in performance mode only, could be attempted
328+
/// for diagnostic purposes.
329+
unsigned IsDisabledForPerformance : 1;
330+
327331
/// Whether the choice of this disjunction should be recorded in the
328332
/// solver state.
329333
unsigned RememberChoice : 1;
@@ -542,18 +546,27 @@ class Constraint final : public llvm::ilist_node<Constraint>,
542546
IsActive = active;
543547
}
544548

545-
/// Whether this constraint is active, i.e., in the worklist.
546-
bool isDisabled() const { return IsDisabled; }
549+
/// Whether this constraint is disabled and shouldn't be attempted by the
550+
/// solver.
551+
bool isDisabled() const { return IsDisabled || IsDisabledForPerformance; }
552+
553+
/// Whether this constraint is disabled and shouldn't be attempted by the
554+
/// solver only in "performance" mode.
555+
bool isDisabledInPerformanceMode() const { return IsDisabledForPerformance; }
547556

548557
/// Set whether this constraint is active or not.
549-
void setDisabled() {
558+
void setDisabled(bool enableForDiagnostics = false) {
550559
assert(!isActive() && "Cannot disable constraint marked as active!");
551-
IsDisabled = true;
560+
if (enableForDiagnostics)
561+
IsDisabledForPerformance = true;
562+
else
563+
IsDisabled = true;
552564
}
553565

554566
void setEnabled() {
555567
assert(isDisabled() && "Can't re-enable already active constraint!");
556568
IsDisabled = false;
569+
IsDisabledForPerformance = false;
557570
}
558571

559572
/// Mark or retrieve whether this constraint should be favored in the system.

include/swift/Sema/ConstraintSystem.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5301,7 +5301,20 @@ class DisjunctionChoice {
53015301

53025302
bool attempt(ConstraintSystem &cs) const;
53035303

5304-
bool isDisabled() const { return Choice->isDisabled(); }
5304+
bool isDisabled() const {
5305+
if (!Choice->isDisabled())
5306+
return false;
5307+
5308+
// If solver is in a diagnostic mode, let's allow
5309+
// constraints that have fixes or have been disabled
5310+
// in attempt to produce a solution faster for
5311+
// well-formed expressions.
5312+
if (CS.shouldAttemptFixes()) {
5313+
return !(hasFix() || Choice->isDisabledInPerformanceMode());
5314+
}
5315+
5316+
return true;
5317+
}
53055318

53065319
bool hasFix() const {
53075320
return bool(Choice->getFix());

lib/Sema/CSStep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ bool DisjunctionStep::shouldSkip(const DisjunctionChoice &choice) const {
612612

613613
// Skip disabled overloads in the diagnostic mode if they do not have a
614614
// fix attached to them e.g. overloads where labels didn't match up.
615-
if (choice.isDisabled() && !(CS.shouldAttemptFixes() && choice.hasFix()))
615+
if (choice.isDisabled())
616616
return skip("disabled");
617617

618618
// Skip unavailable overloads (unless in dignostic mode).

lib/Sema/Constraint.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Constraint::Constraint(ConstraintKind kind, ArrayRef<Constraint *> constraints,
3030
ConstraintLocator *locator,
3131
SmallPtrSetImpl<TypeVariableType *> &typeVars)
3232
: Kind(kind), HasRestriction(false), IsActive(false), IsDisabled(false),
33-
RememberChoice(false), IsFavored(false),
33+
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
3434
NumTypeVariables(typeVars.size()), Nested(constraints), Locator(locator) {
3535
assert(kind == ConstraintKind::Disjunction);
3636
std::uninitialized_copy(typeVars.begin(), typeVars.end(),
@@ -41,7 +41,7 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
4141
ConstraintLocator *locator,
4242
SmallPtrSetImpl<TypeVariableType *> &typeVars)
4343
: Kind(Kind), HasRestriction(false), IsActive(false), IsDisabled(false),
44-
RememberChoice(false), IsFavored(false),
44+
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
4545
NumTypeVariables(typeVars.size()), Types{First, Second, Type()},
4646
Locator(locator) {
4747
switch (Kind) {
@@ -110,7 +110,7 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,
110110
ConstraintLocator *locator,
111111
SmallPtrSetImpl<TypeVariableType *> &typeVars)
112112
: Kind(Kind), HasRestriction(false), IsActive(false), IsDisabled(false),
113-
RememberChoice(false), IsFavored(false),
113+
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
114114
NumTypeVariables(typeVars.size()), Types{First, Second, Third},
115115
Locator(locator) {
116116
switch (Kind) {
@@ -168,7 +168,7 @@ Constraint::Constraint(ConstraintKind kind, Type first, Type second,
168168
ConstraintLocator *locator,
169169
SmallPtrSetImpl<TypeVariableType *> &typeVars)
170170
: Kind(kind), HasRestriction(false), IsActive(false), IsDisabled(false),
171-
RememberChoice(false), IsFavored(false),
171+
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
172172
NumTypeVariables(typeVars.size()), Member{first, second, {member}, useDC},
173173
Locator(locator) {
174174
assert(kind == ConstraintKind::ValueMember ||
@@ -187,7 +187,7 @@ Constraint::Constraint(ConstraintKind kind, Type first, Type second,
187187
ConstraintLocator *locator,
188188
SmallPtrSetImpl<TypeVariableType *> &typeVars)
189189
: Kind(kind), HasRestriction(false), IsActive(false), IsDisabled(false),
190-
RememberChoice(false), IsFavored(false),
190+
IsDisabledForPerformance(false), RememberChoice(false), IsFavored(false),
191191
NumTypeVariables(typeVars.size()), Locator(locator) {
192192
Member.First = first;
193193
Member.Second = second;
@@ -207,8 +207,8 @@ Constraint::Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
207207
ConstraintFix *fix, ConstraintLocator *locator,
208208
SmallPtrSetImpl<TypeVariableType *> &typeVars)
209209
: Kind(ConstraintKind::BindOverload), TheFix(fix), HasRestriction(false),
210-
IsActive(false), IsDisabled(bool(fix)), RememberChoice(false),
211-
IsFavored(false),
210+
IsActive(false), IsDisabled(bool(fix)), IsDisabledForPerformance(false),
211+
RememberChoice(false), IsFavored(false),
212212
NumTypeVariables(typeVars.size()), Overload{type, choice, useDC},
213213
Locator(locator) {
214214
std::copy(typeVars.begin(), typeVars.end(), getTypeVariablesBuffer().begin());
@@ -219,8 +219,8 @@ Constraint::Constraint(ConstraintKind kind,
219219
Type second, ConstraintLocator *locator,
220220
SmallPtrSetImpl<TypeVariableType *> &typeVars)
221221
: Kind(kind), Restriction(restriction), HasRestriction(true),
222-
IsActive(false), IsDisabled(false), RememberChoice(false),
223-
IsFavored(false),
222+
IsActive(false), IsDisabled(false), IsDisabledForPerformance(false),
223+
RememberChoice(false), IsFavored(false),
224224
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
225225
Locator(locator) {
226226
assert(!first.isNull());
@@ -232,7 +232,8 @@ Constraint::Constraint(ConstraintKind kind, ConstraintFix *fix, Type first,
232232
Type second, ConstraintLocator *locator,
233233
SmallPtrSetImpl<TypeVariableType *> &typeVars)
234234
: Kind(kind), TheFix(fix), HasRestriction(false), IsActive(false),
235-
IsDisabled(false), RememberChoice(false), IsFavored(false),
235+
IsDisabled(false), IsDisabledForPerformance(false), RememberChoice(false),
236+
IsFavored(false),
236237
NumTypeVariables(typeVars.size()), Types{first, second, Type()},
237238
Locator(locator) {
238239
assert(!first.isNull());

0 commit comments

Comments
 (0)