Skip to content

Commit aa3b886

Browse files
committed
[ConstraintSystem] Attempt conjunction before closure result or generic parameter holes
Closure result type or generic parameter associated with such a location could bw inferred from a body of a multi-statement closure (when inference is enabled), so we need to give closure a chance to run before attemtping a hole for such positions in diagnostic mode.
1 parent 8b90154 commit aa3b886

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ class BindingSet {
338338

339339
TypeVariableType *getTypeVariable() const { return Info.TypeVar; }
340340

341+
/// Check whether this binding set belongs to a type variable
342+
/// that represents a result type of a closure.
343+
bool forClosureResult() const;
344+
345+
/// Check whether this binding set belongs to a type variable
346+
/// that represents a generic parameter.
347+
bool forGenericParameter() const;
348+
341349
bool canBeNil() const;
342350

343351
/// If this type variable doesn't have any viable bindings, or

lib/Sema/CSBindings.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ using namespace swift;
2525
using namespace constraints;
2626
using namespace inference;
2727

28+
bool BindingSet::forClosureResult() const {
29+
return Info.TypeVar->getImpl().isClosureResultType();
30+
}
31+
32+
bool BindingSet::forGenericParameter() const {
33+
return bool(Info.TypeVar->getImpl().getGenericParameter());
34+
}
35+
2836
bool BindingSet::canBeNil() const {
2937
auto &ctx = CS.getASTContext();
3038
return Literals.count(

lib/Sema/CSStep.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ StepResult ComponentStep::take(bool prevFailed) {
345345
auto *disjunction = CS.selectDisjunction();
346346
auto bestBindings = CS.determineBestBindings();
347347

348+
if (CS.shouldAttemptFixes()) {
349+
if ((bestBindings &&
350+
(bestBindings->forClosureResult() ||
351+
bestBindings->forGenericParameter()) &&
352+
bestBindings->isHole()) &&
353+
!disjunction) {
354+
if (auto *conjunction = CS.selectConjunction()) {
355+
return suspend(
356+
std::make_unique<ConjunctionStep>(CS, conjunction, Solutions));
357+
}
358+
}
359+
}
360+
348361
if (bestBindings &&
349362
(!disjunction || bestBindings->favoredOverDisjunction(disjunction))) {
350363
// Produce a type variable step.

0 commit comments

Comments
 (0)